Glassfish - Preserve sessions across redeployment - SessionListener is not called on session recreation

半城伤御伤魂 提交于 2019-12-08 03:55:58

问题


So I made a simple session listener - there are many on the web :

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
class SessionListener implements ServletContextListener, HttpSessionListener {

    private static final int MAX_INACTIVE_INTERVAL = 1000; // in secs
    // static AtomicInteger numOfSessions;
    // singleton ? static ?
    static int numOfSessions;
    static ServletContext context;

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        se.getSession().setMaxInactiveInterval(MAX_INACTIVE_INTERVAL);
        increase();
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        decrease();
    }

    private synchronized void increase() {
            ++numOfSessions;
            context.setAttribute("numberOfSessions", numOfSessions);
            System.out.println("SessionListener - increase - numberOfSessions = " +
                               numOfSessions);
    }

    private synchronized void decrease() {
            --numOfSessions;
            context.setAttribute("numberOfSessions", numOfSessions);
            System.out.println("SessionListener - decrease - numberOfSessions = " +
                               numOfSessions);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("SessionListener - contextDestroyed");
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        context = sce.getServletContext();
        System.out.println("SessionListener - contextInitialized : " +
                           context);
    }
}

_heavily_edited_

I am on Glassfish 3.1.2 on Eclipse Juno. The session is created via request.getSession() in the doPost() method in the relevant servlet. When I redeploy the project (on save) decrease() is called - session gets invalidated naturally.

Now, "Preserve sessions across redeployment" is on by default in the Eclipse glassfish plugin - so when I again save the project in Eclipse and is redeployed I get :

INFO: SessionListener - decrease - numberOfSessions = -1

Meaning : GF recreates the sessions BUT does not call the listener - so on redeployment a session is invalidated - but since sessionCreated() was not called my session count is on 0.

I need a workaround for this !

Historical (it helped me understand what was going on) :

if you modify and recompile a java program with tomcat running, tomcat first removes all sessions via calling the session listener, and then re-create new sessions objects with same session IDs edit and all attributes apart from non serializable objects (?) /edit, but this time it does not call registered session listeners when it does this.

NB : I knew nothing of session preservation and since the session was not preserved entirely (a POJO session attribute was annihilated - as I understand it now it should be serializable to be preserved - right ? docs ?) it really took a while to understand what was going on.

来源:https://stackoverflow.com/questions/11551788/glassfish-preserve-sessions-across-redeployment-sessionlistener-is-not-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!