You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by "Hensley, Richard" <Ri...@McKesson.com> on 2004/06/15 23:49:09 UTC

Detecting an Invalidated Session in Tapestry

I need to detect an invalidated session from a Tapestry application. The
purpose is to detect when the container times a session out so clean up work
can be done.

I noticed that AbstractEngine implements the HttpSessionBindingListenter.
Can I simply extend BaseEngine override valueUnbound?

That method would be implemented something like

    public void valueUnbound(HttpSessionBindingEvent arg0) {
        super.valueUnbound(arg0);
        Visit visit = (Visit)getVisit();
        if (visit != null) {
            visit.cleanup();            
        }
    }

I think this would mean that anytime the container discards the session, or
the session is invalidated by Tapestry, this method will get called.

Is this the best way to detect this situation?

According to the JavaDoc for Servlet 2.3

"When an application stores an object in or removes an object from a
session, the session checks whether the object implements
HttpSessionBindingListener. If it does, the servlet notifies the object that
it has been bound to or unbound from the session. Notifications are sent
after the binding methods complete. For session that are invalidated or
expire, notifications are sent after the session has been invalidatd or
expired."

So I think I have the right animal. :)

Richard Hensley


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


valueUnbound() ?

Posted by Laurent Rouvet <la...@roovay.com>.
Does someone can confirm if it works or not with Tomcat 5.x ?


Nikla Ratinen wrote:
> Hi,
> 
> Overriding valueUnbound() works in Jetty but not in 
> Tomcat (4.1.27). The reason is that Tomcat calls valueUnbound()
> of the old listener each time the new listener is set to the HTTP
> session. Tapestry engine gets stored to session after (almost) every
> request even if its already there, therefore resulting in valueUnbound()
> called at the end of each request. Jetty is wise enough to check
> for parameter equality and calls valueUnbound() on old parameter
> only if the new and old values are not equal.
> 
> If anybody knows a better way I would like to hear it, 
> but at least this works:
> 
> ----------- MyEngine.java ---------
> 
> public class MyEngine extends BaseEngine 
> {
>     public static String SESSION_LISTENER = "session_listener";
> 
>     public boolean service(RequestContext context)
>         throws ServletException, IOException
>     {
>         HttpSession session = context.getSession();
>         if (session != null &&
>             session.getAttribute(SESSION_LISTENER) == null)
>         {
>             session.setAttribute(SESSION_LISTENER, 
>                         new SessionEndListener(this));
>         }
>         return super.service(context);
>     }
> }  
> 
> --------- SessionEndListener.java ------------
> 
> public class SessionEndListener
>     implements HttpSessionBindingListener,
>                Serializable
> {
>     private IEngine engine;
>     public SessionEndListener(IEngine engine)
>     {
>         this.engine = engine;
>     }
>     
>     public void valueBound(HttpSessionBindingEvent hsbe)
>     {
>     }
> 
>     public void valueUnbound(HttpSessionBindingEvent hsbe)
>     {
>         if (engine != null)
>         {
>             // Cleanup code here
>         }
>         engine = null;
>     }
> }
> 
> -----------------------------------------------
> 
> 
> Rgds, 
> -- Nikla
> 
> 
> On Wed, 2004-06-16 at 00:49, Hensley, Richard wrote:
> 
>>I need to detect an invalidated session from a Tapestry application. The
>>purpose is to detect when the container times a session out so clean up work
>>can be done.
>>
>>I noticed that AbstractEngine implements the HttpSessionBindingListenter.
>>Can I simply extend BaseEngine override valueUnbound?
>>
>>That method would be implemented something like
>>
>>    public void valueUnbound(HttpSessionBindingEvent arg0) {
>>        super.valueUnbound(arg0);
>>        Visit visit = (Visit)getVisit();
>>        if (visit != null) {
>>            visit.cleanup();            
>>        }
>>    }
>>
>>I think this would mean that anytime the container discards the session, or
>>the session is invalidated by Tapestry, this method will get called.
>>
>>Is this the best way to detect this situation?
>>
>>According to the JavaDoc for Servlet 2.3
>>
>>"When an application stores an object in or removes an object from a
>>session, the session checks whether the object implements
>>HttpSessionBindingListener. If it does, the servlet notifies the object that
>>it has been bound to or unbound from the session. Notifications are sent
>>after the binding methods complete. For session that are invalidated or
>>expire, notifications are sent after the session has been invalidatd or
>>expired."
>>
>>So I think I have the right animal. :)
>>
>>Richard Hensley
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Detecting an Invalidated Session in Tapestry

Posted by Nikla Ratinen <ni...@itmoon.fi>.
Hi,

Overriding valueUnbound() works in Jetty but not in 
Tomcat (4.1.27). The reason is that Tomcat calls valueUnbound()
of the old listener each time the new listener is set to the HTTP
session. Tapestry engine gets stored to session after (almost) every
request even if its already there, therefore resulting in valueUnbound()
called at the end of each request. Jetty is wise enough to check
for parameter equality and calls valueUnbound() on old parameter
only if the new and old values are not equal.

If anybody knows a better way I would like to hear it, 
but at least this works:

----------- MyEngine.java ---------

public class MyEngine extends BaseEngine 
{
    public static String SESSION_LISTENER = "session_listener";

    public boolean service(RequestContext context)
        throws ServletException, IOException
    {
        HttpSession session = context.getSession();
        if (session != null &&
            session.getAttribute(SESSION_LISTENER) == null)
        {
            session.setAttribute(SESSION_LISTENER, 
                        new SessionEndListener(this));
        }
        return super.service(context);
    }
}  

--------- SessionEndListener.java ------------

public class SessionEndListener
    implements HttpSessionBindingListener,
               Serializable
{
    private IEngine engine;
    public SessionEndListener(IEngine engine)
    {
        this.engine = engine;
    }
    
    public void valueBound(HttpSessionBindingEvent hsbe)
    {
    }

    public void valueUnbound(HttpSessionBindingEvent hsbe)
    {
        if (engine != null)
        {
            // Cleanup code here
        }
        engine = null;
    }
}

-----------------------------------------------


Rgds, 
-- Nikla


On Wed, 2004-06-16 at 00:49, Hensley, Richard wrote:
> I need to detect an invalidated session from a Tapestry application. The
> purpose is to detect when the container times a session out so clean up work
> can be done.
> 
> I noticed that AbstractEngine implements the HttpSessionBindingListenter.
> Can I simply extend BaseEngine override valueUnbound?
> 
> That method would be implemented something like
> 
>     public void valueUnbound(HttpSessionBindingEvent arg0) {
>         super.valueUnbound(arg0);
>         Visit visit = (Visit)getVisit();
>         if (visit != null) {
>             visit.cleanup();            
>         }
>     }
> 
> I think this would mean that anytime the container discards the session, or
> the session is invalidated by Tapestry, this method will get called.
> 
> Is this the best way to detect this situation?
> 
> According to the JavaDoc for Servlet 2.3
> 
> "When an application stores an object in or removes an object from a
> session, the session checks whether the object implements
> HttpSessionBindingListener. If it does, the servlet notifies the object that
> it has been bound to or unbound from the session. Notifications are sent
> after the binding methods complete. For session that are invalidated or
> expire, notifications are sent after the session has been invalidatd or
> expired."
> 
> So I think I have the right animal. :)
> 
> Richard Hensley
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
-- 
IT Moon Oy
Nikla Ratinen
Sovellussuunnittelija
mobile +358 40 772 6780
e-mail nikla.ratinen@itmoon.fi


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org