gpt4 book ai didi

java - 如何在 Spring Boot 中创建无限期返回的请求

转载 作者:行者123 更新时间:2023-12-02 08:59:34 26 4
gpt4 key购买 nike

我有一个网络应用程序,其中包含针对用户的通知(例如 facebook、twitter、instagram 通知)。每次某个期限到期时,我都需要返回前端通知,因此我的后端必须每次检查是否有任何期限即将到期或是否已经过期。这就是为什么我想创建一个请求,例如每 5 分钟返回一些内容。

我尝试过 Spring @Schedule 和 WebSocket。

使用时间表:

@Scheduled(fixedDelay = 1000)
@GetMapping(value = "/notification/get", produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<?> returnsNotification() throws InterruptedException {
Thread.sleep(4000);
List<Notification> notifications = seachNotifications();
return new ResponseEntity<>(notifications, HttpStatus.OK);
}

我的问题是,使用此代码我的日程安排不起作用,因此我的前端每次都必须调用请求。

使用 websocket 我还不明白如何使用它。

最佳答案

您是否在 Spring 应用程序中使用 @EnableScheduling 激活了 @Scheduled?

关于java - 如何在 Spring Boot 中创建无限期返回的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60262854/

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