gpt4 book ai didi

Spring Session - 不调用 SessionDestroyedEvent

转载 作者:可可西里 更新时间:2023-11-01 11:24:34 30 4
gpt4 key购买 nike

我有一个 Spring 应用程序,其中 session 存储在具有短超时 (1m) 的 redis 中。我想在我的 session 超时后调用一个函数,但是 SessionDestroyedEvent @EventListener 没有被调用。

session 监听器.java:

import org.springframework.context.event.EventListener;
import org.springframework.session.events.SessionCreatedEvent;
import org.springframework.session.events.SessionDestroyedEvent;
import org.springframework.stereotype.Component;

@Component
public class SessionListener {

@EventListener
public void sessionCreated(SessionCreatedEvent event) {
System.out.println("created"); // gets called
}

@EventListener
public void sessionDestroyed(SessionDestroyedEvent event) {
System.out.println("destroyed"); // never gets called

}

}

应用程序属性:

spring.session.store-type=redis
server.servlet.session.timeout=1m

注意事项:

  1. 调用 SessionCreatedEvent 上的 eventListener
  2. redis 的 session 在超时后消失

最佳答案

SessionDeletedEvent and SessionExpiredEvent在 Spring Session 引用中描述了如何清理 session 。

来自文档:

Firing SessionDeletedEvent or SessionExpiredEvent is made available through the SessionMessageListener which listens to Redis Keyspace events. In order for this to work, Redis Keyspace events for Generic commands and Expired events needs to be enabled

关于Spring Session - 不调用 SessionDestroyedEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50664376/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com