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?