Spring Non-Blocking Rest "Send and Forget"

I am writing a non-blocking Spring Rest controller. My client must send a request and does not need a response and does not need to wait.

This is my server code:

@RestController @EnableAsync public class testController { @RequestMapping(value = "test", method = RequestMethod.GET) public ResponseEntity<String> test() throws InterruptedException { timeConsumingMethod(); System.out.println("I'm should be first"); return new ResponseEntity<String>("the server is processing your request", HttpStatus.OK); } @Async private void timeConsumingMethod() throws InterruptedException { Thread.sleep(1000*5); System.out.println("I'm should be second!"); } 

However, when I call http: // localhost: 8181 / test using (POSTMAN, Chrome, etc.) in the server log I get the following:

I must be the second!

I must be the first

And only after waiting 5 seconds my browser shows:

the server is processing your request

Is this the right way to β€œSend and Forget” Behavior?

+3
source share
1 answer

According to the doc page in the configuration class, @EnableAsync should be added.

Includes the ability to execute the Spring asynchronous method, similar functionality found in the Spring XML namespace.

Used for the @Configuration classes as follows, where MyAsyncBean is a custom type with one or more methods annotated, either Spring annotation @Async, EJB 3.1 @ javax.ejb.Asynchronous annotation, or any custom annotation specified in the annotation () attribute.

+6
source

Source: https://habr.com/ru/post/1271810/


All Articles