用httpessionlistener连接作用域



我正在编写一个允许人们协作的web应用程序。我希望有我的一些服务范围的协作(涉及几个人),而不是任何单独的http会话。我创建了一个存储bean的自定义Scope。为了管理bean生命周期,我按照如下方式跟踪关联的会话id:

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;
    }
}

当会话关闭时,我想从与给定bean名称关联的活动会话列表中删除会话id。当set变为空时,我可以清理。

我能想到的管理会话关闭的最简单方法是使用HttpSessionListener,但是我的Scope和侦听器之间断开了连接。我看到了以下可能性:

  1. 我可以静态地创建HttpSessionListener,假设有一个单独的实例,让它管理订阅列表,并让我的Scope实例订阅它的事件。但这似乎是多余的,我不喜欢单例模式。

  2. 如果我可以访问Scope中的HttpSession,我可以将Scope添加到会话中存储的列表中,并让侦听器通知该列表的成员会话即将结束。但是我不知道如何在Scope实例中获得会话对象(而不仅仅是它的id)。

  3. 我可以让我的Scope实现HttpSessionListener接口,从而直接更新其状态,但我不知道如何以编程方式注册侦听器。有没有一种公开的方式?

  4. 有更好的方法吗?

谢谢你的帮助,

基因

由于没有收到任何评论或回答,我选择了选项#1,如下:

public class SessionMonitor implements HttpSessionListener {
    protected final Log logger = LogFactory.getLog(getClass());
    protected CopyOnWriteArrayList<SessionEventListener> subscribers = new CopyOnWriteArrayList<SessionEventListener>();
    protected ConcurrentHashMap<String,HttpSession> sessions = new ConcurrentHashMap<String,HttpSession>();
    protected static SessionMonitor singleton;
    public static SessionMonitor soleInstance() throws ConfigurationException {
        if (singleton == null)
            throw new ConfigurationException("No SessionMonitor instance has been created");
        return singleton;
    }
    public SessionMonitor() {
        if (singleton == null)
            singleton = this;
    }
    @Override
    public void sessionCreated(HttpSessionEvent e) {
        HttpSession session = e.getSession();
        this.sessions.putIfAbsent(session.getId(), session);
        logger.trace("Registered session " + session.getId());
    }
    @Override
    public void sessionDestroyed(HttpSessionEvent e) {
        String sessionId = e.getSession().getId();
        this.sessions.remove(sessionId);
        for (SessionEventListener listener: subscribers)
            listener.sessionEnded(sessionId);
        logger.trace("Removed session " + sessionId);
    }
    public HttpSession getSession(String id) {
        return this.sessions.get(id);
    }
    public void addListener(SessionEventListener listener) {
        this.subscribers.add(listener);
        logger.trace("Added listener " + listener);
    }
    public void removeListener(SessionEventListener listener) {
        this.subscribers.remove(listener);
        logger.trace("Removed listener " + listener);
    }
}

当范围被创建时,它将自己注册到SessionMonitor:

public ConditionalScope() throws ConfigurationException {
    logger.debug("Registering " + this.toString() + " for session monitoring");
    SessionMonitor.soleInstance().addListener(this);
}
我不清楚,然而,何时从SessionMonitor中删除Scope。这里会有某种WeakArray吗?

最新更新