|
| 1 | +package org.baeldung.web.controller; |
| 2 | + |
| 3 | +import java.util.concurrent.CompletableFuture; |
| 4 | +import java.util.function.Consumer; |
| 5 | + |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | +import org.springframework.http.HttpStatus; |
| 9 | +import org.springframework.http.ResponseEntity; |
| 10 | +import org.springframework.ui.Model; |
| 11 | +import org.springframework.web.bind.annotation.GetMapping; |
| 12 | +import org.springframework.web.bind.annotation.RestController; |
| 13 | +import org.springframework.web.context.request.async.DeferredResult; |
| 14 | + |
| 15 | +@RestController |
| 16 | +public class DeferredResultController { |
| 17 | + |
| 18 | + private final static Logger LOG = LoggerFactory.getLogger(DeferredResultController.class); |
| 19 | + |
| 20 | + @GetMapping("/async-deferredresult") |
| 21 | + public DeferredResult<ResponseEntity<?>> handleReqDefResult(Model model) { |
| 22 | + LOG.info("Received request"); |
| 23 | + DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>(); |
| 24 | + |
| 25 | + deferredResult.onCompletion(() -> LOG.info("Processing complete")); |
| 26 | + |
| 27 | + CompletableFuture.supplyAsync(() -> { |
| 28 | + LOG.info("Processing in separate thread"); |
| 29 | + try { |
| 30 | + Thread.sleep(6000); |
| 31 | + } catch (InterruptedException e) { |
| 32 | + e.printStackTrace(); |
| 33 | + } |
| 34 | + return "OK"; |
| 35 | + }) |
| 36 | + .whenCompleteAsync((result, exc) -> deferredResult.setResult(ResponseEntity.ok(result))); |
| 37 | + |
| 38 | + LOG.info("Servlet thread freed"); |
| 39 | + return deferredResult; |
| 40 | + } |
| 41 | + |
| 42 | + @GetMapping("/process-blocking") |
| 43 | + public ResponseEntity<?> handleReqSync(Model model) { |
| 44 | + // ... |
| 45 | + return ResponseEntity.ok("ok"); |
| 46 | + } |
| 47 | + |
| 48 | + @GetMapping("/async-deferredresult-timeout") |
| 49 | + public DeferredResult<ResponseEntity<?>> handleReqWithTimeouts(Model model) { |
| 50 | + LOG.info("Received async request with a configured timeout"); |
| 51 | + DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>(500l); |
| 52 | + deferredResult.onTimeout(() -> deferredResult.setErrorResult(ResponseEntity.status(HttpStatus.REQUEST_TIMEOUT) |
| 53 | + .body("Request timeout occurred."))); |
| 54 | + |
| 55 | + CompletableFuture.supplyAsync(() -> { |
| 56 | + LOG.info("Processing in separate thread"); |
| 57 | + try { |
| 58 | + Thread.sleep(6000); |
| 59 | + } catch (InterruptedException e) { |
| 60 | + e.printStackTrace(); |
| 61 | + } |
| 62 | + return "error"; |
| 63 | + }) |
| 64 | + .whenCompleteAsync((result, exc) -> deferredResult.setResult(ResponseEntity.ok(result))); |
| 65 | + LOG.info("servlet thread freed"); |
| 66 | + return deferredResult; |
| 67 | + } |
| 68 | + |
| 69 | + @GetMapping("/async-deferredresult-error") |
| 70 | + public DeferredResult<ResponseEntity<?>> handleAsyncFailedRequest(Model model) { |
| 71 | + LOG.info("Received async request with a configured error handler"); |
| 72 | + DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>(); |
| 73 | + deferredResult.onError(new Consumer<Throwable>() { |
| 74 | + @Override |
| 75 | + public void accept(Throwable t) { |
| 76 | + deferredResult.setErrorResult(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 77 | + .body("An error occurred.")); |
| 78 | + } |
| 79 | + |
| 80 | + }); |
| 81 | + LOG.info("servlet thread freed"); |
| 82 | + return deferredResult; |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments