0

Spring Boot supports persistent sessions through the configuration property

server.servlet.session.persistent=true

This makes sure that Tomcat persists the current sessions to disk and loads them again on a server restart.

I want to have the sessions loaded from disk available in the SessionRegistry. This behavior is not provided by Spring Boot and proposals to support it have been rejected several times. I don't want to use Spring Session. I'm running a single instance of my application and don't need to share sessions between instances. I've read several posts that claim to have implemented this behavior for Tomcat session persistence through a filter, but I haven't found a full example online.

A comment by Paul Khodchenkov says:

StandardManager,PersistentManager,DeltaManager fire http session created/destroyed/passivate/activate events in differents ways.

  1. StandardManager : will fire session activate event for every serialized session on startup (HttpSessionActivationListener object should be stored inside a session), but does not fire http session created event
  2. DeltaManager : fire http session created event for every session on startup
  3. PersistentManager : does NOT load session on startup. Sessions are restored lazily(and http session created event is fired) only when the required session is accessed by http.

Spring Boot seems to configure the StandardManager. So the way to get notified about restored sessions seems to be a HttpSessionActivationListener. In a comment on StackOverflow Alexey Usharovski says that Spring Boot doesn't support HttpSessionActivationListeners and indeed if I add a listener like this it doesn't get called:

@Component
public class RestoredSessionListener implements HttpSessionActivationListener {

    private final SessionRegistry sessionRegistry;

    public RestoredSessionListener(SessionRegistry sessionRegistry) {
        this.sessionRegistry = sessionRegistry;
    }

    @Override
    public void sessionDidActivate(HttpSessionEvent se) {
        sessionRegistry.registerNewSession(se.getSession().getId(), null);
    }
}

In the above post by Paul Khodchenkov says that a HttpSessionActivationListener object should be stored inside a Session. StandardSession indeed has a addSessionListener method, but the StandardSession gets created inside of the StandardManager and I'm not sure how I would add a listener to those sessions.

Do I need to provide a custom session Manager? How would I do that? Can I get session activation events from StandardManager any other way? Am I on the wrong path and there is a different solution?

fap
  • 663
  • 1
  • 5
  • 14

0 Answers0