利用Spring进行Quartz定时开发的两种方式进行讲解

利用Spring进行Quartz定时开发的两种方式进行讲解
Spring+Quartz结合方式,大体有两种方式可以达到定时任务功能

1 借助于Spring的org.springframework.scheduling.quartz.JobDetailBean的类功能,继承Spring封装Quartz的org.springframework.scheduling.quartz.QuartzJobBean类,要实现executeInternal方法,并把其中涉及到需要定时任务处理的功能放入其中


Spring配置如下:

1   
2        <bean id="saveProjectJob"
3        class="org.springframework.scheduling.quartz.JobDetailBean">
4        <property name="jobClass">
5        <value>
6        com.gresoft.components.fileupload.service.ParseFileQuartz
7        </value>
8        </property>
9        <property name="jobDataAsMap">
10        <map>
11        <entry key="readXmlService">
12        <ref bean="readXmlService" />
13        </entry>
14        </map>
15        </property>
16        </bean>
17        <bean id="saveCron"
18        class="org.springframework.scheduling.quartz.CronTriggerBean">
19        <property name="jobDetail">
20            <ref local="saveProjectJob" />
21        </property>
22        <property name="cronExpression">
23            <value>0/30 * * * * ? </value>
24        </property>
25    </bean>
26    <bean id="scheduler"
27        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
28        <property name="triggers">
29            <ref local="saveCron" />
30        </property>
31    </bean>
注意上述红色字体 <map>结点,这里主要是为了在定时任务需要使用到Bean,通过Spring给注入进来,如果不写明,就会报
java.lang.NullPointerException错误,主要是因为没有注入Bean


方法代码如下:

1package com.gresoft.components.fileupload.service;
2
3import org.quartz.JobExecutionContext;
4import org.quartz.JobExecutionException;
5import org.springframework.scheduling.quartz.QuartzJobBean;
6
7public class ParseFileQuartz extends QuartzJobBean {
8    private readXmlService readXmlService;
9
10    @Override
11    protected void executeInternal(JobExecutionContext jobexecutioncontext)
12            throws JobExecutionException {
13        // TODO Auto-generated method stub
14        // System.out.println(genderManager.get(1).getGenderName());
15        readXmlService.refreshFileList("D:/tomcat/webapps/sanitation/upload");
16    }
17
18    public void setReadXmlService(readXmlService readXmlService) {
19        this.readXmlService = readXmlService;
20    }
21}
22

2 借助于Spring 的org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean类,使用此方法使开发人员对Quartz完全透明,需要实现定时任务的方法只是一个普通方法

Spring配置文件如下:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
3 <beans>
4
5    <bean id="saveProjectJob"
6        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
7        <property name="targetObject">
8            <ref local="parseQuartz" />
9
10        </property>
11        <property name="targetMethod">
12            <value>execute </value>
13        </property>
14
15
16    </bean>
17    <bean id="parseQuartz" class="com.gresoft.components.fileupload.service.ParseFileQuartzOther">
18        <property name="readXmlService" ref="readXmlService" />
19    </bean>   
20 <bean id="saveCron"
21        class="org.springframework.scheduling.quartz.CronTriggerBean">
22        <property name="jobDetail">
23            <ref local="saveProjectJob" />
24        </property>
25        <property name="cronExpression">
26            <value>0/30 * * * * ? </value>
27        </property>
28    </bean>
29    <bean id="scheduler"
30        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
31        <property name="triggers">
32            <ref local="saveCron" />
33        </property>
34    </bean>
35
业务代码如下:

1package com.gresoft.components.fileupload.service;
2
3public class ParseFileQuartzOther {
4    private readXmlService readXmlService;
5
6
7    public void execute(){
8        // TODO Auto-generated method stub
9        // System.out.println(genderManager.get(1).getGenderName());
10        readXmlService.refreshFileList("D:/tomcat/webapps/sanitation/upload");
11    }
12
13    public void setReadXmlService(readXmlService readXmlService) {
14        this.readXmlService = readXmlService;
15    }
16}
17

以上就是结合Spring使用Quartz编写定时任务两种方式,Spring很好的封装使用Quartz的细节,第一种方式是利用SPring封装的Quartz类进行特定方法的实现,第二种是通过透明的使用Quartz达到定时任务
转:http://www.unix-center.net/bbs/viewthread.php?tid=16839

你可能感兴趣的:(spring,tomcat,bean,PHP,quartz)