| 知乎专栏 |
项目中经常会用到计划任务,spring Boot 中通过@EnableScheduling启用计划任务,再通过@Scheduled注解配置计划任务如果运行。
@EnableScheduling
Application.java
启用计划任务, 在Spring Boot启动类加上注解@EnableScheduling,表示该项目启用计划任务
package api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({ "api.config", "api.web", "api.rest", "api.service","api.schedule" })
@EnableMongoRepositories
@EnableJpaRepositories
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
开启计划任务 @EnableScheduling
确保你的计划任务在 @ComponentScan 包中。
# 任务调度线程池大小 默认 1 建议根据任务加大 spring.task.scheduling.pool.size=1 # 调度线程名称前缀 默认 scheduling- spring.task.scheduling.thread-name-prefix=scheduling- # 线程池关闭时等待所有任务完成 spring.task.scheduling.shutdown.await-termination= # 调度线程关闭前最大等待时间,确保最后一定关闭 spring.task.scheduling.shutdown.await-termination-period=
在计划任务方法上加上@Scheduled注解,表示该方法是一个计划任务,项目启动后会去扫描该注解的方法并加入计划任务列表。
package api.schedule;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTasks {
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
public final static long ONE_DAY = 24 * 60 * 60 * 1000;
public final static long ONE_HOUR = 60 * 60 * 1000;
public ScheduledTasks() {
// TODO Auto-generated constructor stub
}
@Scheduled(fixedRate = 5000) //5秒运行一次调度任务
public void echoCurrentTime() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
@Scheduled(fixedRate = ONE_DAY)
public void scheduledTask() {
System.out.println("每隔一天执行一次调度任务");
}
@Scheduled(fixedDelay = ONE_HOUR)
public void scheduleTask2() {
System.out.println("运行完后隔一小时就执行任务");
}
@Scheduled(initialDelay = 1000, fixedRate = 5000)
public void doSomething() {
// something that should execute periodically
}
@Scheduled(cron = "0 0/1 * * * ? ")
public void ScheduledTask3() {
System.out.println(" 每隔一分钟执行一次任务");
}
}
matchIfMissing = true, 如果改属性条目不存在返回 true
@ConditionalOnProperty("batch.metrics.export.influxdb.enabled")
# mybean.enabled = true
@ConditionalOnProperty(value='mybean.enabled')
@ConditionalOnProperty(value = "endpoints.hal.enabled", matchIfMissing = true)
# server.host = localhost
@ConditionalOnProperty(name="server.host", havingValue="localhost")
@ConditionalOnExpression("'${server.host}'=='localhost'")
# spring.rabbitmq.dynamic = true
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic", matchIfMissing = true)
@ConditionalOnProperty(prefix = "extension.security.cors", name = "enabled", matchIfMissing = false)
@ConditionalOnProperty(prefix = "camunda.bpm.job-execution", name = "enabled", havingValue = "true", matchIfMissing = true)
# spring.social.auto-connection-views = true
@ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views")
使用案例
package mis.schedule;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@ConditionalOnProperty("mis.schedule.enabled")
@Component
public class ScheduledTasks {
private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
public final static long ONE_DAY = 24 * 60 * 60 * 1000;
public final static long ONE_HOUR = 60 * 60 * 1000;
public final static long ONE_SECOND = 1000;
public ScheduledTasks() {
// TODO Auto-generated constructor stub
}
@Scheduled(fixedDelay = ONE_SECOND)
public void scheduleTaskSplitLine() {
logger.info("==================== {} ====================", dateFormat.format(new Date()));
}
}
application.properties 配置如下
mis.schedule.enabled=true
@Scheduled参数说明
@Scheduled注解有一些参数,用于配置计划任务执行频率,执行时段等。
cron :cron表达式,e.g. {@code "0 * * * * ?"}从前到后依次表示秒 分 时 日 月 年
zone:设置时区,指明计划任务运行在哪个时区下,默认为空,采用操作系统默认时区
fixedDelay:同一个计划任务两次执行间隔固定时间,单位毫秒,上次执行结束到下次开始执行的时间,以long类型复制
fixedDelayString:同一个计划任务两次执行间隔固定时间,单位毫秒,上次执行结束到下次开始执行的时间,以String类型赋值
fixedRate:以一个固定频率执行,单位毫秒,表示每隔多久执行一次,以long类型赋值
fixedRateString:以一个固定频率执行,单位毫秒,表示每隔多久执行一次,以String类型赋值
initialDelay:延迟启动计划任务,单位毫秒,表示执行第一次计划任务前先延迟一段时间,以long类型赋值
initialDelayString:延迟启动计划任务,单位毫秒,表示执行第一次计划任务前先延迟一段时间,以String赋值
package cn.netkiller.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Service
@EnableScheduling
@Slf4j
public class ScheduleService {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");
@Scheduled(fixedRate = 5000) //5秒运行一次调度任务
public void echoCurrentTime() {
String dateTime = dateTimeFormatter.format(LocalDateTime.now());
System.out.println(dateTime);
log.info("The time is now {}", dateTime);
}
}
cron表达式使用空格分隔的时间元素。
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /
按顺序依次为
秒(0~59)
分钟(0~59)
小时(0~23)
天(月)(0~31,但是你需要考虑你月的天数)
月(0~11)
天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
7.年份(1970-2099)
其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置?.
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午12点
"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
有些子表达式能包含一些范围或列表
例如:子表达式(天(星期))可以为 “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT”
“*”字符代表所有可能的值
因此,“*”在子表达式(月)里表示每个月的含义,“*”在子表达式(天(星期))表示星期的每一天
“/”字符用来指定数值的增量
例如:在子表达式(分钟)里的“0/15”表示从第0分钟开始,每15分钟
在子表达式(分钟)里的“3/20”表示从第3分钟开始,每20分钟(它和“3,23,43”)的含义一样
“?”字符仅被用于天(月)和天(星期)两个子表达式,表示不指定值
当2个子表达式其中之一被指定了值以后,为了避免冲突,需要将另一个子表达式的值设为“?”
“L” 字符仅被用于天(月)和天(星期)两个子表达式,它是单词“last”的缩写
但是它在两个子表达式里的含义是不同的。
在天(月)子表达式中,“L”表示一个月的最后一天
在天(星期)自表达式中,“L”表示一个星期的最后一天,也就是SAT
如果在“L”前有具体的内容,它就具有其他的含义了
例如:“6L”表示这个月的倒数第6天,“FRIL”表示这个月的最一个星期五
注意:在使用“L”参数时,不要指定列表或范围,因为这会导致问题
@Component
@EnableScheduling
public class MyTask {
@Scheduled(cron="*/3 * * * * *")
public void myTaskMethod(){
//do something
}
}
@Scheduled(cron = "0 0 23 * * ?")
private void cleanNewToday() {
long begin = System.currentTimeMillis();
redisTemplate.delete("news:today");
long end = System.currentTimeMillis();
logger.info("Schedule clean redis {} 耗时 {} 秒", "cleanNewFlash()", (end-begin) / 1000 );
}
package cn.netkiller.schedule;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TimerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
System.out.println("task run:" + new Date());
}
};
Timer timer = new Timer();
// 每3秒执行一次
timer.schedule(timerTask, 10, 3000);
}
}
package cn.netkiller.schedule;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorServiceTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
// 参数:执行命令,初始执行的延时时间,任务执行间隔,间隔时间单位
service.scheduleAtFixedRate(() -> System.out.println("ScheduledExecutorService " + new Date()), 0, 3, TimeUnit.SECONDS);
}
}