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?
source share