Configuring the basis of regular tasks
The most basic configuration, and this configuration is single-threaded task timed serial execution, which means you can only have a timed tasks you can perform, you can try to declare two methods, the method written in an infinite loop, will card has been found on a task does not move, the other did not execute.
1, start class
Add @EnableScheduling open support for the cron job
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@EnableScheduling
@SpringBootApplication
public
class
TestScheduledApplication
extends
SpringBootServletInitializer {
@Override
protected
SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return
builder.sources(
this
.getClass());
}
public
static
void
main(String[] args) {
new
SpringApplicationBuilder(TestScheduledApplication.
class
).web(
true
).run(args);
}
}
|
2, a task execution timing configuration class
Add this class @Component scan, add annotations @Scheduled declared task on the timing method, configuration time period
1
2
3
4
5
6
7
8
9
10
11
|
@Component
public
class
TestTask1 {
private
static
final
Logger logger = LogManager.getLogger();
// 间隔1秒执行一次
@Scheduled
(cron =
"0/1 * * * * ?"
)
public
void
method1() {
logger.info(
"——————————method1 start——————————"
);
logger.info(
"——————————method1 end——————————"
);
}
}
|
Configure thread pool to perform regular tasks
Because sometimes you need to perform regular tasks a lot, if it is serial execution will bring some problems, such as a very time-consuming task of blocking live, tasks that require short execution cycles will be stuck, so you can configure a thread pool to perform regular tasks in parallel
1, the thread pool configuration
Add @EnableAsync open support for asynchronous
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Configuration
@EnableAsync
public
class
ExecutorConfig {
@Bean
public
Executor executor1() {
ThreadPoolTaskExecutor executor =
new
ThreadPoolTaskExecutor();
executor.setThreadNamePrefix(
"test-schedule2-"
);
executor.setMaxPoolSize(
20
);
executor.setCorePoolSize(
15
);
executor.setQueueCapacity(
0
);
executor.setRejectedExecutionHandler(
new
ThreadPoolExecutor.CallerRunsPolicy());
return
executor;
}
}
|
2, configuring the timing task executed asynchronously
@Async add annotations to indicate that the scheduled task is executed asynchronously, because of the above thread pool configuration name, so you can see the log print is a thread in the thread pool to perform the task, if the thread pool is not configured by default to use the words of SimpleAsyncTaskExecutor this asynchronous execution will open each time a sub-thread execution, performance overhead is relatively large, so it is best to configure their own thread pool
1
2
3
4
5
6
|
@Async
@Scheduled
(cron =
"0/1 * * * * ?"
)
public
void
method1() {
logger.info(
"——————————method1 start——————————"
);
logger.info(
"——————————method1 end——————————"
);
}
|
A plurality of thread pools performs different tasks timings
Because some scheduled task is more important, while others are less important, trying to scheduled tasks are placed in a different thread pool, but also achievable.
1、配置多个线程池
分别配置两个线程池
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
@Configuration
@EnableAsync
public
class
ExecutorConfig1 {
@Bean
public
Executor executor1() {
ThreadPoolTaskExecutor executor =
new
ThreadPoolTaskExecutor();
executor.setThreadNamePrefix(
"test-schedule1-"
);
executor.setMaxPoolSize(
20
);
executor.setCorePoolSize(
15
);
executor.setQueueCapacity(
0
);
executor.setRejectedExecutionHandler(
new
ThreadPoolExecutor.CallerRunsPolicy());
return
executor;
}
@Bean
public
Executor executor2() {
ThreadPoolTaskExecutor executor =
new
ThreadPoolTaskExecutor();
executor.setThreadNamePrefix(
"test-schedule2-"
);
executor.setMaxPoolSize(
20
);
executor.setCorePoolSize(
15
);
executor.setQueueCapacity(
0
);
executor.setRejectedExecutionHandler(
new
ThreadPoolExecutor.CallerRunsPolicy());
return
executor;
}
}
|
2、定时任务显示指定调用线程池
因为上面在配置类里面初始化了两个线程池,所以会有两个线程池分别叫executor1和executor1被生成放到容器中,因为@Bean注解生成的对象默认就是和方法名相同的名字,而@Async注解是可以指定使用哪个线程池的。这样就可以在不同的线程池中执行不同的定时任务了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// 间隔1秒执行一次
@Async
(
"executor1"
)
@Scheduled
(cron =
"0/1 * * * * ?"
)
public
void
method1() {
logger.info(
"——————————method1 start——————————"
);
logger.info(
"——————————method1 end——————————"
);
}
// 间隔1秒执行一次
@Scheduled
(cron =
"0/1 * * * * ?"
)
@Async
(
"executor2"
)
public
void
method2() {
logger.info(
"——————————method2 start——————————"
);
logger.info(
"——————————method2 end——————————"
);
}
|
注意:
- 没有配置自己的线程池时,会默认使用SimpleAsyncTaskExecutor。
- 如果项目中只配置了一个线程池,那么不需要显示指定使用这个线程池,spring也会自动使用用户配置的线程池,但是如果配置了多个就必须要显示指定,否则还是会使用默认的。
- 如果想要指定使用哪个线程池,可以使用@Async("executor2")显示指定。