定时任务
1:启动类添加@EnableScheduling注解
@SpringBootApplication
@EnableScheduling //开启定时器
public class ScheduleApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduleApplication.class, args);
}
}
2:给定时任务方法添加@Scheduled
注意:@Scheduled不能修饰私有方法
@Scheduled(cron = "*/1 * * * * ?") //每秒执行一次
public void refresh(){
System.out.println("定时任务执行了+" + System.currentTimeMillis() / 1000);
}
启动项目
常用cron表达式
表达式 说明
0 0 2 1 * ? * 表示在每月的1日的凌晨2点调整任务
0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作
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触发
Redis管道技术对定时器任务优化
redis工具类中添加方法
public List<Object> refreshWithPipeline(String futureKey,String topicKey,Collection<String> tasks){
List<Object> result = stringRedisTemplate.executePipelined(new RedisCallback<Object>() {
@Nullable
@Override
public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
StringRedisConnection stringRedisConnection = (StringRedisConnection)redisConnection;
String[] allTask = tasks.toArray(new String[tasks.size()]);
stringRedisConnection.lPush(topicKey,allTask);
stringRedisConnection.zRem(futureKey,allTask);
return null;
}
});
return result;
}
//定时器任务
@Scheduled(cron = "*/1 * * * * ?")
public void refresh(){
System.out.println("refesh "+System.currentTimeMillis()/1000);
//从未来数据集合中获取所有的key
Set<String> keys = cacheService.scan(Constants.FUTURE + "*");// future_*
for (String key : keys) {
// key= future_1001_3
String topicKey =Constants.TOPIC+key.split(Constants.FUTURE)[1];
//获取当前key的任务数据集合
Set<String> values = cacheService.zRangeByScore(key, 0,
System.currentTimeMillis());
//将每个任务数据添加到消费者队列并从未来数据集合中删除
if(!values.isEmpty()){
cacheService.refreshWithPipeline(key,topicKey,values);// 改造,只需一句话
System.out.println("成功的将"+key+"定时刷新到"+topicKey);
}
}
}
ThreadPoolTaskScheduler 线程池定时任务的两个常用API
package com.chongba.schedule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.concurrent.ScheduledFuture;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ScheduleApplication.class)
public class DynamicTaskTest {
private final static Logger logger = LoggerFactory.getLogger(DynamicTaskTest.class);
@Autowired
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
@Test
public void testCron() {
String cron = "0 */1 * * * ?";//区别于: String cron = "* */1 * * * ?"
threadPoolTaskScheduler.schedule(new Runnable() {
@Override
public void run() {
System.out.println("schedule time-" + (System.currentTimeMillis() / 1000));
}
}, new CronTrigger(cron));
System.out.println("开始执行schedule"+(System.currentTimeMillis()/1000));
while (true){
}
}
@Test
public void testFixedSchedule(){
ScheduledFuture<?> future = threadPoolTaskScheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("scheduleAtFixedRate time-" + (System.currentTimeMillis() / 1000));
}
}, 1000);
System.out.println("开始执行scheduleAtFixedRate"+(System.currentTimeMillis()/1000));
future.cancel(true);
while (true){
}
}
}