Spring MVC ASyncConfigurer 中的 getAsyncExecutor 和 threadPoolTaskScheduler 有什么区别?

问题描述 投票:0回答:1

有人可以告诉我

getAsyncExecutor
threadPoolTaskScheduler
之间的区别吗,如 spring async-instrumentation 文档中所述? 我创建了下面的配置,如上面的文档中所述。

@Configuration
public class ServiceConfig {
  @Configuration(proxyBeanMethods = false)
  @EnableAsync
  static class AsyncConfig implements AsyncConfigurer {
    @Override
    public Executor getAsyncExecutor() {
      return ContextExecutorService.wrap(
          Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("my-sad-thread-%d").build()), ContextSnapshot::captureAll);
    }
  }


  @Bean(name = "taskExecutor", destroyMethod = "shutdown")
  ThreadPoolTaskScheduler threadPoolTaskScheduler() {
    final ThreadPoolTaskScheduler threadPoolTaskScheduler =
        new ThreadPoolTaskScheduler() {
          @Override
          protected ExecutorService initializeExecutor(
              final ThreadFactory threadFactory,
              final RejectedExecutionHandler rejectedExecutionHandler) {
            final ExecutorService executorService =
                super.initializeExecutor(threadFactory, rejectedExecutionHandler);
            return ContextExecutorService.wrap(executorService, ContextSnapshot::captureAll);
          }

          @Override
          public ScheduledExecutorService getScheduledExecutor() throws IllegalStateException {
            return ContextScheduledExecutorService.wrap(super.getScheduledExecutor());
          }
        };
    threadPoolTaskScheduler.setThreadNamePrefix("SpTaskExecutor1");
    threadPoolTaskScheduler.initialize();
    return threadPoolTaskScheduler;
  }

  @Bean
  public WebMvcConfigurer webMvcConfigurerConfigurer(final AsyncTaskExecutor taskExecutor) {
    return new WebMvcConfigurer() {
      @Override
      public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
        configurer.setTaskExecutor(taskExecutor);
        // Added timeout of 60 seconds for async api due to downloads api having 100K rows.
        configurer.setDefaultTimeout(60 * 1000L /* 60 sec*/);
      }
    };
  }
}

我有一个简单的

Hello World
控制器。

public class HelloWorldController {
    @GetMapping("/hello")
    public ResponseEntity<StreamingResponseBody> sendGreetings() {
        logMsg("Hello Outside Body Thread");
        return ResponseEntity.ok()
            .body(
                outputStream ->
                {
                    logMsg("Hello Inside Body Thread");
                    outputStream.write("Hello World".getBytes());
                }
            );
    }

    @GetMapping("/hello1")
    public ResponseEntity<String> sendGreetings1() {
        logMsg("Hello1");
        return ResponseEntity.ok().body("Hello World");
    }

    private void logMsg(final String msg) {
        log.info("Thread: {} {}", Thread.currentThread().getName(), msg);
    }
}

我正在观察如下日志。

2024-09-02T16:04:56.072+05:30  INFO 68014 --- [nio-8080-exec-5] c.e.h.controller.HelloWorldController    : Thread: http-nio-8080-exec-5 Hello Outside Body Thread
2024-09-02T16:04:56.072+05:30  INFO 68014 --- [pTaskExecutor11] c.e.h.controller.HelloWorldController    : Thread: SpTaskExecutor11 Hello Inside Body Thread

2024-09-02T12:35:17.273+05:30  INFO 21749 --- [nio-8080-exec-4] c.e.h.controller.HelloWorldController    : Thread: http-nio-8080-exec-4 Hello1

threadPoolTaskScheduler
正在使用。
getAsyncExecutor
预计会用在哪里?

示例代码放在这里

spring-boot spring-mvc micrometer spring-micrometer
1个回答
0
投票

getAsyncExecutor()
- 当您调用用
@Async
注释的方法时使用。

ThreadPoolTaskScheduler
- 用于调度任务(如
@Scheduled
注释)或用于异步请求处理,如
StreamingResponseBody

© www.soinside.com 2019 - 2024. All rights reserved.