简体   繁体   中英

How to create a request with indefinitely returns in Spring Boot

I have a web application with notifications for users (like facebook, twitter, instagram notifications). I need to return to my frontend notifications every time some term expires, so my backend has to check every time if there's any term about to expire or if it already expired. That's why I want to create a request that returns something each 5 minutes for example.

I've tried Spring @Schedule and WebSocket.

Using Schedule:

@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);
    }

My problem is that with this code my schedule don't work, so my frontend has to call the request everytime.

Using websocket I don't understand how to work with it yet.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM