Home » Java » java – How to create a request with indefinitely returns in Spring Boot-Exceptionshub

java – How to create a request with indefinitely returns in Spring Boot-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

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.

How to&Answers:

Have you activated @Scheduled in your spring application with @EnableScheduling?