0

I have an application which is based on JSF in which I have a managed bean with request-scoped.

I have a singleton class having static ConcurrentHashMap, this class is used to store some information where I am using current FacesContext as the key. private static ConcurrentHashMap<FacesContext, ConcurrentHashMap<String, Object>> contextStore = new ConcurrentHashMap<>();

This class is used globally across the application.

This map will be shared by multiple requests.

For every request, I am storing some information in it as the FacesContext as the key. I need this map in different parts of this application during the request lifetime and it should be cleared when the request is completed in order to avoid the memory leak.

ie, the current FacesContext key should be removed from this map.

Is there any way to clear this map (this class has a method clearAllByContext to clear the map for the current FacesContext)on completing the current request?

The following are the methods to set, get, and clear the Map.

 
  
    

   
    // Method to set values 
        public static void set(FacesContext context, String key, Object value) {
        
          ConcurrentHashMap eachStoreByContext = contextStore.get(context);
        
          if (eachStoreByContext == null) {
            
               eachStoreByContext = new ConcurrentHashMap();
            
               contextStore.put(context, eachStoreByContext);
        
          }
        
          eachStoreByContext.put(key, value);
    
       }

 
       // Method to get values  
       public static Object get(FacesContext context, String key) {
        
            ConcurrentHashMap eachStoreByContext = contextStore.get(context);
        
            if (eachStoreByContext != null) {
            
                 return eachStoreByContext.get(key);
        
             }
        
             return null;
    
        }

 
        //Method to clear all values for the Facescontext   
        public static void clearAllByContext(FacesContext context) {
        
             contextStore.remove(context);
    
        }



I need to clear this Map by calling clearAllByContext(FacesContext context) on completing every request. Is there any FacesContextListener available so that I can invoke this method in that?

  • Did you alread see [this question](https://stackoverflow.com/questions/1282251/saving-data-to-session-in-jsf). Also can you describe a bit more clearly what you are trying to do (examples what you have tried yet, etc.). – fuggerjaki61 May 17 '20 at 14:47
  • @fuggerjaki61, Yes I saw the link you have shared in the above comment. That doesn't solve my problem. I have edited my question and included some more information. As I mentioned in the question, I need to clear this map by removing the facesContext key on completing every request, where facesContext is the current faces context. – Arun Prabhath May 17 '20 at 16:24
  • Always include links you found that seem to be related but did not solve your problem. Saves us from searching and commenting. See [ask] – Kukeltje May 18 '20 at 06:10
  • What is your functional requirement for which you think you need this solution? Are you sort of creating a cusom scope? – Kukeltje May 18 '20 at 06:13
  • My question is “Do we have an option to hook a method which should be executed on completing the request in jsf based application? “ – Arun Prabhath May 18 '20 at 06:48
  • What's about https://stackoverflow.com/questions/5719336/execute-javascript-after-every-jsf-ajax-postback and https://stackoverflow.com/questions/24912335/execute-javascript-before-and-after-the-fajax-listener-is-invoked – Holger May 18 '20 at 07:13
  • I think a [`SystemEventListener`](https://stackoverflow.com/questions/13748202/how-to-register-a-componentsystemeventlistener-for-all-uiinputs) could do this with the `PostRenderViewEvent`. – fuggerjaki61 May 18 '20 at 09:37

0 Answers0