Not able to capture SessionDestroyed event in Spring Redis Session + Spring boot environment

☆樱花仙子☆ 提交于 2020-01-07 02:58:07

问题


I have a Spring Boot project with Spring Session backed by Redis. I am trying to capture sessionDestroyed event so that I can perform some clean up.

The code to capture the event is as below as per guidance I found elsewhere on Stack Overflow.

 @Component
 public class SessionEndedListener implements     ApplicationListener<SessionDestroyedEvent> {


private final Logger LOGGER = LoggerFactory.getLogger(getClass());

@Override
public void onApplicationEvent(SessionDestroyedEvent event) {

    LOGGER.info("Destroyed session: {}", event.getSessionId()); 

}

}

I have set up my Redis Session Configuration like so

@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 120)
public class RedisSessionConfig {
}

I can see some logs from Spring Redis cleaning up expired sessions every minute as shown below

2016-07-21 11:07:00,026 ==== RedisSessionExpirationPolicy.java ==== thread: pool-4-thread-1 ==== DEBUG > org.springframework.session.data.redis.RedisSessionExpirationPolicy.cleanExpiredSessions() => [] (line: 107) Cleaning up sessions expiring at Thu Jul 21 11:07:00 EDT 2016

But the code that is meant to capture the SessionDestroyedEvent is never called. What am I missing here?

Please note that I am testing this with a local Redis server where there is no problem in configuring key-space-events as Egx.

When I debugged through the code.

This statement

   if(!body.startsWith("spring:session:sessions:")) {
       return;
    }

in the class org.springframework.session.data.redis.SessionMessageListener returns prematurely before getting a chance to publish the event

because the variable body has a value

\xac\xed\x00\x05t\x00<spring:session:sessions:2392443d-62a9-4f8c-81f0-c0bb446eb16f

I am using spring-session 1.0.2.RELEASE. Was there a bug that was fixed?

来源:https://stackoverflow.com/questions/38510187/not-able-to-capture-sessiondestroyed-event-in-spring-redis-session-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!