0

我正在使用 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 管理上,会话条目的数量一直在增加

4

1 回答 1

1

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

我也想,这个示例应用程序就是你想要的。

于 2015-03-19T23:13:17.220 回答