How to access a stateful bean in a listener
I have a Java EE project with an EJB containing a Remote Stateful Bean.
I have an enterprise application with a GUI.
I have listener in my GUI but my problem is that I can't access to my Bean in a button listener (to check username and password).
I use netBeans and glassFish.
To lookup beans I use:
ctx.lookup("java:comp/env/BeanStateful");
But ap开发者_如何学Pythonparently the listener change the context and it doesn't work anymore.
The listener is probably running on a thread that does not have a JavaEE context. Try obtaining the java:comp/env Context from the main thread, and then only lookup() the bean name from the listener. For example:
// Member variables
private Context _javaCompEnvContext;
...
// Main thread
_javaCompEnvContext = ctx.lookup("java:comp/comp/env");
...
// Listener thread
BusinessIntf bean = _javaCompEnvContext.lookup("BeanStateful");
精彩评论