I have a SessionScoped class. For each user access, I need my own instance of this class. Everything went well. But now I also need access to these objects from the backend without any user interaction. I have an idle bean, but whenever I want to access an object with a limited session, I get excepiton. A simple code example looks like this:
@SessionScoped public class SessionObj implements Serializable { public String getValue() { return "Hello World"; } } @Stateless public class StatelessBean { private static final Logger LOG=Logger.getLogger(StatelessBean.class); @Inject private SessionObj sessionObj; public void test() { LOG.info("session object: "+sessionObj); LOG.info("Method call: "+sessionObj.getValue()); } }
But the test method call ends as an exception, for example:
12:19:10,484 ERROR [org.jboss.as.ejb3.tx.CMTTxInterceptor] (EJB default - 6) javax.ejb.EJBTransactionRolledbackException: WELD-001303 No active contexts for scope type javax.enterprise.context.SessionScoped 12:19:10,484 ERROR [org.jboss.ejb3.invocation] (EJB default - 6) JBAS014134: EJB Invocation failed on component StatelessBean for method public void package.StatelessBean.test(): javax.ejb.EJBTransactionRolledbackException: WELD-001303 No active contexts for scope type javax.enterprise.context.SessionScoped at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleInCallerTx(CMTTxInterceptor.java:139) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final] at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:204) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final] at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:306) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final] at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final] at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final] ... Caused by: org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.SessionScoped at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:598) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31] at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:71) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31] at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:79) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31] at package.SessionObj$Proxy$_$$_WeldClientProxy.toString(SessionObj$Proxy$_$$_WeldClientProxy.java) [ws_core_job_manager.jar:] at java.lang.String.valueOf(String.java:2826) [rt.jar:1.6.0_21] at java.lang.StringBuilder.append(StringBuilder.java:115) [rt.jar:1.6.0_21] at package.StatelessBean.test(StatelessBean.java:29) [ws_core_job_manager.jar:] ...
So my question is: * Is it possible to access the object even without a session using any trick? * Is it possible to generate one session from my class without a name?
I understand the reason for the exception, but I want to have one βglobalβ session for this new use of existing code. Of course, in many cases this is not so simple, and changing the session-bound code to the POJO and the container limited to the session is not so simple.
Environment:
Decision:
As mentioned by Yang: Extend StatelessBean as follows:
@Stateless public class StatelessBean { private static final Logger LOG=Logger.getLogger(StatelessBean.class); @Inject private BoundSessionContext sessionContext; @Inject private SessionObj sessionObj; public void test() { Map<String,Object> myMap=new HashMap<String,Object>(); sessionContext.associate(myMap); sessionContext.activate(); LOG.info("session object: "+sessionObj); LOG.info("Method call: "+sessionObj.getValue()); sessionContext.invalidate(); sessionContext.deactivate(); } }
And the example works! Now I just have to understand the details; -)
source share