简单3步实现在Spring中使用Quartz

王朝学院·作者佚名  2009-12-31
窄屏简体版  字體: |||超大  

直接先看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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="testTimerTask" class="com.test.timerTask.TestTimerTask"></bean>

<bean id="serviceFor" class="com.test.timerTask.Service4Job"></bean>

<!-- 第一步:定义任务调度类,即需要进行业务工作的类.分为两种情况,选择一种来进行调度 -->

<!-- 任务调度类情况1: 使用一个继承自抽象类QuartzJobBean的类来实现任务调度 -->

<bean id="testTimerTaskJob" class="org.springframework.scheduling.quartz.JobDetailBean">

<!-- 注意这里是注入一个类全名,而不是类的引用 -->

<property name="jobClass" value="com.test.timerTask.TestTimerTask"></property>

<!-- 表示向testTimerTask类中注入需要的bean -->

<property name="jobDataAsMap">

<map>

<entry key="service4Job" value-ref="serviceFor"></entry>

</map>

</property>

</bean>

<!-- 任务调度类情况2: 在不需要继承自QuartzJobBean的情况下,直接调度现有类中的业务方法 -->

<bean id="noJobBeanTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

<property name="targetObject" ref="serviceFor"></property>

<property name="targetMethod" value="job"></property>

</bean>

<!-- 第二步:定义触发器,表示啥时候或者隔多久进行调度第一步中定义的那些任务 -->

<!-- 触发器情况1: 基于SimpleTriggerBean任务调用 -->

<bean id="quartzSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">

<property name="jobDetail" ref="testTimerTaskJob"></property>

<property name="repeatInterval" value="3000"></property>

<property name="startDelay" value="2000"></property>

</bean>

<!-- 触发器情况2: 基于SimpleTriggerBean任务调用 -->

<bean id="quartzCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

<property name="jobDetail" ref="noJobBeanTaskJob"></property>

<!-- 这里使用cronExpression的字符串形式.0/3 表示每3秒执行一次. -->

<property name="cronExpression" value="0/3 * * * * ?"></property>

</bean>

<!-- 第三步:开始执行任务调度 -->

<!-- 开始执行任务调度触发器 -->

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<property name="triggers">

<list>

<ref bean="quartzCronTrigger"/>

</list>

</property>

</bean>

</beans>

//===================================================

两个类: TestTimerTask 和 Service4Job

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.springframework.scheduling.quartz.QuartzJobBean;

public class TestTimerTask extends QuartzJobBean{

private Service4Job service4Job;

public void setService4Job(Service4Job service4Job) {

this.service4Job = service4Job;

}

@Override

protected void executeInternal(JobExecutionContext arg0)

throws JobExecutionException {

this.service4Job.job();

}

}

public class Service4Job {

public void job(){

System.out.println("**** "+System.currentTimeMillis());

}

}

(thismonth)转贴:http://yanda20056.blog.163.com/blog/static/5650193120091113115434635/

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/thismonth/archive/2009/12/30/5103969.aspx

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航