You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by Apache Wiki <wi...@apache.org> on 2007/05/24 20:21:47 UTC

[Myfaces Wiki] Update of "Conversation Termination" by WernerPunz

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Myfaces Wiki" for change notification.

The following page has been changed by WernerPunz:
http://wiki.apache.org/myfaces/Conversation_Termination

New page:
= Conversation Termination =
== Introduction ==
Ok we are done with it, we have done everything we want inside our converstion. When is the conversation closed, how can I force a close, is it possible to close all currently running conversations?

== We are done with it ==
Lets again look at the example from our ["BO/DAO Patterns in Orchestra"] page:

{{{
   public class BasicViewController {
      BasicBO basicbo;

      MyEntity workEntity;


      public void doAction() {
          basicbo.doSomething(workEntity);
          return "success";
      }

      public void doWearedonewithit() {
         Conversation.getCurrentInstance().invalidate();
         return "done";  
      }      

      public void valueBound(ConversationBindingEvent arg0) {
        workEntity = basicbo.loadEntity(); //we first init it with an empty query
      }



     //setters and getters
     public  BasicBO set....  
  }
}}}

As we already said, we are done with it, we are ready for the weekly recycling...

so what happens.

 1. We simply could forget about it and wait for the next timeout to close and dispose everything
 2. We could manually interfere by triggering an action
 3. We could terminate all Currently running conversations (which also means conversations which are tangaling in the air for whatever reason waiting for their timeout.

in the example above we decided to go for number two, we try to terminate our conversation because we trigger an action which just said ok, i am done (endstate)


{{{
   public void doWearedonewithit() {
         Conversation.getCurrentInstance().invalidate();
         return "done";  
   }      
}}}

So what happens, if the user jumps out of our page without triggering the action.
In this case 1. is triggered, the conversation will be hanging until recovered or terminated.

So it is advicable to locate central points in your application when you can terminate all conversations, or at least the current conversation which at that point probably still is referenced (usually a central navigation or a main page is a good place for it)

{{{

 ConversationManager.getInstance()
                        .clearCurrentConversationContext();

}}}

Would clear all conversations in our current conversation context (most of the times there is only one context, multiple contexts usually exist in multi window solutions.


A nav handler doing this could look like following:

{{{

public class FusionNavigationHandler extends NavigationHandler {

    private static final String CONVNAV_PREFIX = "convnav_";
    private NavigationHandler _handler;
  
    
    public FusionNavigationHandler(NavigationHandler handler) {
        super();
        _handler = handler;
    }

    public void handleNavigation(FacesContext context, String fromAction,
            String outcome) {
        if (outcome != null && outcome.startsWith(CONVNAV_PREFIX)) {
            try {
                //We drop the navigation just in case to add some cleanup
                ConversationManager.getInstance()
                        .clearCurrentConversationContext();
            } catch (RuntimeException e) {
                LogFactory.getLog(this.getClass()).error(e);
            }            
        }
        _handler.handleNavigation(context, fromAction, outcome);

    }

}
}}}

This is a nav handler which adds the implicit behavior to the applications navigation, that every navigation case, starting with convnav_ triggers a conversation clearup automatically:

This could look like this in our faces-config.xml

{{{

	<navigation-rule>
		<from-view-id>/*</from-view-id>
		<navigation-case>
			<from-outcome>convnav_template</from-outcome>
			<to-view-id>/template/master.jsf</to-view-id>
		</navigation-case>
		<navigation-case>
			<from-outcome>convnav_room</from-outcome>
			<to-view-id>/room/master.jsf</to-view-id>
		</navigation-case>
		<navigation-case>
			<from-outcome>convnav_user</from-outcome>
			<to-view-id>/user/master.jsf</to-view-id>
		</navigation-case>
		<navigation-case>
			<from-outcome>convnav_login</from-outcome>
			<to-view-id>/login/login.jsf</to-view-id>
		</navigation-case>
        </navigation-rule>
}}}

This examples a central inter module navigation for an application linking the various modules in combination with our navigation handler, every
time the application jumps from one module to the other the conversations are cleared up.
So in our case we can be sure that from time to time a semi automatic garbage collection consuming tangling conversation happens without any code interference into our application logic.