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

标签 java spring websocket request

我有一个网络应用程序,其中包含针对用户的通知(例如 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/

相关文章:

java - 使用 ffmpeg 降低比特率

Java流从集合中获取单个元素

java - Spring 应用程序的 SSE 响应未到达客户端

c++ - 将 Websocket 与 Poco 库连接

java - 无法将字符串附加到 jtextarea

Java -> LDAP帐号密码加密

java - @Service 是否保证 Spring 中的唯一性?

javascript - Chrome 无法连接到 websocket 服务器(操作码 -1) "handshake was canceled"

ruby-on-rails - 如何在 rails/actioncable 中向除发件人以外的所有客户端发送消息?

java - 通用 : why use "class A <E extends Superclass>" in stead of "class B<Superclass>"?