-
1. Re: Conversation Scope in Weld 1.1.0-Beta2 (with Glassfish 3.1)
swiegersf.francois.swiegers.gmail.com Nov 25, 2010 1:57 AM (in response to swiegersf.francois.swiegers.gmail.com)OK, to simplify a bit, I took a look at the ExecutorHandler from Seam Remoting (the real latest one from
github), and according to how they do things, the following should work:public class MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Inject @Http ConversationContext conversationContext; @Inject Conversation conversation; @Inject BeanManager beanManager; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String cid = request.getParameter("cid"); conversationContext.activate(cid); try { Bean<?> bean = beanManager.getBeans(MyConversation.class) .iterator().next(); CreationalContext<?> ctx = beanManager .createCreationalContext(bean); MyConversation mc = (MyConversation) beanManager.getReference(bean, MyConversation.class, ctx); String out = mc.increment(); response.getOutputStream().println("<html><p>"); response.getOutputStream().println( "Value for " + conversation.getId() + " is : " + out); response.getOutputStream().println("</p></html>"); response.getOutputStream().close(); } finally { conversationContext.deactivate(); } } @ConversationScoped public static class MyConversation implements Serializable { private static final long serialVersionUID = 1L; @Inject Conversation conversation; private int num; public String increment() { if (conversation.isTransient()) { conversation.begin(); } return String.valueOf(num++); } } }
The above servlet should allow the user to increment the value in a conversation by simply appending the cid parameter to the servlet URL.
However, it simply does not work. The
conversationContext.activate(cid)
method simply creates a new conversation, regardless of whether that conversation ID was generated and made non-transient in a previous request.
Does Weld support conversations from servlets? I don't mind doing a little non-portable tap-dancing, but if Weld does not currently support this, can you please let me know?
thanks
-
2. Re: Conversation Scope in Weld 1.1.0-Beta2 (with Glassfish 3.1)
swiegersf.francois.swiegers.gmail.com Nov 26, 2010 1:15 AM (in response to swiegersf.francois.swiegers.gmail.com)Just an update to this, I made the above code work by simply adding
request.getSession();
as the first statement in my servlet. It seems that the ConversationManager requires the session to be instantiated before calling the deactivate() method, which is reasonable. It is only in small example apps that the session would not be created by the time the conversation is deactivated. :)
-
3. Re: Conversation Scope in Weld 1.1.0-Beta2 (with Glassfish 3.1)
cjalmeida Dec 1, 2010 5:38 PM (in response to swiegersf.francois.swiegers.gmail.com)Yet, this is a hard to debug problem. Should be part of the Javadoc.
-
4. Re: Conversation Scope in Weld 1.1.0-Beta2 (with Glassfish 3.1)
ssamayoagt Jan 11, 2011 4:02 PM (in response to swiegersf.francois.swiegers.gmail.com)FWIW, Same with Tomcat 7 + weld 1.1.0CR1, I mean:
request.getSession();
Regards. -
5. Re: Conversation Scope in Weld 1.1.0-Beta2 (with Glassfish 3.1)
kibojan Feb 23, 2011 3:02 PM (in response to swiegersf.francois.swiegers.gmail.com)Thanks for the tip. Really helpful.
I like when people share theirfindings
. :)