0

I add my object to the session like this:

session.setAttribute("bucket", new ArrayList<ProductDto>());

I would like to know if it is possible to set the time for the life of this object. Ie, for example, after an hour it will be automatically destroyed

3 Answers3

0
  1. If you mean "http servlet session" for "session", then there is no way to configure timeout for individual attribute, no matter is it spring or not. You can only timeout the whole session.

  2. If you use pure spring and servlet web application - configure session timeout in web.xml.

  3. If you mean "spring-boot" for "spring", configure session timeout as written here:

Boris
  • 1,054
  • 1
  • 13
  • 23
0

you can set session time out in your application.properties file

server.session.timeout=1000         # session timeout in second 
server.session.cookie.max-age=1000 # Maximum age of the session cookie in seconds. also add if server.session.timeout not working

refer:https://javadeveloperzone.com/spring-boot/spring-boot-tomcat-session-timeout/

0

You can use cache manager to acheive that:

@EnableCaching
@Configuration
public class CacheConfiguration implements CachingConfigurer {

@Override
public CacheManager cacheManager() {
    ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager() {

        @Override
        protected Cache createConcurrentMapCache(final String name) {
            return new ConcurrentMapCache(name,
                CacheBuilder.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES).maximumSize(100).build().asMap(), false);
        }
    };

    return cacheManager;
}

@Override
public KeyGenerator keyGenerator() {
    return new DefaultKeyGenerator();
}

}
Ajay Negi
  • 322
  • 1
  • 7