如何使 Hazelcast 会话过期

How to expire Hazelcast session

我正在使用 spring-session 库在 Hazelcast 上保持会话,例如:

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();
    }
}
}

我如何使 Hazelcast 上的会话过期在 Hazelcast 管理上会话条目的数量总是递增 ?

您可以将 ttl 添加到地图配置中。因此,非活动会话在超时后会被驱逐。你可以在这里看到一个例子: https://github.com/spring-projects/spring-session/blob/1.0.0.RELEASE/samples/hazelcast/src/main/java/sample/Initializer.java#L59

另外我想,这个示例应用程序就是您想要的。