Thread-Scheduling

线程调度允许用户根据需要随意制定Controller在IO线程上执行还是在Biz线程上执行还是在自定义线程上运行。
使用@Scheduled注解进行线程调度
eg.
在IO线程上执行
@Scheduled(Schedulers.IO)
@GetMapping("/foo")
public String io() {
// ....
return "";
}
在BIz线程上执行
// 在业务线程池中执行
@Scheduled(Schedulers.BIZ)
@GetMapping("/bar")
public String biz() {
// ....
return "";
}
不加注解默认Scheduler上执行
@GetMapping("/baz")
public String bizBatching() {
// ....
return "";
}
自定义Scheduler
将Scheduler实现注入Spring
@Bean
public Scheduler scheduler() {
return Schedulers.fromExecutor("foo", Executors.newCachedThreadPool());
}
在自定义Scheduler上执行
// 在业务线程池中执行
@Scheduled("foo")
@GetMapping("/foo")
public String foo() {
// ....
return "";
}
Tip
- 自定义
Scheduler时请勿使用IO,BIZ,Restlight等作为name(作为Restlight中Scheduler的保留字) - 定义线程池建议使用
RestlightThreadFactory以获得更高的性能
Warning
基于ThreadPoolExecutor类型自定义Scheduler时, 不管是否设置RejectExecutionHandler,Restlight都会覆盖ThreadPoolExecutor中的RejectExecutionHandler, 即不允许用户自定义实现RejectExecutionHandler实现拒绝策略(因为Restlight需要保证每个请求都能被正确的完成,否则可能会导致链接等资源无法被释放等问题), 相反如果自定义实现Scheduler时请保证每个请求都被正确的完成。
配置
所有配置均以restlight.server.scheduling开头
| 配置项 | 默认 | 说明 |
|---|---|---|
| default-scheduler | BIZ | 在不加@Scheduled注解时采用的Scheduler |
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.
Last modified March 10, 2022: introduce docsy as the website framework and add docs-v1.0.0 (#120) (79630ff)