I googled and stackoverflowed a lot but cant get this to work. Here is my code. I set a session attribute "topic" in subscribe method but in sessionDestroyed I get it as null. This question on SO seems relevant to mine but doesn't solve the issue.
@Path("/jpubsub/{topic}")
public class JMSSubscribe implements HttpSessionListener, ServletContextListener, MessageListener, ExceptionListener {
@GET
public subscribe(@javax.ws.rs.core.Context HttpServletRequest request) {
HttpSession session = request.getSession();
session.setAttribute("topic", "1");
}
@Override
public void sessionCreated(HttpSessionEvent hse) {
HttpSession session = hse.getSession();
System.out.println("Created Session - ID : " + session.getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent hse) {
System.out.println("Destroyed Session - ID : " + hse.getSession().getId());
System.out.println("Topic ID sessionDestroyed - " + hse.getSession().getAttribute("topic"));
}
Please help.
PS: When I set an attribute in sessionCreated()
, I get it in sessionDestroyed()
.
Is it because I am using diferent session objects? Also, when I print session ID. I get the same session ID in all 3 methods.
Please ask if any other piece of code is required.