问题
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