Jhon Charles
Jhon Charles

Reputation: 137

Add session destroy event on Spring-Session with Redis

Does anybody know how to add a session destroy event in Spring Session with Redis?, I read that the normal way isn´t possible. I would like to do this because I need to do some cleaning process after a session is destroyed(timeout or logout). I would really appreciate your help, thanks in advance guys.

Upvotes: 1

Views: 3637

Answers (2)

Subin Chalil
Subin Chalil

Reputation: 3660

For spring boot 3+

Change @EnableRedisHttpSession to --->@EnableRedisIndexedHttpSession. This change will configure RedisIndexedSessionRepository over RedisSessionRepository .

By using this RedisIndexedSessionRepository you can now start to listen to SessionCreatedEvent, SessionDeletedEvent, SessionDestroyedEvent and SessionExpiredEvent events.

For detailed explanation visit this. For listening to Session Events visit this.

Upvotes: 0

Vedran Pavic
Vedran Pavic

Reputation: 2389

RedisOperationsSessionRepository will publish org.springframework.session.events.SessionDestroyedEvent (or to be more exact, its subclasses SessionDeletedEvent and SessionExpiredEvent) if it's configured with an ApplicationEventPublisher. This will happen automatically if you're using @EnableRedisHttpSession configuration support.

These events will then in turn be translated into javax.servlet.http.HttpSessionEvent and then forwarded to all HttpSessionListeners, assuming they are registered with your application context as beans.

For more details refer to HttpSessionListener section of Spring Session's reference manual.

Upvotes: 2

Related Questions