这个不是重点,就简单的实现一下,至于cron表达式怎么写也不是重点,自行百度即可。
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@EnableScheduling
public class XdxOne {
@Scheduled(cron = "*/1 * * * * ?")
public void testOne(){
System.out.println("one " + " "+ Thread.currentThread().getName());
}
}
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component // 1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class XdxTestOne implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
Runnable task = new Runnable() {
@Override
public void run() {
Thread t = Thread.currentThread();
String name = t.getName();
System.out.println("XdxTestOne=" + name);
}
};
Trigger trigger = new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
// 这里我们可以通过去数据库获取cron表达式,从而实现动态
CronTrigger trigger = new CronTrigger("*/3 * * * * ?");
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
}
};
scheduledTaskRegistrar.addTriggerTask(task, trigger);
}
}
上面两种方式都可以实现定时任务,毫无疑问注解方式实现起来更为舒服。但假如你不想把这个cron表达式写死,对于注解方式我们可能无可奈何(也可能是我没有找到方法)。但是对于接口的方式,我们可以在设置的时候去数据库获取,这样就实现了动态。
接口方式的定时任务每次执行的时候,都会去执行那个获取cron的方法。
上面只是简单的实现了定时任务,一般来说也没有什么问题,但是上面的方式是一个线程挨个执行定时任务,这就会导致比如你某个线程是1s执行一次,但是你另外一个定时任务执行一次需要10min,那么你的这个定时任务就会被阻塞。
一般来说可能也不重要,因为只要最后执行就好了,但有时却是致命的,比我在工作中有一个定时任务是及时去同步数据来处理,这个时候阻塞了就是致命的。因此我们需要开启异步执行,也就是多线程执行,这样你一个定时任务阻塞了,还有另外的线程去执行我们的定时任务。
我们可以使用 @Async 注解实现异步(异步方法使用注解@Async的返回值只能为void或者Future),把@Async加在类或者方法上面,然后还需要一个 @EnableAsync来开启异步。
@EnableAsync可以加在启动类上面这样可以直接开启所有的异步,也可以单独加在每个类上面。
springBoot2.0 和 springBoot2.2 默认的线程池是不一样的,2.0默认线程池每开启一个新的任务都是新开一个线程的。2.2默认线程池有8个。
.
如果你的定时任务比较少,并且执行时间比较短其实上面两种都没啥关系。但是如果定时任务多且慢,那么2.0每次创建一个线程可能导致JVM挂掉,而2.2只有8个线程也会满足不了,会导致任务等待。
创建一个线程池:
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.ThreadPoolExecutor.CallerRunsPolicy;
@Configuration
@EnableAsync
public class AsyncConfig {
@Bean(name = "taskExecutor")
public ThreadPoolTaskExecutor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程数
executor.setCorePoolSize(10);
// 最大线程数
executor.setMaxPoolSize(50);
// 队列最大长度
executor.setQueueCapacity(1000);
// 线程池维护线程所允许的空闲时间
executor.setKeepAliveSeconds(100);
// 线程前缀
executor.setThreadNamePrefix("AsyncExecutorThread-");
// 线程池对拒绝任务(无线程可用)的处理策略
executor.setRejectedExecutionHandler(new CallerRunsPolicy());
executor.initialize();
return executor;
}
}
我们创建了上面的线程池后,所有的任务都将使用这个线程池里面的线程(可以通过输出线程名查看)
但是如果我们有多个线程池的时候,这个时候我们可以使用 @Async(“name”) name就是线程池的名字,没有写name的时候默认使用bean为taskExecutor 的线程池。
而对于使用注解的定时任务,如果你没有使用@Async注解给它指定线程池(可以理解默认name = taskExecutor,如果你没有taskExecutor线程池就会使用默认线程池),它也会使用接口里面创建的线程池进行调用。
@EnableScheduling注解是开启定时任务的,随便放在那里都可以,只需要一个。