How to get SessionContext in JBOSS
I tried several ways in the session bean, like:
@Resource
private SessionContext ctx;
OR
private SessionContext ctx;
@Resource
private void setSessionContext(SessionContext ctx) {
this.sctx = ct开发者_JAVA技巧x;
}
OR
InitialContext ic = new InitialContext();
SessionContext ctx = (SessionContext) ic.lookup("java:comp/env/sessionContext");
None of them worked, differnet exceptions occured in JBOSS.
I really get mad about it. Anyone could tell me what's wrong. Thanks a lot!
The two first solutions (field injection and setter method injection) look fine and should work.
I have a doubt about the third one (the lookup approach) as you didn't show the corresponding @Resource(name="sessionContext")
annotation but it should work too if properly used.
A fourth option would be to look up the standard name 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);
}
}
}
These four approaches are all EJB 3 compliant and should definitely work with any Java EE 5 app server as reminded in 4 Ways to Get EJBContext in EJB 3. Please provide the full stack trace of the exception that you get if they don't.
You can list these bindings using the following code, it will show you whats available in the context. (This uses groovy code to do the iteration (each) over the enumeration)
Context initCtx = new InitialContext();
Context context = initCtx.lookup("java:comp") as Context
context.listBindings("").each {
println it
}
Dependending if this code is run in an ejb context or web context you would see different output.
You can get SessionContext as follows:
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;
}
}
My JNDILookupClass is as follows:
public class JNDILookupClass { private static Context initialContext; private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory"; private static final String PKG_INTERFACES ="org.jboss.ejb.client.naming"; private static final String PROVIDER_URL = "http-remoting://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;
}
}
My jboss-ejb-client.properties is as follows:
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
My Ejb bean is as follows:
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();
}
}
My Ejb Interface is as follows:
package com.ejb3.tutorial.stateless.bean;
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
public Long addBid(String userId,Long itemId,Double bidPrice);
}
精彩评论