①简介
在企业级应用中,经常会制定一些“计划任务”,即在某个时间点做某件事情,核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作。常见的任务调度框架有Quartz和SpringTask等。
②使用
applicationContext-task.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<context:component-scan base-package="com.pb.task"/>
<task:annotation-driven/>
</beans>
SeckillTask.java
注:秒杀定时更新缓存
package com.pb.task;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.pinyougou.mapper.TbSeckillGoodsMapper;
import com.pinyougou.pojo.TbSeckillGoods;
import com.pinyougou.pojo.TbSeckillGoodsExample;
import com.pinyougou.pojo.TbSeckillGoodsExample.Criteria;
@Component
public class SeckillTask {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private TbSeckillGoodsMapper seckillGoodsMapper;
@Scheduled(cron="* * * * * ?")
public void refreshSeckillGoods(){
System.out.println("秒杀缓存更新"+new Date());
List ids=new ArrayList(redisTemplate.boundHashOps("seckillGoods").keys());
TbSeckillGoodsExample example = new TbSeckillGoodsExample();
Criteria criteria = example.createCriteria();
criteria.andStatusEqualTo("1");
criteria.andStockCountGreaterThan(0);
criteria.andStartTimeLessThan(new Date());
criteria.andEndTimeGreaterThan(new Date());
criteria.andIdNotIn(ids);
List<TbSeckillGoods> list = seckillGoodsMapper.selectByExample(example);
for (TbSeckillGoods tbSeckillGoods : list) {
redisTemplate.boundHashOps("seckillGoods").put(tbSeckillGoods.getId()+"", JSON.toJSONString(tbSeckillGoods));
}
System.out.println("秒杀缓存更新"+list.size()+"条");
}
@Scheduled(cron="* * * * * ?")
public void removeSeckillGoods(){
System.out.println("秒杀缓存删除"+new Date());
List<String> seckillStringList = redisTemplate.boundHashOps("seckillGoods").values();
for (String tbSeckillGoodsString : seckillStringList) {
TbSeckillGoods seckillGoods = JSON.parseObject(tbSeckillGoodsString,TbSeckillGoods.class);
if(seckillGoods.getEndTime().getTime()<new Date().getTime()){
seckillGoodsMapper.updateByPrimaryKey(seckillGoods);
redisTemplate.boundHashOps("seckillGoods").delete(seckillGoods.getId()+"");
System.out.println("秒杀商品过期了"+seckillGoods.getId());
}
}
System.out.println("秒杀审核结束");
}
}
Cron表达式(了解)网上有Corn在线表达式,可以直接生成
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:
(1)Seconds Minutes Hours DayofMonth Month DayofWeek Year
(2)Seconds Minutes Hours DayofMonth Month DayofWeek
每一个域可出现的字符如下:
Seconds:可出现", - * /"四个字符,有效范围为0-59的整数
Minutes:可出现", - * /"四个字符,有效范围为0-59的整数
Hours:可出现", - * /"四个字符,有效范围为0-23的整数
DayofMonth:可出现", - * / ? L W C"八个字符,有效范围为1-31的整数
Month:可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc
DayofWeek:可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推
Year:可出现", - * /"四个字符,有效范围为1970-2099年
(1) * 表示匹配该域的任意值
(2) ? 只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和 DayofWeek会相互影响。所以指定日、周几指定一方,另一方就需要设置为?
(3) - 表示范围
(4) / 表示起始时间开始触发,然后每隔固定时间触发一次
(5) , 表示列出枚举值值。
(6) L 表示最后,只能出现在DayofWeek和DayofMonth域
(7)W: 表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。
(8)LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
(9)#:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。
举个例子
"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触发