2

I just wondered how it is possible to begin a new conversation in CDI. The container seems to begin a new conversation, if there is no cid request parameter, but in some cases I want to start a new conversation explicit from my controller.

Example: A JSF page is used to edit an enity hold by a conversation scoped controller bean. A ajax request is submitted, calling a method of the controller, which fires an event, passing a sub-entity that should be edited in a different controller within a new conversation. I cannot find a solution for that problem. I already looked into Apache Myfaces CODI but also cant find a pattern that solves that issue.

Please help me out of that.

[EDIT] Ok. Maybe i did not describe the problem detailed enough. one more try: if i want to access a new instance of the "same" conversationscoped controller bean which has a different (new) conversation id. how does that work? image you have.. lets say.. an OrderControllerBean to edit one Order entity. this controller has an JSF action method like public void createSubOrder(). this method should create a new Order and do some fancy stuff with it. After that it should be passed to a new OrderControllerBean instance with a different conversation id. maybe by using CDI events. doesn't matter. The problem is: The OrderControllerBean already exists in my conversation. it is used to edit the current Order. So it cannot be used to create and edit the sub-Order we just created. so how is it possible to create a new instance of the OrderControllerBean? Maybe this is not the right way to do this. But so far, I cannot find another possibily to create a new conversation, without sending the browser to a new url with a blank cid parameter. but then i cannot exchange information between theses conversations. how can i bring the non persistent Order created in conversation 1 to the same controller in conversation 2? My problem is just about conversation of conversations.

[EDIT] I read the entire documentation and all the examples coming with it so far. According to these statements (http://docs.jboss.org/cdi/spec/1.0/html/contexts.html#conversationcontext), CDI conversations seem to be closely tied to JSF. Unfortunately I cannot find a documentation which goes deeper into the technical details. Since there is no other way to obtain a new conversation as sending a new request, it is necessary to keep the logic about how and when a new conversation begins directly in the view layer. That's definitly not the best way I can imagine. My next attempt is the evaluation of Seam 3 since I can remember there were some @Begin and @End annotations in Seam 2 that gave more control over the conversations. There were also some tag libs that let you specify the conversation propagation. I am wondering why nobody else has similar questions to mine. Either CDI is not very widespread for complex projects at this moment, or I have a big problem understanding how things should work. So far I cannot find any CDI/CODI example that is more than just a simple demo. If anybody has a link to an example which shows the advanced use of conversations, regardless of which portable extension is used, please let me know.

hidehawk
  • 81
  • 1
  • 6
  • Just because you don't understand it, doesn't mean that something isn't used. @Begin/@End won't help you at all. Yes CODI doesn't ship a lot of examples. However, you have to understand something to use it correctly. You can do that by checking the API/SPI (java)docs or ask at the source. The only issue you have is that you like to pass an instance from a closed instance to a new instance. That just can't work without a storage which is active during the transition. If you have that it's perfectly supported by CODI (but not by std. CDI conversations). The rest is in the 'Edit' of my answer. – Dar Whi Dec 14 '12 at 10:28

3 Answers3

2

With CODI a conversation of a bean gets started autom. as soon as you access it. And you can have multiple conversations in parallel. Std. CDI conversations are more like the Window-Scope of CODI. But even here CODI is way better.

Std. CDI conversations need #begin, but that is an issue when it comes e.g. to a validation error. In this case you create your beans over and over again. I dropped the usage of std. CDI conversations after two weeks, because they are just utterly broken in many cases of apps which are more complex than one-day-demos. I found http://os890.blogspot.co.at/2011/04/slides-codi-conversations.html which helped me to understand why the CODI team did their own CDI scopes.

Also what you described is petty easy with CODI.

[EDIT]: What you added in your 'EDIT' can't work at all without a 2nd bean, but you will never get a new window-id. If you just need a new (/restarted) instance of your conversation scoped bean, you can use: org.apache.myfaces.extensions.cdi.core.api.scope.conversation.Conversation#restart You would need a 2nd bean which calls something like controller#getXyz controller#restart (which by itself calls Conversation#restart) and then controller#setXyz. However, it sounds very dirty what your are trying to do.

Dar Whi
  • 822
  • 5
  • 14
  • hey. thanks for your reply. I read all the CODI documentation but cannot find anything about beginning a new conversation. there are named conversation groups and so on but this is not what i need. I just want to begin a new, unamed, independend conversation, from somewhere in the backend, without submitting a new browser request. – hidehawk Dec 10 '12 at 16:34
  • You don't have to begin them! Just access the beans and the rest just works. You also don't start other scopes like the Request-Scope... – Dar Whi Dec 10 '12 at 17:05
  • Thanks again, for your addition. Maybe CODI does the job. I will keep on trying. – hidehawk Dec 17 '12 at 14:29
0
   @Inject
   private Conversation conversation;

conversation.begin();
conversation.end();

Pretty easy to google this? What exactly is the problem?

Karl Kildén
  • 2,415
  • 22
  • 34
  • try pseudo code to describe it better. I still cant understand the functional requirement you have. If something is very hard to do with CDI then chances are you are doing things wrong – Karl Kildén Dec 10 '12 at 21:41
0

What do you mean by the "firing an event"; JSF, ajax, CDI event?
Why do you want to start a new conversation for your sub-entity?
I think you should somehow request sub-entity editing page from the client without attaching the cid. So that the sub-entity's controller gains new transient conversation. You can then begin it in its @PostConstruct method. AFAIK you cannot start new (or restart) conversation in the same request.

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
  • I mean CDI events. The problem is, the only way I know how to begin a new CDI conversation is to open up a new web page with a blank cid parameter. There is no possibility to obtain a new conversation within the controller logic and redirect the user to the appropiate page (maybe) later. the jsf frontend has always to know about, whats going on with the conversations but i want to manage the conversations within the controller logic because my application is too complex to put all that stuff into the jsf page. – hidehawk Dec 10 '12 at 16:27