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); }
It will be:
- Schedule
somefunction() to run after a 10 second delay. - HTTP 202 Return Accepted (this is what you should return when the POST endpoint actually creates nothing in place).
- Run
somefunction() after 10 seconds.
source share