4

I need to inject a ConversationScoped bean into a servlet. i use the standard simple @Inject tag and I invoke the servlet with the cid parameter but when it invokes any method in the injected bean I get the following error:

org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.ConversationScoped

Can I inject these beans in servlets or I can inject only Session and Request scoped beans?

skaffman
  • 398,947
  • 96
  • 818
  • 769
icordoba
  • 1,834
  • 2
  • 33
  • 60
  • Looks like there is no conversation at the moment you try to use your @ConversationScoped bean. Are you using it in doGet/doPost method? If so, there should be conversation created automatically for the request, or beginned by you "by hand" if you do it. – amorfis Feb 04 '11 at 11:47

2 Answers2

1

In a servlet the context is application context that's why you loose conversation scope. Here is a small utility class that you can use as an anonymous class and wrap the request with if you want conversation scope support in servlets...

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.jboss.weld.Container;
import org.jboss.weld.context.ContextLifecycle;
import org.jboss.weld.context.ConversationContext;
import org.jboss.weld.servlet.ConversationBeanStore;


public abstract class ConversationalHttpRequest {
    protected HttpServletRequest request;


    public ConversationalHttpRequest(HttpServletRequest request) {
        this.request = request;
    }

    public abstract void process() throws Exception;

    public void run() throws ServletException {
        try {
            initConversationContext();
            process();
        } catch (Exception e) {
            throw new ServletException("Error processing conversational request", e);
        } finally {
            cleanupConversationContext();
        }
    }

    private void initConversationContext() {
        ConversationContext conversationContext = Container.instance().deploymentServices().get(ContextLifecycle.class).getConversationContext();
        conversationContext.setBeanStore(new ConversationBeanStore(request.getSession(), request.getParameter("cid")));
        conversationContext.setActive(true);
    }

    private void cleanupConversationContext() {
        ConversationContext conversationContext = Container.instance().deploymentServices().get(ContextLifecycle.class).getConversationContext();
        conversationContext.setBeanStore(null);
        conversationContext.setActive(false);
    }

}
Anthony O.
  • 22,041
  • 18
  • 107
  • 163
EricParis16
  • 809
  • 5
  • 9
  • The problem is that first, you depend on Weld implementation of CDI (and don't use necessarily your Application Server implementation), and secondly, `Container.instance().deploymentServices` is `private`. – Anthony O. Mar 19 '12 at 14:18
0

What is the equivalent of ConversationContext proposed in the previous answer in Java EE if we don't use Weld?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
dpalacio
  • 436
  • 4
  • 13