spring定时任务
1.在spring下的task.xml中添加配置
<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:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<task:annotation-driven scheduler="scheduleTask"/>
<task:scheduler id="scheduleTask" pool-size="200"/>
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.**.task" />
<!--<!– 通过指定 javaBean 方式 –>-->
<!--<bean id="myTaskXml" class="com.ybl.test.task.MyTaskXml"/>-->
</beans>
2.新建Java类
package com.ybl.task;
import com.ybl.clickcube.admin.advertiser.mapper.AdvertiserDOMapper;
import com.ybl.clickcube.admin.agency.mapper.AgencyDOMapper;
import com.ybl.clickcube.notification.bean.NotificationDO;
import com.ybl.clickcube.notification.mapper.NotificationDOMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* Created by wangxin on 2018/4/4.
*/
@Component
public class TimerTask {
@Autowired
AdvertiserDOMapper advertiserDOMapper;
@Autowired
NotificationDOMapper notificationDOMapper;
@Autowired
AgencyDOMapper agencyDOMapper;
@Scheduled(cron = "0/5 * * * * ?")//每隔5秒隔行一次
public void notifiTimer() {
System.out.println("开始查询");
List<Integer> advertiserIdList=advertiserDOMapper.selectuserIdListByAccount();
List<Integer> agencyIdList=agencyDOMapper.selectuserIdListByAccount();
advertiserIdList.addAll(agencyIdList);
System.out.println(advertiserIdList);
if(advertiserIdList.size()!=0) {
notificationDOMapper.insertIdIn(advertiserIdList);
}
else{
}
}
}
3.corn解释
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:
Seconds Minutes Hours DayofMonth Month DayofWeek Year或
Seconds Minutes Hours DayofMonth Month DayofWeek
每一个域可出现的字符如下:
Seconds:可出现", - * /"四个字符,有效范围为0-59的整数
Minutes:可出现", - * /"四个字符,有效范围为0-59的整数
Hours:可出现", - * /"四个字符,有效范围为0-23的整数
DayofMonth:可出现", - * / ? L W C"八个字符,有效范围为0-31的整数
Month:可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc
DayofWeek:可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推
Year:可出现", - * /"四个字符,有效范围为1970-2099年