Custom code execution before Session expires



Sometime in our application, it is required to execute custom code before session is going to expire or timeout
In such case,Hybirs provides a mechanism to write custom code.

Let’s see how we can do that


We can achieve this by providing a custom sessionCloseStrategy Spring bean in the web Spring context

This strategy class has to either implement the sessionCloseStrategy interface or can extend the DefaultSessionCloseStrategy class

We need to override closeSessionInHttpSession() method of SessionCloseStrategy

Inside this SessionCloseStrategy() method, we can write our custom code


Let’s define this strategy with custom code

Step 1

First create a class which extends DefaultSessionCloseStrategy

  1. public class StorefrontSessionCloseStrategy extends DefaultSessionCloseStrategy
  2. {
  3. @Override
  4.     public void closeSessionInHttpSession(final HttpSession httpSession)
  5.     {
  6.       //Write custom code here
  7.       //We can also use session values here as session is available here
  8.         super.closeSessionInHttpSession(httpSession);
  9.     }
  10. }
public class StorefrontSessionCloseStrategy extends DefaultSessionCloseStrategy
{
@Override
    public void closeSessionInHttpSession(final HttpSession httpSession)
    {
      //Write custom code here
      //We can also use session values here as session is available here
        super.closeSessionInHttpSession(httpSession);
    } 
}

Step 2

Register the above class as a spring bean in the application context

  1. <alias alias="sessionCloseStrategy" name="storefrontSessionCloseStrategy"/>
  2. <bean id="storefrontSessionCloseStrategy " class=“com.kb.storefront.StorefrontSessionCloseStrategy "/>
<alias alias="sessionCloseStrategy" name="storefrontSessionCloseStrategy"/>
<bean id="storefrontSessionCloseStrategy " class=“com.kb.storefront.StorefrontSessionCloseStrategy "/>

Note :

Bean alias must exactly be sessionCloseStrategy in order for the bean to be picked up by hybris
When session is about to expire, above overridden closeSessionInHttpSession() method is called automatically by Hybris.
Session is available inside this method and gets closed immediately after leaving this method

Note :

It is very important to call “super.closeSessionInHttpSession(httpSession)” at the end of the overridden method as this deals with the correct closing and disposal of the Session

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on