From 4a08fd1352dc2fafb2a51a6fecb5822af7f0811f Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Fri, 11 May 2018 10:07:57 +0300 Subject: [PATCH] update, move deferredresult controller (#4164) --- .../servlets/DeferredResultController.java | 71 ---------------- .../controller/DeferredResultController.java | 85 +++++++++++++++++++ 2 files changed, 85 insertions(+), 71 deletions(-) delete mode 100644 spring-boot/src/main/java/com/baeldung/servlets/DeferredResultController.java create mode 100644 spring-rest/src/main/java/org/baeldung/web/controller/DeferredResultController.java diff --git a/spring-boot/src/main/java/com/baeldung/servlets/DeferredResultController.java b/spring-boot/src/main/java/com/baeldung/servlets/DeferredResultController.java deleted file mode 100644 index c43b0e6dc0..0000000000 --- a/spring-boot/src/main/java/com/baeldung/servlets/DeferredResultController.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.baeldung.servlets; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.context.request.async.DeferredResult; - -@RestController -public class DeferredResultController { - - private final static Logger LOG = LoggerFactory.getLogger(DeferredResultController.class); - - @GetMapping("/async-deferredresult") - public DeferredResult> handleReqDefResult(Model model) { - LOG.info("Received async-deferredresult request"); - DeferredResult> output = new DeferredResult<>(); - new Thread(() -> { - LOG.info("Processing in separate thread"); - output.setResult(ResponseEntity.ok("ok")); - }).start(); - LOG.info("servlet thread freed"); - return output; - } - - public DeferredResult> handleReqWithTimeouts(Model model) { - LOG.info("Received async request with a configured timeout"); - DeferredResult> deferredResult = new DeferredResult<>(500l); - deferredResult.onTimeout(new Runnable() { - @Override - public void run() { - deferredResult.setErrorResult( - ResponseEntity.status(HttpStatus.REQUEST_TIMEOUT).body("Request timeout occurred.")); - } - }); - - new Thread(() -> { - LOG.info("Processing in separate thread"); - try { - Thread.sleep(600l); - deferredResult.setResult(ResponseEntity.ok("ok")); - } catch (InterruptedException e) { - LOG.info("Request processing interrupted"); - deferredResult.setErrorResult(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - .body("INTERNAL_SERVER_ERROR occurred.")); - } - - }).start(); - LOG.info("servlet thread freed"); - return deferredResult; - } - - public DeferredResult> handleAsyncFailedRequest(Model model) { - DeferredResult> deferredResult = new DeferredResult<>(); - new Thread(() -> { - try { - // Exception occurred in processing - throw new Exception(); - } catch (Exception e) { - LOG.info("Request processing failed"); - deferredResult.setErrorResult(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - .body("INTERNAL_SERVER_ERROR occurred.")); - } - }).start(); - return deferredResult; - } - -} diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/DeferredResultController.java b/spring-rest/src/main/java/org/baeldung/web/controller/DeferredResultController.java new file mode 100644 index 0000000000..5d039d2d02 --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/controller/DeferredResultController.java @@ -0,0 +1,85 @@ +package org.baeldung.web.controller; + +import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.context.request.async.DeferredResult; + +@RestController +public class DeferredResultController { + + private final static Logger LOG = LoggerFactory.getLogger(DeferredResultController.class); + + @GetMapping("/async-deferredresult") + public DeferredResult> handleReqDefResult(Model model) { + LOG.info("Received request"); + DeferredResult> deferredResult = new DeferredResult<>(); + + deferredResult.onCompletion(() -> LOG.info("Processing complete")); + + CompletableFuture.supplyAsync(() -> { + LOG.info("Processing in separate thread"); + try { + Thread.sleep(6000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return "OK"; + }) + .whenCompleteAsync((result, exc) -> deferredResult.setResult(ResponseEntity.ok(result))); + + LOG.info("Servlet thread freed"); + return deferredResult; + } + + @GetMapping("/process-blocking") + public ResponseEntity handleReqSync(Model model) { + // ... + return ResponseEntity.ok("ok"); + } + + @GetMapping("/async-deferredresult-timeout") + public DeferredResult> handleReqWithTimeouts(Model model) { + LOG.info("Received async request with a configured timeout"); + DeferredResult> deferredResult = new DeferredResult<>(500l); + deferredResult.onTimeout(() -> deferredResult.setErrorResult(ResponseEntity.status(HttpStatus.REQUEST_TIMEOUT) + .body("Request timeout occurred."))); + + CompletableFuture.supplyAsync(() -> { + LOG.info("Processing in separate thread"); + try { + Thread.sleep(6000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return "error"; + }) + .whenCompleteAsync((result, exc) -> deferredResult.setResult(ResponseEntity.ok(result))); + LOG.info("servlet thread freed"); + return deferredResult; + } + + @GetMapping("/async-deferredresult-error") + public DeferredResult> handleAsyncFailedRequest(Model model) { + LOG.info("Received async request with a configured error handler"); + DeferredResult> deferredResult = new DeferredResult<>(); + deferredResult.onError(new Consumer() { + @Override + public void accept(Throwable t) { + deferredResult.setErrorResult(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("An error occurred.")); + } + + }); + LOG.info("servlet thread freed"); + return deferredResult; + } + +}