I am writing an web application that allows people to collaborate. I would like to have some of my services scoped to the collaboration (which involves a few people) rather than to any individual http session. I created a custom Scope that stores the beans. To manage the bean lifecycle, I keep track of the session ids associated as follows:
protected ConcurrentMap<String,Object> attributes =
new ConcurrentHashMap<String, Object>();
...
@Override
public Object get(String name, ObjectFactory<?> factory) {
synchronized(this.attributes) {
Object scopedObject = this.attributes.get(name);
if (scopedObject == null) {
scopedObject = factory.getObject();
this.attributes.put(name, scopedObject);
RequestAttributes reqAttrs = RequestContextHolder.currentRequestAttributes();
activeSession(name).add(reqAttrs.getSessionId());
}
return scopedObject;
}
}
When a session closes, I would like to remove the session id from the list of active sessions associated with a given bean name. When set becomes empty, I can clean up.
The easiest way I can think of the manage session closing is with an HttpSessionListener, but I have a disconnect between my Scope and the listener. I see the following possibilities:
I can create the
HttpSessionListenerstatically, assume a sole instance, have it manage a subscription list, and have myScopeinstances subscribe to its events. But that seems redundant, and I don't like the singleton pattern for this.If I had access to the
HttpSessionin theScope, I could add theScopeto a list stored in the session, and have the listener notify the members of that list that the session is going away. But I don't see how to get my hands on the session object (rather than just its id) in theScopeinstance.I can make my
Scopeimplement theHttpSessionListenerinterface and thereby update its state directly, but I don't know how to register a listener programmatically. Is there a public way of doing that?Is there a better way?
Thanks for your help,
Gene