Reputation: 81
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.
Upvotes: 2
Views: 3267
Reputation: 822
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.
Upvotes: 2
Reputation: 49215
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.
Upvotes: 0
Reputation: 2435
@Inject
private Conversation conversation;
conversation.begin();
conversation.end();
Pretty easy to google this? What exactly is the problem?
Upvotes: 0