Spring Task
导入依赖
在pom.xml中添加spring-boot-starter-web依赖即可,它包含了spring-context,定时任务相关的就属于这个JAR下的org.springframework.scheduling包中
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
定时任务
@Scheduled定时任务的核心
- **cron:**cron表达式,根据表达式循环执行,与fixedRate属性不同的是它是将时间进行了切割。(@Scheduled(cron = “0/5 * * * * *”)任务将在5、10、15、20…这种情况下进行工作)
- fixedRate:每隔多久执行一次,无视工作时间(@Scheduled(fixedRate = 1000)假设第一次工作时间为2018-05-29 16:58:28,工作时长为3秒,那么下次任务的时候就是2018-05-29 16:58:31)
- fixedDelay:当前任务执行完毕后等待多久继续下次任务(@Scheduled(fixedDelay = 3000)假设第一次任务工作时间为2018-05-29 16:54:33,工作时长为5秒,那么下次任务的时间就是2018-05-29 16:54:41)
- initialDelay:第一次执行延迟时间,只是做延迟的设定,与fixedDelay关系密切,配合使用,相辅相成。
@Async代表该任务可以进行异步工作,由原本的串行改为并行
package com.winterchen.task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
/**
* Created by Donghua.Chen on 2018/7/24.
*/
@Component
public class SpringTaskDemo {
private static final Logger log = LoggerFactory.getLogger(SpringTaskDemo.class);
@Async
@Scheduled(cron = "0/1 * * * * *")
public void scheduled1() throws InterruptedException {
Thread.sleep(3000);
log.info("scheduled1 每1秒执行一次:{}", LocalDateTime.now());
}
@Scheduled(fixedRate = 1000)
public void scheduled2() throws InterruptedException {
Thread.sleep(3000);
log.info("scheduled2 每1秒执行一次:{}", LocalDateTime.now());
}
@Scheduled(fixedDelay = 3000)
public void scheduled3() throws InterruptedException {
Thread.sleep(5000);
log.info("scheduled3 上次执行完毕后隔3秒继续执行:{}", LocalDateTime.now());
}
}
主函数
@EnableScheduling注解表示开启对@Scheduled注解的解析;同时new ThreadPoolTaskScheduler()也是相当的关键,通过阅读过源码可以发现默认情况下的private volatile int poolSize = 1;这就导致了多个任务的情况下容易出现竞争情况(多个任务的情况下,如果第一个任务没执行完毕,后续的任务将会进入等待状态)。
@EnableAsync注解表示开启@Async注解的解析;作用就是将串行化的任务给并行化了。(@Scheduled(cron = “0/1 * * * * *”)假设第一次工作时间为2018-05-29 17:30:55,工作周期为3秒;如果不加@Async那么下一次工作时间就是2018-05-29 17:30:59;如果加了@Async下一次工作时间就是2018-05-29 17:30:56)
package com.winterchen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@EnableAsync
@EnableScheduling
@SpringBootApplication
public class SpringBootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootTaskApplication.class, args);
}
/**
* 很关键:默认情况下 TaskScheduler 的 poolSize = 1
*
* @return 线程池
*/
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
return taskScheduler;
}
}
测试
完成准备事项后,启动Chapter15Application,观察日志信息如下
2018-07-24 10:19:24.200 INFO 52194 --- [taskScheduler-1] com.winterchen.task.SpringTaskDemo : scheduled2 每1秒执行一次:2018-07-24T10:19:24.200
2018-07-24 10:19:25.010 INFO 52194 --- [taskScheduler-4] com.winterchen.task.SpringTaskDemo : scheduled1 每1秒执行一次:2018-07-24T10:19:25.010
2018-07-24 10:19:26.008 INFO 52194 --- [taskScheduler-5] com.winterchen.task.SpringTaskDemo : scheduled1 每1秒执行一次:2018-07-24T10:19:26.008
2018-07-24 10:19:26.190 INFO 52194 --- [taskScheduler-2] com.winterchen.task.SpringTaskDemo : scheduled3 上次执行完毕后隔3秒继续执行:2018-07-24T10:19:26.190
2018-07-24 10:19:27.007 INFO 52194 --- [taskScheduler-3] com.winterchen.task.SpringTaskDemo : scheduled1 每1秒执行一次:2018-07-24T10:19:27.007
2018-07-24 10:19:27.202 INFO 52194 --- [taskScheduler-8] com.winterchen.task.SpringTaskDemo : scheduled2 每1秒执行一次:2018-07-24T10:19:27.202
2018-07-24 10:19:28.008 INFO 52194 --- [taskScheduler-7] com.winterchen.task.SpringTaskDemo : scheduled1 每1秒执行一次:2018-07-24T10:19:28.007
2018-07-24 10:19:29.008 INFO 52194 --- [taskScheduler-1] com.winterchen.task.SpringTaskDemo : scheduled1 每1秒执行一次:2018-07-24T10:19:29.008
2018-07-24 10:19:30.007 INFO 52194 --- [taskScheduler-4] com.winterchen.task.SpringTaskDemo : scheduled1 每1秒执行一次:2018-07-24T10:19:30.007
2018-07-24 10:19:30.208 INFO 52194 --- [taskScheduler-8] com.winterchen.task.SpringTaskDemo : scheduled2 每1秒执行一次:2018-07-24T10:19:30.208
2018-07-24 10:19:31.009 INFO 52194 --- [askScheduler-10] com.winterchen.task.SpringTaskDemo : scheduled1 每1秒执行一次:2018-07-24T10:19:31.009
2018-07-24 10:19:32.007 INFO 52194 --- [taskScheduler-2] com.winterchen.task.SpringTaskDemo : scheduled1 每1秒执行一次:2018-07-24T10:19:32.007
2018-07-24 10:19:33.010 INFO 52194 --- [taskScheduler-7] com.winterchen.task.SpringTaskDemo : scheduled1 每1秒执行一次:2018-07-24T10:19:33.010
2018-07-24 10:19:33.213 INFO 52194 --- [taskScheduler-8] com.winterchen.task.SpringTaskDemo : scheduled2 每1秒执行一次:2018-07-24T10:19:33.213
2018-07-24 10:19:34.005 INFO 52194 --- [taskScheduler-6] com.winterchen.task.SpringTaskDemo : scheduled1 每1秒执行一次:2018-07-24T10:19:34.005
2018-07-24 10:19:34.199 INFO 52194 --- [taskScheduler-5] com.winterchen.task.SpringTaskDemo : scheduled3 上次执行完毕后隔3秒继续执行:2018-07-24T10:19:34.199