Spring MVC異步處理簡介

Spring MVC異步處理簡介

Github地址

相關係列文章:

本文講到的所有特性皆是基於Servlet 3.0 Async Processing的,不是基於Servlet 3.1 Async IO的。

Callable<?>

A Callable<?> can be returned when the application wants to produce the return value asynchronously in a thread managed by Spring MVC.

用於異步返回結果,使用的是Spring MVC的AsyncTaskExecutor,Spring MVC使用CallableMethodReturnValueHandler負責處理它。

下面是例子CallableController

@RestController
public class CallableController {

  @RequestMapping("callable-hello")
  public Callable<String> hello() {
    return () -> new SlowJob("CallableController").doWork();
  }
}

用瀏覽器訪問:http://localhost:8080/callable-hello 查看返回結果。

DeferredResult<?>

A DeferredResult<?> can be returned when the application wants to produce the return value from a thread of its own choosing.

用於異步返回結果,使用的是client code自己的thread,Spring MVC使用DeferredResultMethodReturnValueHandler負責處理它。

下面是例子DeferredResultController

@RestController
public class DeferredResultController {

  @Autowired
  @Qualifier("customExecutorService")
  private ExecutorService executorService;

  @RequestMapping("deferred-result-hello")
  public DeferredResult<String> hello() {
    DeferredResult<String> deferredResult = new DeferredResult<>();
    executorService.submit(() -> {
      try {
        deferredResult.setResult(new SlowJob("DeferredResultController").doWork());
      } catch (Exception e) {
        deferredResult.setErrorResult(e);
      }

    });
    return deferredResult;
  }

}

在這個例子裏使用了ExecutorService(見ExecutorServiceConfiguration),你也可以根據實際情況採用別的機制來給DeferredResult.setResult

用瀏覽器訪問:http://localhost:8080/deferred-result-hello 查看返回結果。

ListenableFuture<?> or CompletableFuture<?>/CompletionStage<?>

A ListenableFuture<?> or CompletableFuture<?>/CompletionStage<?> can be returned when the application wants to produce the value from a thread pool submission.

用於異步返回結果,使用client code自己的thread pool,Spring MVC使用DeferredResultMethodReturnValueHandler負責處理它。

下面是例子ListenableFutureController

@RestController
public class ListenableFutureController {

  @Autowired
  @Qualifier("customExecutorService")
  private ExecutorService executorService;

  @RequestMapping("listenable-future-hello")
  public ListenableFutureTask<String> hello() {

    ListenableFutureTask<String> listenableFutureTask = new ListenableFutureTask<>(
        () -> new SlowJob("ListenableFutureController").doWork());
    executorService.submit(listenableFutureTask);
    return listenableFutureTask;
  }

}

用瀏覽器訪問:http://localhost:8080/listenable-future-hello 查看返回結果。

下面是例子CompletionFutureController

@RestController
public class CompletionFutureController {

  @RequestMapping("completable-future-hello")
  public CompletableFuture<String> hello() {

    return CompletableFuture
        .supplyAsync(() -> new SlowJob("CompletionFutureController").doWork());
  }

}

用瀏覽器訪問:http://localhost:8080/completable-future-hello 查看返回結果。

ResponseBodyEmitter

A ResponseBodyEmitter can be returned to write multiple objects to the response asynchronously; also supported as the body within a ResponseEntity.

用於異步的寫入多個消息,使用的是client code自己的thread,Spring MVC使用ResponseBodyEmitterReturnValueHandler負責處理它。

下面是例子ResponseBodyEmitterController

@RestController
public class ResponseBodyEmitterController {

  @Autowired
  @Qualifier("customExecutorService")
  private ExecutorService executorService;

  @RequestMapping("response-body-emitter-hello")
  public ResponseBodyEmitter hello() {

    ResponseBodyEmitter emitter = new ResponseBodyEmitter();
    executorService.submit(() -> {
      try {
        for (int i = 0; i < 5; i++) {

          String hello = new SlowJob("ResponseBodyEmitterController").doWork();
          emitter.send("Count: " + (i + 1));
          emitter.send("\n");
          emitter.send(hello);
          emitter.send("\n\n");
        }
        emitter.complete();
      } catch (Exception e) {
        emitter.completeWithError(e);
      }

    });

    return emitter;
  }
}

用瀏覽器訪問:http://localhost:8080/response-body-emitter-hello 查看返回結果。

SseEmitter

An SseEmitter can be returned to write Server-Sent Events to the response asynchronously; also supported as the body within a ResponseEntity.

作用和ResponseBodyEmitter類似,也是異步的寫入多個消息,使用的是client code自己的thread,區別在於它使用的是Server-Sent Events。Spring MVC使用ResponseBodyEmitterReturnValueHandler負責處理它。

下面是例子SseEmitterController

@RestController
public class SseEmitterController {

  @Autowired
  @Qualifier("customExecutorService")
  private ExecutorService executorService;

  @RequestMapping("sse-emitter-hello")
  public ResponseBodyEmitter hello() {

    SseEmitter emitter = new SseEmitter();
    executorService.submit(() -> {
      try {
        for (int i = 0; i < 5; i++) {

          String hello = new SlowJob("SseEmitterController").doWork();
          StringBuilder sb = new StringBuilder();
          sb.append("Count: " + (i + 1)).append(". ").append(hello.replace("\n", ""));
          emitter.send(sb.toString());
        }
        emitter.complete();
      } catch (Exception e) {
        emitter.completeWithError(e);
      }

    });

    return emitter;
  }
}

用瀏覽器訪問:http://localhost:8080/sse-emitter-hello 查看返回結果。

StreamingResponseBody

A StreamingResponseBody can be returned to write to the response OutputStream asynchronously; also supported as the body within a ResponseEntity.

用於異步write outputStream,使用的是Spring MVC的AsyncTaskExecutor,Spring MVC使用StreamingResponseBodyReturnValueHandler負責處理它。要注意,Spring MVC並沒有使用Servlet 3.1 Async IO([Read|Write]Listener)。

下面是例子StreamingResponseBodyController

@RestController
public class StreamingResponseBodyController {

  @RequestMapping("streaming-response-body-hello")
  public StreamingResponseBody hello() {

    return outputStream -> {
      String hello = new SlowJob("CallableController").doWork();
      outputStream.write(hello.getBytes());
      outputStream.flush();
    };

  }
}

用瀏覽器訪問:http://localhost:8080/streaming-response-body-hello 查看返回結果。

配置MVC Async

AsyncTaskExecutor

Spring MVC執行異步操作需要用到AsyncTaskExecutor,這個可以在用WebMvcConfigurer.configureAsyncSupport方法來提供(相關文檔)。 如果不提供,則使用SimpleAsyncTaskExecutorSimpleAsyncTaskExecutor不使用thread pool,因此推薦提供自定義的AsyncTaskExecutor

需要注意的是@EnableAsync也需要用到AsyncTaskExecutor,不過Spring MVC和它用的不是同一個。 順帶一提,EnableAsync默認也使用SimpleAsyncTaskExecutor,可以使用AsyncConfigurer.getAsyncExecutor方法來提供一個自定義的AsyncTaskExecutor

例子見:MvcAsyncTaskExecutorConfigurer

Interceptors

  • AsyncHandlerInterceptor,使用WebMvcConfigurer.addInterceptors註冊
  • CallableProcessingInterceptor[Adapter],使用WebMvcConfigurer.configureAsyncSupport註冊
  • DeferredResultProcessingInterceptor[Adapter],使用WebMvcConfigurer.configureAsyncSupport註冊

官方文檔:Intercepting Async Requests

WebAsyncManager

WebAsyncManager是Spring MVC管理async processing的中心類,如果你可以閱讀它的源碼來更多瞭解Spring MVC對於async processing的底層機制。

參考資料

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章