@RequestMapping(value = "/endpoint", method = RequestMethod.POST)
public ResponseEntity<?> endpoint(@RequestBody final ObjectNode data, final HttpServletRequest request) {
somefunction();
return new ResponseEntity<>(HttpStatus.OK);
}
public somefunction() {
.....
}
In Java spring controller, I have an endpoint. When this endpoint is called, I want it return directly, not wait somefunction()
to finish. Any could teach me how to deal with this?
If you are using Java 8, you can use the new Executor
classes:
@RequestMapping(value = "/endpoint", method = RequestMethod.POST)
public ResponseEntity<?> endpoint(@RequestBody final ObjectNode data, final HttpServletRequest request) {
Executors.newScheduledThreadPool(1).schedule(
() -> somefunction(),
10, TimeUnit.SECONDS
);
return new ResponseEntity<>(HttpStatus.ACCEPTED);
}
This will:
somefunction()
to run after a 10 second delay. somefunction()
after 10 seconds have passed.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With