No active context exception
meetoblivion Jan 1, 2010 6:03 PMSo I'm not sure if there's other reasons for me not realizing this, but I just noticed these exceptiosn while playing with my app this morning.
Caused by: javax.enterprise.context.ContextNotActiveException: No active contexts for scope type javax.enterprise.context.SessionScoped at org.jboss.weld.BeanManagerImpl.getContext(BeanManagerImpl.java:928) at org.jboss.weld.bean.proxy.ClientProxyMethodHandler.getProxiedInstance(ClientProxyMethodHandler.java:140) at org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethodHandler.java:101) at security.MySecurityContext_$$_javassist_112.getUserName(MySecurityContext_$$_javassist_112.java) at org.jboss.dna.graph.request.processor.RequestProcessor.notifyObserverOfChanges(RequestProcessor.java:979) at org.jboss.dna.connector.store.jpa.model.simple.SimpleJpaConnection.execute(SimpleJpaConnection.java:138) at org.jboss.dna.graph.request.CompositeRequestChannel$2.call(CompositeRequestChannel.java:193) at org.jboss.dna.graph.request.CompositeRequestChannel$2.call(CompositeRequestChannel.java:183) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) ... 1 more
The way it works, when I am closing a session (via a disposer, since I don't own the Session object), I get this exception. From what I can tell, DNA is using a wrapper of my managed bean with the security context in a FutureTask.
String userName = context.getSecurityContext() != null ? context.getSecurityContext().getUserName() : null;
where context.getSecurityContext() is returning the instance of MySecurityContext that is a managed bean/CDI enabled bean. Since this happens in a FutureTask, I'm not sure what exactly should be happening. An interesting note, I can make it go away by creating a method in MySecurityContext:
public SecurityContext getSimpleSession() { return new SecurityContext() { @Override public String getUserName() { return username; } @Override public boolean hasRole(String roleName) { return MySecurityContext.this.hasRole(roleName); } @Override public void logout() { } }; }
and it works perfectly. I assume because this forces it out of the weld managed bean lifecycle. The question though, why won't it work with a FutureTask? I could have sworn that EJB Async was done via FutureTask, so it stands to reason that weld would work here as well. Should this be working?