Spring实现定时任务方法
标签: springquartztask
2017-03-11 02:02
190人阅读
收藏
举报
版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)[+]
- Quartz
- Spring-Task
从 技术上说实现定时任务常见的方法有三种
- Java自带的java.util.Timer类
- 使用Quartz调度器,这种方式需要第三方jar支持
- Spring3.0以后自带的task定时器
第一种方式在这里就不多做说明了,因为timer类功能比较单一,在实际项目中运用也比较少
Quartz
Quartz的使用方式大致有三种,这三种方式的在这里都会和spring进行整合所以都会用到applicationContext.xml进行配置
- 实现org.quartz.StatefulJob或org.quartz.Job接口
- 继承org.springframework.scheduling.quartz.QuartzJobBean类
- 普通类+xml配置文件
1.实现org.quartz.StatefulJob或org.quartz.Job接口
首先在applicationContext.xml配置定时任务tomcat加载的初始化类,加载时运行init方法
<bean id="StarJob" class="com.lovo.task.server.impl.StarJob" init-method="init">bean>
初始化类代码
package com.lovo.task.server.impl;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class StarJob {
public void init() throws SchedulerException{
TestImpl impl=new TestImpl();
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
try {
impl.startJob(scheduler);
} catch (Throwable e) {
e.printStackTrace();
}
scheduler.start();
}
}
作业及调度实现类
package com.lovo.task.server.impl;
import java.text.ParseException;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.StatefulJob;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
import com.lovo.task.server.ITest;
public class TestImpl implements StatefulJob,ITest {
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
test();
}
@Override
public void startJob(Scheduler scheduler) throws Throwable {
JobDetail JobDetail=new JobDetail("TestImpl",Scheduler.DEFAULT_GROUP, TestImpl.class);
String rex="0/5 * * * * ?";
CronTrigger trigger=new CronTrigger("testTrigger","testGroup", rex);
scheduler.scheduleJob(JobDetail, trigger);
}
public void test(){
System.out.println("测试");
}
}
2.继承org.springframework.scheduling.quartz.QuartzJobBean类
首先在xml中进行配置
<bean id="TestTaskImpl" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.lovo.task.server.impl.TestTaskImpl">property>
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="0">entry>
map>
property>
bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="TestTaskImpl">property>
<property name="startDelay" value="0">property>
<property name="repeatInterval" value="1000">property>
bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
list>
property>
bean>
作业类代码
package com.lovo.task.server.impl;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.lovo.task.server.TestTask;
public class TestTaskImpl extends QuartzJobBean implements TestTask{
private int timeout;
public static int INDEX;
public void setTime(int timeout) {
this.timeout = timeout;
}
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
testTask();
}
@Override
public void testTask() {
INDEX++;
System.out.println("执行计时任务第"+INDEX+"次");
}
}
3.普通类+xml配置文件
首先定义个普通Java类
package com.lovo.task.server;
public class TestTask02 {
public void Task(){
System.out.println("普通类实现定时任务");
}
}
在xml中进行配置
<bean id="Task02" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="com.lovo.task.server.TestTask02">bean>
property>
<property name="targetMethod" value="Task">property>
<property name="concurrent" value="false">property>
bean>
<bean id="simpleTrigger02" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="Task02">property>
<property name="startDelay" value="0">property>
<property name="repeatInterval" value="1000">property>
bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger02" />
list>
property>
bean>
Spring-Task
Spring自带的Task实现定时任务也有两种方式,一种是xml配置的方式,一种是使用注解@Scheduled,不管是那种方式,首先都需要在xml开头声明task
<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
配置定时器开关
<task:scheduler id="Scheduler" pool-size="10"/>
<task:annotation-driven scheduler="Scheduler" proxy-target-class="true"/>
基于注解的任务类
package com.lovo.task.server;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class Task03 {
@Scheduled(cron="0/3 * * * * ?")
public void test(){
System.out.println("task定时任务");
}
}
如果不习惯注解的,追求代码的简洁性,也可以在xml中配置task定时任务
<bean id="xmlTask" class="com.lovo.task.server.Task04">bean>
<task:scheduled-tasks>
<task:scheduled ref="xmlTask" method="test" cron="0/2 * * * * ?"/>
task:scheduled-tasks>
<task:scheduler id="Scheduler" pool-size="10"/>
<task:annotation-driven scheduler="Scheduler" proxy-target-class="true"/>
定时任务
package com.lovo.task.server;
public class Task04 {
public void test(){
System.out.println("xml中配置Task");
}
}
参考来源:
http://gong1208.iteye.com/blog/1773177
cronExpression语法
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /
- 区间
* 通配符
? 你不想设置那个字段
下面只例出几个式子
CRON表达式 含义
“0 0 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分每分钟一次触发
“0 0/5 14 * * ?” 每天从下午2点开始到2:55分结束每5分钟一次触发
“0 0/5 14,18 * * ?” 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
“0 0-5 14 * * ?” 每天14:00至14:05每分钟一次触发
“0 10,44 14 ? 3 WED” 三月的每周三的14:10和14:44触发
“0 15 10 ? * MON-FRI” 每个周一、周二、周三、周四、周五的10:15触发