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

Spring Boot Tomcat About 1,854 words

异步任务

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

Views: 1,923 · Posted: 2022-10-04

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

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

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


Today On History
Browsing Refresh