0

I'm using spring-session libs to persist the session on Hazelcast like :

1.

@WebListener
public class HazelcastInitializer implements ServletContextListener {

private HazelcastInstance instance;

@Override
public void contextInitialized(ServletContextEvent sce) {
    String sessionMapName = "spring:session:sessions";
    ServletContext sc = sce.getServletContext();

    ClientConfig clientConfig = new ClientConfig();
    clientConfig.getGroupConfig().setName("nameValue").setPassword("passValue");
    clientConfig.getNetworkConfig().addAddress("ipValue");
    clientConfig.getNetworkConfig().setSmartRouting(true);

    Collection<SerializerConfig> scfg = new ArrayList<SerializerConfig>();
    SerializerConfig serializer = new SerializerConfig()
            .setTypeClass(Object.class)
            .setImplementation(new ObjectStreamSerializer());
    scfg.add(serializer);
    clientConfig.getSerializationConfig().setSerializerConfigs(scfg);

    instance = HazelcastClient.newHazelcastClient(clientConfig);

    Map<String, ExpiringSession> sessions = instance.getMap(sessionMapName);

    SessionRepository<ExpiringSession> sessionRepository
            = new MapSessionRepository(sessions);
    SessionRepositoryFilter<ExpiringSession> filter
            = new SessionRepositoryFilter<ExpiringSession>(sessionRepository);
    Dynamic fr = sc.addFilter("springSessionFilter", filter);
    fr.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
    if (instance != null) {
        instance.shutdown();
    }
}
}

How can i expire the session on Hazelcast ( on Hazelcast Management the number of sessions entries allways incrementing ) ?

1 Answers1

1

You can add ttl to map config. So inactive sessions are evicted after some timeout. You can see an example here: https://github.com/spring-projects/spring-session/blob/1.0.0.RELEASE/samples/hazelcast/src/main/java/sample/Initializer.java#L59

Also i guess, this sample application is what you want.

Bilal Yasar
  • 947
  • 2
  • 11
  • 24
  • 1) In the example i shared i used the object "ClientConfig"; 2) The link you provided its show with "Config" object. 3) How can i set the "MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS" value using the "ClientConfig" object to expire the map session key ? – Amit Ranjitchi Mar 20 '15 at 10:01
  • hmm, you cannot set ttl in client side, you have to make it in your server side, you can put hazelcast.xml config file to your classpath. and hazelcast.xml can have map config. – Bilal Yasar Mar 20 '15 at 11:20
  • i put the following lines: 1 true 1800 And worked! Many tks :D – Amit Ranjitchi Mar 20 '15 at 23:06