Spring中使用Quartz(一)
http://yefriendly.iteye.com/blog/404374
Quartz使用
开发环境:Spring2.0。如果包类没有Quartz包,则需要加入到类路径。
A:extends QuartzJobBean方法
1:定义任务(extends QuartzJobBean)
创建一个Job:
package onlyfun.bb.Quartz;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class QuartzDemo extends QuartzJobBean {
private JobData jobData; //JobData为自定义类
public void executeInternal(JobExecutionContext context){
System.out.println(jobData.getData()+" is executed");
}
public JobData getJobData() {
return jobData;
}
public void setJobData(JobData jobData) {
this.jobData = jobData;
}
}
JobData.java:
package onlyfun.bb.Quartz;
import java.util.Date;
public class JobData {
public String getData(){
return "Data from JobData at "+new Date().toString();
}}
2: 配置
<!-- use Quartz -->
<bean id="someData" class="onlyfun.bb.Quartz.JobData"></bean>
<bean id="jobDetailBean" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="onlyfun.bb.Quartz.QuartzDemo"></property>
<property name="jobDataAsMap">
<map>
<entry key="jobData" value-ref="someData"></entry>
</map>
</property>
</bean>
<bean id="simpleTriggerBean"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="jobDetailBean"></property>
<property name="repeatInterval" value="2000"></property>
<property name="startDelay" value="1000"></property>
</bean>
<bean id="scheduledFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTriggerBean"/>
</list>
</property>
</bean>
注意:在JobDetailBean的jobClass属性必须提供Job的类名称,而不是Job类的实例。Job所需的数据可以在jobDataAsMap属性中设置。
在任务的周期指定上,使用SimpleTriggerBean类来指定,与TimerTask指定方式类似。
排定Job用SchedulerFactoryBean类。
完成设置后,只要在启动Spring并读取定义文件后,计划任务就会执行。
3:测试
package onlyfun.bb.Quartz;
import java.io.*;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestQuartzDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("启动Task:");
System.out.println("请输入 exit 结束Task:");
//A处
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
while(true){
if(reader.readLine().equals("exit")) break; }
//此句可以放在A处
Scheduler scheduler=
(Scheduler)context.getBean("scheduledFactoryBean");
try {
scheduler.shutdown();
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
4:测试分析:
使用SimpleTriggerBean类只能做简单Job与Job之间的执行周期指定,如果需要在指定时间执行,可以使用CronTriggerBean类。
修改配置文件如下:
<!-- use Quartz -->
<bean id="someData" class="onlyfun.bb.Quartz.JobData"></bean>
<bean id="jobDetailBean" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="onlyfun.bb.Quartz.QuartzDemo"></property>
<property name="jobDataAsMap">
<map>
<entry key="jobData" value-ref="someData"></entry>
</map>
</property>
</bean>
<bean id="cronTriggerBean"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobDetailBean"></property>
<property name="cronExpression" value="0 37-38 15 * * ?"/>
</bean>
<bean id="scheduledFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTriggerBean"/>
</list>
</property>
</bean>