我在会话bean中尝试了几种方法,例如:
@Resource
private SessionContext ctx;或
private SessionContext ctx;
@Resource
private void setSessionContext(SessionContext ctx) {
this.sctx = ctx;
}或
InitialContext ic = new InitialContext();
SessionContext ctx = (SessionContext) ic.lookup("java:comp/env/sessionContext");它们都不起作用,在JBOSS中出现了不同的异常。
我真的对此很生气。任何人都能告诉我出了什么问题。非常感谢!
发布于 2009-12-21 14:57:34
前两个解决方案(字段注入和设置器方法注入)看起来很好,应该可以在上工作。
我对第三种方法(查找方法)有疑问,因为您没有显示相应的@Resource(name="sessionContext")注释,但如果使用得当,它的也应该可以使用。
第四种选择是查找标准名称java:comp/EJBContext
@Stateless
public class HelloBean implements com.foo.ejb.HelloRemote {
public void hello() {
try {
InitialContext ic = new InitialContext();
SessionContext sctxLookup =
(SessionContext) ic.lookup("java:comp/EJBContext");
System.out.println("look up EJBContext by standard name: " + sctxLookup);
} catch (NamingException ex) {
throw new IllegalStateException(ex);
}
}
}这四种方法都是EJB3兼容的,应该可以与4 Ways to Get EJBContext in EJB 3中提到的任何JavaEE5应用服务器一起工作。如果没有,请提供您得到的异常的完整堆栈跟踪。
发布于 2022-02-23 06:53:16
您可以通过如下方式获取SessionContext:
package com.ejb3.tutorial.stateless.client;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.ejb3.tutorial.stateless.clientutility.JNDILookupClass;
import com.ejb3.tutorial.stateless.bean.EmployeeBean;
import com.ejb3.tutorial.stateless.bean.EmployeeServiceRemote;
public class Main {
public static void main(String[] a) throws Exception {
EmployeeServiceRemote service = null;
Context context = JNDILookupClass.getInitialContext();
String lookupName = getLookupName();
service = (EmployeeServiceRemote) context.lookup(lookupName);
service.addBid("userId",1L,0.1);
}
private static String getLookupName() {
/*The app name is the EAR name of the deployed EJB without .ear
suffix. Since we haven't deployed the application as a .ear, the app
name for us will be an empty string */
String appName = "SessionContextInjectionEAR";
/* The module name is the JAR name of the deployed EJB without the
.jar suffix.*/
String moduleName = "SessionContextInjection";
/* AS7 allows each deployment to have an (optional) distinct name.
This can be an empty string if distinct name is not specified.*/
String distinctName = "";
// The EJB bean implementation class name
String beanName = EmployeeBean.class.getSimpleName();
// Fully qualified remote interface name
final String interfaceName = EmployeeServiceRemote.class.getName();
// Create a look up string name
String name = "ejb:" + appName + "/" + moduleName + "/" +
distinctName + "/" + beanName + "!" + interfaceName +"?stateless";
System.out.println("lookupname"+name);
return name;
}
}我的JNDILookupClass如下:
公有类远程{私有静态上下文远程;私有静态最终串INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";私有静态最终串PKG_INTERFACES ="org.jboss.ejb.client.naming";私有静态最终串PROVIDER_URL =“http- JNDILookupClass ://127.0.0.1:8080”;
public static Context getInitialContext() throws NamingException {
if (initialContext == null) {
Properties clientProperties = new Properties();
clientProperties.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
clientProperties.put(Context.URL_PKG_PREFIXES, PKG_INTERFACES);
clientProperties.put(Context.PROVIDER_URL, PROVIDER_URL);
clientProperties.put("jboss.naming.client.ejb.context", true);
initialContext = new InitialContext(clientProperties);
}
return initialContext;
}}
我的jboss-ejb-client.properties如下:
endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.password=admin123
remote.connection.default.host=localhost
remote.connection.default.port=8080
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=adminUser我的Ejb bean如下所示:
package com.ejb3.tutorial.stateless.bean;
import java.util.Date;
import javax.annotation.Resource;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
/**
* Session Bean implementation class EmployeeBean
*/
@Stateless
@Local({EmployeeServiceLocal.class})
@Remote({EmployeeServiceRemote.class})
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
@Resource
private SessionContext ctx;
public EmployeeBean() {
}
public Long addBid(String userId, Long itemId, Double bidPrice) {
System.out.println("Bid for " + itemId + " received with price" + bidPrice);
TimerService timerService = ctx.getTimerService();
Timer timer = timerService.createTimer(new Date(new Date().getTime()), "Hello World");
return 0L;
}
@Timeout
public void handleTimeout(Timer timer) {
System.out.println(" handleTimeout called.");
System.out.println("---------------------");
System.out.println("* Received Timer event: " + timer.getInfo());
System.out.println("---------------------");
timer.cancel();
}
}我的Ejb接口如下:
package com.ejb3.tutorial.stateless.bean;
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
public Long addBid(String userId,Long itemId,Double bidPrice);
}https://stackoverflow.com/questions/1938517
复制相似问题