Search code examples
jakarta-eejsf-2cdi

How to cleanly end a CDI @ConversationScoped


My Project is using JSF2.0 and CDI. For one page, I want my backing bean to match the lifespan of the page. @ViewScoped seems a perfect fit but it's not part of CDI and then make our solution not consistent. Then my next option would be the CDI @ConversationScoped. Seems to me the only way to mark the boundary of a conversation is the program way via conversation.begin and conversation.end (I have used Seam 2.x, there you can use annotations to mark conversation boundary). My page is sitting in a common layout with global navigation, which means there are "unlimited" ways to leave my page. How can I make sure the conversation is ended whichever way the user might choose (e.g. clicking on a global navi option which is totally outside of my backing bean's control)? And I hope the solution would not spread the code to other modules; and if that's inevitable, I hope it could be implemented in a cross-cutting manner (AOP).


Solution

  • This can be achieved with a custom ConfigurableNavigationHandler.

    1. Implement a JSF NavigationHandler

       public class NavigationHandlerTest extends ConfigurableNavigationHandler {
      
       private NavigationHandlerTest concreteHandler;
      
       public NavigationHandlerTest(NavigationHandler concreteHandler) {
            this.concreteHandler = concreteHandler;
       }
      
      
       @Override
       public void handleNavigation(FacesContext context, String fromAction, String outcome) 
       {
          //here, check where navigation is coming from and based on that, retrieve the CDI bean and kill the conversation
           if(fromAction.equals("someAction"){
           BeanManager theBeanManager = getBeanManager(context);
           Bean bean = theBeanManager.getBeans("yourCDIBean").iterator().next() 
           CreationalContext ctx = theBeanManager.createCreationalContext(bean);
           MyBeanType o = theBeanManager.getReference(bean, bean.getClass(), ctx); //retrieve the bean from the manager by name. You're guaranteed to retrieve only one of the same name;
           o.getConversation.end(); //end the conversation from the bean reference
            }
      
           //proceed with normal navigation
           concreteHandler.handleNavigation(context, fromAction, outcome);   
      
       }
      
       //This method provides access to the cdi bean manager. You need it to be able to 
       //gain access to the cdi bean and from that to the injected conversation instance
       public BeanManager getBeanManager(FacesContext facesContext){
         BeanManager cdiBeanManager = (BeanManager)((ServletContext) facesContext.getExternalContext().getContext()).getAttribute("javax.enterprise.inject.spi.BeanManager"); 
          return cdiBeanManager;
       }
      
      
         } 
      
    2. Register your custom navigation handler in the faces-config.xml

       <application>
           <navigation-handler>com.foo.bar.NavigationHandlerTest</navigation-handler>
       </application>
      

    This approach is centralized and minimally invasive