Posted on 2010-01-27 14:47
疯狂 阅读(3203)
评论(1) 编辑 收藏 所属分类:
spring
简单的使用java Timer调度任务
首先写一个定时器任务类
- package com.myspring.quartz;
-
- import java.util.TimerTask;
-
- public class MyTimeTask extends TimerTask {
-
- @Override
- public void run() {
-
- System.out.println("hello");
- }
-
- }
package com.myspring.quartz;
import java.util.TimerTask;
public class MyTimeTask extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("hello");
}
}
spring配置文件
<?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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="timeTask" class="com.myspring.quartz.MyTimeTask"></bean>
- <bean id="myTimeTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
- <property name="timerTask" ref="timeTask"></property>
-
- <property name="period" value="1000"></property>
-
- <property name="delay" value="1000"></property>
- </bean>
- <bean class="org.springframework.scheduling.timer.TimerFactoryBean">
- <property name="scheduledTimerTasks">
- <list>
- <ref bean="myTimeTask"/>
- </list>
- </property>
- </bean>
- </beans>
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="timeTask" class="com.myspring.quartz.MyTimeTask"></bean>
<bean id="myTimeTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="timeTask"></property>
<!-- 指定任务运行周期,单位毫秒 -->
<property name="period" value="1000"></property>
<!-- 指定任务延时时间,即第一次运行之前等待时间,单位毫秒 -->
<property name="delay" value="1000"></property>
</bean>
<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="myTimeTask"/>
</list>
</property>
</bean>
</beans>
局限性:Java Timer无法精确指定何时运行
使用Quartz调度器
同Java Timer一样首先创建一个任务类MyQuartzJob.java
package com.myspring.quartz;
-
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.springframework.scheduling.quartz.*;
- public class MyQuartzJob extends QuartzJobBean{
-
- @Override
- protected void executeInternal(JobExecutionContext arg0)
- throws JobExecutionException {
-
- System.out.println("方法调用");
- }
-
- }
package com.myspring.quartz;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.*;
public class MyQuartzJob extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println("方法调用");
}
}
添加spring配置
<bean id="quartzJob" class="org.springframework.scheduling.quartz.JobDetailBean">
- <property name="jobClass" value="com.myspring.quartz.MyQuartzJob"></property>
-
- <property name="jobDataAsMap">
- <map></map>
- </property>
- </bean>
-
- <bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
- <property name="jobDetail" ref="quartzJob"></property>
-
- <property name="startDelay" value="1000"></property>
-
- <property name="repeatInterval" value="1000"></property>
- </bean>
<bean id="quartzJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.myspring.quartz.MyQuartzJob"></property>
<!-- 接受一个Map,其中包含了需要设置给jobClass的各种属性 -->
<property name="jobDataAsMap">
<map></map>
</property>
</bean>
<bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="quartzJob"></property>
<!-- 设置延迟工作的第一次执行,单位毫秒 -->
<property name="startDelay" value="1000"></property>
<!-- 设置调度任务频度,单位毫秒 -->
<property name="repeatInterval" value="1000"></property>
</bean>
为了精确的控制调度工作,Quartz提供了cron工作
调度一个cron工作
例如,要在每天晚上11:59运行任务
可以如下配置spring配置
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail" ref="quartzJob"></property>
- <property name="cronExpression" value="0 59 23 * * ?"></property></bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="quartzJob"></property>
<property name="cronExpression" value="0 59 23 * * ?"></property></bean>
我的主管写的cronExpression配置说明
最后启动工作,添加spring配置
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
-
- <property name="triggers">
- <list>
- <ref bean="simplerTrigger"/>
- <ref bean="cronTrigger"/>
- </list>
- </property>
- </bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 接受一组触发器,可以接受一个列表 -->
<property name="triggers">
<list>
<ref bean="simplerTrigger"/>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
使用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean类,直接调度任务,而不用继承QuartzJobBean
首先编写一个简单任务类MyQuartz.java
- package com.myspring.quartz;
-
- public class MyQuartz {
- public void hello(){
- System.out.println("hello");
- }
- }
package com.myspring.quartz;
public class MyQuartz {
public void hello(){
System.out.println("hello");
}
}
然后添加spring配置
- <bean id="myQuartz" class="com.myspring.quartz.MyQuartz"></bean>
- <bean id="newJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject" ref="myQuartz"></property>
- <property name="targetMethod" value="hello"></property>
- </bean>
<bean id="myQuartz" class="com.myspring.quartz.MyQuartz"></bean>
<bean id="newJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myQuartz"></property>
<property name="targetMethod" value="hello"></property>
</bean>
最后同理添加spring配置
- <bean id="simplerTrigger2" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
- <property name="jobDetail" ref="newJob"></property>
- <property name="startDelay" value="1000"></property>
- <property name="repeatInterval" value="1000"></property>
- </bean>
- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
-
- <property name="triggers">
- <list>
- <ref bean="simplerTrigger2"/>
- </list>
- </property>
- </bean?