我有一个基于JSF的应用程序,其中有一个受请求范围限制的托管bean。
我有一个具有static ConcurrentHashMap
的单例类,该类用于在我使用当前FacesContext
作为键的位置存储一些信息。 private static ConcurrentHashMap<FacesContext, ConcurrentHashMap<String, Object>> contextStore = new ConcurrentHashMap<>();
此类在整个应用程序中全局使用。
此地图将被多个请求共享。
对于每个请求,我都将其中的一些信息存储为FacesContext
作为密钥。在请求生命周期中,我需要在该应用程序的不同部分使用此映射,并且在请求完成后应将其清除,以避免内存泄漏。
即,当前的FacesContext
键应从此映射中删除。
完成当前请求后,是否有任何方法可以清除此地图(此类具有方法clearAllByContext
来清除当前FacesContext
的地图)?
以下是设置,获取和清除地图的方法。
// 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);
}
我需要通过在完成每个请求时致电clearAllByContext(FacesContext context)
来清除此地图。是否有可用的FacesContextListener,以便我可以在其中调用此方法?