13 October 2011

Listing long running conversations in CDI based web application

I am not sure if CDI provides APIs to list all long running conversations. So I came up with the below class.

Note: Time-out and eviction of conversations by the container when not in use for a long time to save resoruces is NOT considered in the code below.


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named
@SessionScoped
public class SRackWorkSpace implements Serializable{
   
    private List<String> cids=new ArrayList<String>();
   
    public void addConversation(String cid){
        cids.add(cid);
    }
   
    public void removeConversation(String cid){
        cids.remove(cid);
    }
   
    public boolean isConversationActive(String cid){
        return cids.contains(cid);
    }
   
   
}
Inject conversation and the above session scoped bean and invoke

 -  addConversation method when you begin a conversation like

                 conversation.begin("deliverexam");
                 sRackWorkSpace.addConversation("deliverexam");



- removeConversation when we end a conversation like

   sRackWorkSpace.removeConversation(conversation.getId());
            conversation.end();.




You can check of a long running conversation for a desired use case is already active like


 if (sRackWorkSpace.isConversationActive("deliverexam")) {
           return "/YOUR_PATH_TO_XHTML_VIEW?cid=deliverexam&faces-redirect=true";
}