spring定时任务
所谓定时任务,就是在某个时间设定某个频率,程序自行运行,而不用人工的干预执行相关的任务,比如我们做支付的,每天定时按照一定的时间频率和商户的结算,游戏币充值的定时补发功能,这些都需要定时任务。 .net 之前是通过 windows service 来做的, java 是通过什么机制来实现的呢?本文就来讨论一下这个问题。
1、spring 2.0时代
这个阶段,我们需要借助于Quartz调度器(http://www.opensymphony.com/quartz)
就像这样
<bean id=”croEmailTrigger” class=”org.springframework.scheduling.quartz.CroTriggerBean”> <!—定义触发器-->
<property name=”jobDetail” ref=”dailyEmailJob”> <!—什么任务-->
<property name=”cronExpression” value=”0 0 12 * *?” /><!—何时什么频率-->
</bean>
这里不详细说明了,本实验是基于spring 3.0的。下面详细介绍
2、spring 3.0时代
(1)、定义个模拟任务StatisticServiceImpl.java,这个任务很简单,就打印当前时间出来
package com.figo.onlineshop.service.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Component;
import com.figo.onlineshop.service.StatisticService;
/**
* @author zhuzhifei
* 2012-12-25
*/
@Component("statisticService")
public class StatisticServiceImpl implements StatisticService {
/* (non-Javadoc)
* @see com.figo.onlineshop.service.StatisticService#statistic()
*/
@Override
public String statistic() {
// TODO Auto-generated method stub
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format.format(new Date())+"scheduleTest");
return "scheduleTest";
}
}
(2)、spring配置文件onlineshop-service.xml配置调度器
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-autowire="byName">
<importresource="classpath:META-INF/cxf/cxf.xml"/>
<importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<context:annotation-config/>
<context:component-scanbase-package="com.figo.onlineshop.service.impl"/>
<jaxws:endpointid="receiptOrderService"implementor="com.figo.onlineshop.service.impl.ReceiptOrderServiceImpl"
address="/ReceiptOrderService"/>
<task:schedulerid="scheduler"pool-size="5"/> <!--线程池定义5个线程运行 -->
<task:scheduled-tasksscheduler="scheduler">
<task:scheduledref="statisticService"method="statistic"cron="0/1 * * * * ?"/> <!--每隔1秒执行一次statisticService.statistic任务 -->
</task:scheduled-tasks>
</beans>
把程序部署在tomcat或者jboss等等容器上就可以看到任务在自行运行了。运行结果如下:
2012-12-25 18:03:16scheduleTest
2012-12-25 18:03:17scheduleTest
2012-12-25 18:03:17scheduleTest
2012-12-25 18:03:19scheduleTest
2012-12-25 18:03:20scheduleTest
2012-12-25 18:03:21scheduleTest
2012-12-25 18:03:21scheduleTest
2012-12-25 18:03:23scheduleTest
2012-12-25 18:03:24scheduleTest
2012-12-25 18:03:25scheduleTest
(3)、关于cron正则表达式
Cron表达式被用来配置CronTrigger实例。Cron表达式是一个由7个子表达式组成的字符串。
每个子表达式都描述了一个单独的日程细节。
这些子表达式用空格分隔,分别表示:
1.Seconds秒
2.Minutes分钟
3.Hours小时
4.Day-of-Month月中的天
5.Month月
6.Day-of-Week周中的天
7.Year (optional field)年(可选的域 spring 3.0以上不需要)
*表示任何?表示不设置月份中的日期4和星期中的日期6是相互冲突的,所以这两个设置了其中一个,另外一个应该设置为?
Example CronExpressions——Cron表达式的例子
例1–一个简单的每隔5分钟触发一次的表达式
"0 0/5 * * *?"
例2–在每分钟的10秒后每隔5分钟触发一次的表达式(例如. 10:00:10 am, 10:05:10等.)。
"10 0/5 * * *?"
例3–在每个周三和周五的10:30,11:30,12:30触发的表达式。
"0 30 10-13 ? * WED,FRI"
例4–在每个月的5号,20号的8点和10点之间每隔半个小时触发一次且不包括10点,只是8:30,9:00和9:30的表达式。
"0 0/30 8-9 5,20 * ?"