How to notify session listeners on Session Replication in de.javakaffee.web.msm.MemcachedBackupSessionManager cluster?

284 views
Skip to first unread message

Uday Kumar

unread,
Mar 14, 2016, 1:57:12 PM3/14/16
to memcached-session-manager
Hi Martin,

   We would like to save the session via below code

===========================================================================
public class HttpSessionCollector implements HttpSessionListener {
    private static final Map<String, HttpSession> sessions = new HashMap<String, HttpSession>();
    private static final Logger LOGGER = Logger.getLogger(HttpSessionCollector.class);
    @Override
    public void sessionCreated(HttpSessionEvent event) {
   
        HttpSession session = event.getSession();
        sessions.put(session.getId(), session);
        LOGGER.error("Session id being added in collector: "+session.getId());
    }


    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        sessions.remove(event.getSession().getId());
        LOGGER.error("Session id being removed from collector: "+event.getSession().getId());
    }

    public static HttpSession find(String sessionId) {
        return sessions.get(sessionId);
    }

}
===========================================================================

We have two tomcats which are clusters using the Memcached setup

====================
Below is our setup:
====================

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
     memcachedNodes="n1:<ip address>:<port> n2:<ip address>:<port>"
     requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js|jpeg|png|htm|html|txt)$" />


===================================
The following is the problem we are facing
===================================

    When the session is created in tomcat 1 and the second request reaching to tomcat2, We are going to get the session by JSESSIONID using the HttpSessionCollector 
it is failing, Since the HttpSessionCollector of tomcat2 is not having the session it its sessions map variable.

Is there any way Memcached configuration in-order to notify the listener of other tomcat. I tried many ways but could not help this.

In default DeltaManger cluster : i could see the option notifyListenersOnReplication :

Is there any similar config available with the de.javakaffee.web.msm.MemcachedBackupSessionManager

Please suggest or any custom coding also helps us.

Waiting for your replay.

Thanks,
Udaykumar R




Martin Grotzke

unread,
Mar 15, 2016, 12:42:56 PM3/15/16
to memcached-session-manager

Hi Udaykumar,

at first, for clarification: if you run msm with sticky sessions (which is the case according to the configuration you shared) you must configure your load balancer with sticky sessions as well (and configure the jvmRoute in tomcat). If you don't want to use sticky sessions, configure msm with sticky="false".

Do you (want to) use sticky or non-sticky sessions?

HttpSessionListener.sessionCreated is not invoked in your case because from msm's point of view the session is not created / it's not new, but "just" loaded by another tomcat (due to a tomcat failover for sticky sessions, or as a standard operation for non-sticky sessions).
For a session loaded into tomcat a HttpSessionActivationListener's sessionWillPassivate should be invoked.

Can you explain your use case, why you're going to collect sessions?
Knowing this I should be able to help better.

Cheers,
Martin

--

---
You received this message because you are subscribed to the Google Groups "memcached-session-manager" group.
To unsubscribe from this group and stop receiving emails from it, send an email to memcached-session-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Uday Kumar

unread,
Mar 23, 2016, 12:57:46 AM3/23/16
to memcached-session-manager
Hi Martin,

  We will be using Sticky session. i.e, Same session object in both the tomcat.

 As per your suggestion i configured HttpSessionActivationListener in our web app and configure same in web.xml

Code for the same:

===========================================================

public class HttpSessionCollector2 implements HttpSessionActivationListener{

private static final Map<String, HttpSession> sessions = new HashMap<String, HttpSession>();
private static final Logger LOGGER = Logger.getLogger(HttpSessionCollector2.class);

public void sessionDidActivate(HttpSessionEvent event) {
   
        HttpSession session = event.getSession();
        sessions.put(session.getId(), session);
        LOGGER.error("Session id being created in servlet container2: "+session.getId());
    }

public void sessionWillPassivate(HttpSessionEvent event) {
   sessions.remove(event.getSession().getId());
LOGGER.error("Session id being removed from servlet container2: "+event.getSession().getId());
    }
    public static HttpSession find(String sessionId) {
        return sessions.get(sessionId);
    }

}


Unfortunately It is not being called in either of the server , Not sure why it is not calling.

The use case is : Getting the session by session ID is what we are looking for, In single tomcat instance we were able to process successfully, Where in Clustered environment as i mentioned in earlier post the  HttpSessionListener is invoked only in one of the tomcat.

Please correct what can we do for invoking HttpSessionActivationListener as per your suggestion. 

Please suggest How do we achieve , I mean any alternative.

Waiting for your replay..

Thanks,
Udaykumar R
To unsubscribe from this group and stop receiving emails from it, send an email to memcached-session-manager+unsub...@googlegroups.com.

Martin Grotzke

unread,
Mar 23, 2016, 2:57:12 AM3/23/16
to memcached-session-manager

Have you read docs/servlet spec on HttpSessionActivationListener and how to use it? IIRC it must be part of the session.

On your use case: Getting a session by session ID isn't a use case ;-) Why do you want to do this? Still, to achieve this you can look up sessions in memcached.

You can also search issues and the mailing list, maybe other people asked similar things.

Cheers,
Martin

To unsubscribe from this group and stop receiving emails from it, send an email to memcached-session-...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to the Google Groups "memcached-session-manager" group.
To unsubscribe from this group and stop receiving emails from it, send an email to memcached-session-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages