0

Is there a way to store some bean inside the conversation context? I.e for each new conversation, a new separate bean is created belonging to it.

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
azerIO
  • 519
  • 1
  • 9
  • 19
  • The web-application might have multiple simultaneous conversations. Each conversation has to hold its own data not interfering with the data of other conversations. – azerIO Aug 15 '12 at 22:12
  • I'm confused. The way you described this question makes it sound like you already know about the Conversation scope and the CDI ConversationScoped annotation. What don't you understand how to do ? – Craig Ringer Aug 20 '12 at 13:24
  • Actually I'm very new to CDI and Seam. I know that up to Seam 3 there was an easy possibility to inject/outject data to the scope using just In and Out annotations. I'm not sure how one can do it in Seam 3. I read about Produces annotation and tried it out without any luck. I'd very appreciate if you had a code snippet doing that. – azerIO Aug 20 '12 at 13:52
  • Hmm. I found this: http://stackoverflow.com/questions/11891035/how-to-retrieve-all-existing-long-running-conversations-in-weld It seems that there is only one long-running conversation per given session. – azerIO Aug 20 '12 at 14:00
  • That sounds ... odd, and appears to be contradicted by [the CDI/Weld documentation](http://docs.jboss.org/weld/reference/latest/en-US/html/scopescontexts.html). That said, I haven't been using JSF2 and CDI much lately. I never needed multiple conversations so I never tested it. I'd recommend writing a simple test to demonstrate it. – Craig Ringer Aug 20 '12 at 14:08

1 Answers1

2

The easiest way to do what you want is to declare a ConversationScoped managed bean or EJB where JSF2 manages the scope.

There are some good explanations here:

... any of which will do a better job than I will. The very short version is that you annotate a bean - which can be a plain POJO that follows the bean conventions - with the @ConversationScoped annotation. You then @Inject a Conversation object, which you can use to begin() and end() conversations. Inject this @ConversationScoped bean into other things. The Conversation.begin and Conversation.end methods control its lifecycle.

There's a bit much code to just post here, but the above links should help.

An alternative to @ConversationScoped POJO managed beans can be @Stateful @ConversationScoped EJBs. They can be really handy when you need EJB services in a conversation.

For some of the conceptual background and detail read the CDI/Weld reference on scopes - and the rest of the CDI/Weld manual. It's really well written and really good.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • Thx. One of your links led to the @WindowsScoped beans of Apache CODI. That is precisely what I need. For all others, whoever wants to install it, you need to use the bundled version of located here: http://central.maven.org/maven2/org/apache/myfaces/extensions/cdi/bundles/myfaces-extcdi-bundle-jsf20/1.0.5/myfaces-extcdi-bundle-jsf20-1.0.5.jar. Using the normal version gave me some errors during JBoss 7.x start-up. – azerIO Aug 22 '12 at 10:29
  • One more thing to add. Due to several bugs I'd recommend updating the WELD of your JBoss 7.x to version 1.1.8 which fixes the issues. – azerIO Aug 22 '12 at 13:54
  • No idea. I was dealing with the SEAM's ConversationScoped for about a week and couldn't get it to store data per conversation. It seems that the conversation context is different for each conversation created but the conversationscoped beans are created just once for all long-running conversations and get shared between them. – azerIO Aug 22 '12 at 13:55
  • @azerIO I just run on 7.2-alpha builds instead, 'cos I need several other bug fixes that won't land until 7.1.2.Final or 7.2.0.Final get released :-( – Craig Ringer Aug 22 '12 at 23:35