Java OpenResty Spring Spring Boot MySQL Redis MongoDB PostgreSQL Linux Android Nginx 面试 小程序 Arthas JVM AQS juc Kubernetes Docker 诊断工具


Spring Boot 配置异步任务、定时任务、Tomcat 的线程池参数

Spring Boot Tomcat 大约 1854 字

异步任务

spring:
  task:
    execution:
      thread-name-prefix: my-execution-
      pool:
        core-size: 8
        max-size: 16
        keep-alive: 60s
        queue-capacity: 100

定时任务

spring:
  task:
    scheduling:
      thread-name-prefix: my-scheduling-
      pool:
        size: 8

Tomcat

不能设置线程名,默认以http-nio-为前缀。

server:
  tomcat:
    accept-count: 100 # queue capacity
    max-connections: 8192 # tcp connection pool size
    keep-alive-timeout: 60s # keep alive
    connection-timeout: 60s # accept time out
    threads:
      min-spare: 10 # core size
      max: 200 # max size

相关代码

@Component
@EnableAsync
public class ExecutionTask {

    @Async
    public void async() {
        System.out.println(Thread.currentThread().getName() + " ExecutionTask 执行时间:" + LocalDateTime.now());
    }

}


@Component
@EnableScheduling
public class ScheduledTask {

    @Scheduled(cron = "5 * * * * ?")
    public void scheduled() throws IOException {
        System.out.println(Thread.currentThread().getName() + " ScheduledTask 执行时间:" + LocalDateTime.now());
    }

}

@RestController
public class HelloController {

    @Resource
    private ExecutionTask executionTask;

    @GetMapping("/")
    public String hello() {
        executionTask.async();
        System.out.println(Thread.currentThread().getName() + " Controller 执行时间:" + LocalDateTime.now());
        return Thread.currentThread().getName() + "-" + LocalDateTime.now();
    }

}

输出

http-nio-8080-exec-1 Controller 执行时间:2022-06-14T22:02:30.532296200
my-execution-1 ExecutionTask 执行时间:2022-06-14T22:02:30.561319500
my-scheduling-1 ScheduledTask 执行时间:2022-06-14T22:03:05.013107200

补充

关于Tomcataccpet-countmax-connection参数的含义可参考之前的文章:

https://www.zhangbj.com/p/1105.html

官方文档

https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.task-execution-and-scheduling

阅读 1270 · 发布于 2022-10-04

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb

扫描下方二维码关注公众号和小程序↓↓↓

扫描二维码关注我
昵称:
随便看看 换一批