1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.启动类添加@EnableScheduling //开启任务高度
package com.fy8.fund.api.fund.task;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling //开启任务高度
@SpringBootApplication
public class FundTaskApplication {
public static void main(String[] args) {
SpringApplication.run(FundTaskApplication.class, args);
}
}
3.配置执行定时任务的类,添加@Component扫描本类,在方法上添加@Scheduled注解声明定时任务,配置时间周期
package com.fy8.fund.api.fund.task.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class FundBaseInfoTask{
/**
* 间隔1秒执行一次
*/
@Scheduled(cron = "0/1 * * * * ?")
public void task1() throws InterruptedException {
System.out.println("task1 开始................");
for (int i = 0; i<10; i++){
System.out.println("task1 "+i);
Thread.sleep(1);
}
System.out.println("task1 结束................");
}
/**
* 间隔1秒执行一次
*/
@Scheduled(cron = "0/1 * * * * ?")
public void task2() throws InterruptedException {
System.out.println("task2 开始................");
for (int i = 0; i<10; i++){
System.out.println("task2 "+i);
Thread.sleep(1);
}
System.out.println("task2 结束................");
}
}
输入结果:
task2 开始................
task2 0
task2 1
task2 2
task2 3
task2 4
task2 5
task2 6
task2 7
task2 8
task2 9
task2 结束................
task1 开始................
task1 0
task1 1
task1 2
task1 3
task1 4
task1 5
task1 6
task1 7
task1 8
task1 9
task1 结束................
注:
task1执行完后才会执行task2
原因:
spring默认是以单线程执行任务调度。
spring的定时任务默认最大运行线程数为1,多个任务执行起来时间会有问题,一个很耗时的任务阻塞住了,一些需要短周期循环执行的任务也会卡住。
4.添加@EnableAsync开启对异步的支持,添加@Async注解,表示该定时任务是异步执行的。
说明:
- @EnableAsync开启多线程
- @Async标记其为一个异步任务
- 每个定时任务都是在通过不同的线程来处理,线程名的前缀成了task-
- 线程默认为10个。
package com.fy8.fund.api.fund.task.task;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@EnableAsync //开启对异步的支持
public class FundBaseInfoTask{
/**
* 间隔1秒执行一次
*/
@Async
@Scheduled(cron = "0/1 * * * * ?")
public void task1() throws InterruptedException {
System.out.println("task1 开始................");
System.out.println(Thread.currentThread().getName());
for (int i = 0; i<10; i++){
System.out.println("task1 "+i);
Thread.sleep(1);
}
System.out.println("task1 结束................");
}
/**
* 间隔1秒执行一次
*/
@Async
@Scheduled(cron = "0/1 * * * * ?")
public void task2() throws InterruptedException {
System.out.println("task2 开始................");
System.out.println(Thread.currentThread().getName());
for (int i = 0; i<10; i++){
System.out.println("task2 "+i);
Thread.sleep(1);
}
System.out.println("task2 结束................");
}
}
输出结果:
task1 开始................
task-1
task1 0
task2 开始................
task-2
task2 0
task1 1
task2 1
task1 2
task2 2
task1 3
task2 3
task1 4
task2 4
task1 5
task2 5
task1 6
task2 6
task2 7
task1 7
task1 8
task2 8
task1 9
task2 9
task1 结束................
task2 结束................
5.配置线程池,可配置多个线程池分别执行不同的定时任务
因为有些定时任务是比较重要的,有些则是不太重要的,想把定时任务分别放到不同的线程池中,也是可以实现的。
package com.fy8.fund.api.fund.task.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync //开启对异步的支持
public class ThreadPoolTaskExecutorConfig {
@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;
}
}
6.定时任务显示指定调用线程池
因为上面在配置类里面初始化了两个线程池,所以会有两个线程池分别叫executor1和executor1被生成放到容器中,因为@Bean注解生成的对象默认就是和方法名相同的名字,而@Async注解是可以指定使用哪个线程池的。这样就可以在不同的线程池中执行不同的定时任务了。
package com.fy8.fund.api.fund.task.task;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class FundBaseInfoTask{
/**
* 间隔1秒执行一次
*/
@Async("executor1")
@Scheduled(cron = "0/1 * * * * ?")
public void task1() throws InterruptedException {
System.out.println("task1 开始................");
System.out.println(Thread.currentThread().getName());
for (int i = 0; i<10; i++){
System.out.println("task1 "+i);
Thread.sleep(1);
}
System.out.println("task1 结束................");
}
/**
* 间隔1秒执行一次
*/
@Async("executor2")
@Scheduled(cron = "0/1 * * * * ?")
public void task2() throws InterruptedException {
System.out.println("task2 开始................");
System.out.println(Thread.currentThread().getName());
for (int i = 0; i<10; i++){
System.out.println("task2 "+i);
Thread.sleep(1);
}
System.out.println("task2 结束................");
}
}
输出结果:
task1 开始................
test-schedule1-2
task1 0
task2 开始................
test-schedule2-2
task2 0
task1 1
task2 1
task1 2
task2 2
task1 3
task2 3
task2 4
task1 4
task2 5
task1 5
task1 6
task2 6
task2 7
task1 7
task1 8
task2 8
task2 9
task1 9
task1 结束................
task2 结束................
注意:
- 没有配置自己的线程池时,会默认使用SimpleAsyncTaskExecutor。
参考: