---
title: "结合Spring使用Quartz编写定时任务两种方式(转)"
author: "<冫茖>"
date: "2010-01-21T09:10:41Z"
tags: ["Bean", "Spring", "Tomcat", "quartz", "工作"]
categories: ["企业架构"]
slug: "spring-quartz"
draft: false
lastmod: "2010-01-21T09:15:57Z"
id: 576134
url: "https://liuliang136.iteye.com/blog/576134"
---

**１、功能需求**

　　需要每个３０秒对系统指定目录进行扫描，把符合条件的文件进行解析入库工作，这里只提取出有关顶事任务的内容

**２、研究一下Spring+Quartz结合方式，大体有两种方式可以达到定时任务功能**

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

Spring配置如下：  
1     
2        &lt;bean id="saveProjectJob"  
3        class="org.springframework.scheduling.quartz.JobDetailBean"&gt;  
4        &lt;property name="jobClass"&gt;  
5        &lt;value&gt;  
6        com.gresoft.components.fileupload.service.ParseFileQuartz  
7        &lt;/value&gt;  
8        &lt;/property&gt;  
9        &lt;property name="jobDataAsMap"&gt;  
10        &lt;map&gt;  
11        &lt;entry key="readXmlService"&gt;  
12        &lt;ref bean="readXmlService" /&gt;  
13        &lt;/entry&gt;  
14        &lt;/map&gt;  
15        &lt;/property&gt;  
16        &lt;/bean&gt;  
17        &lt;bean id="saveCron"  
18        class="org.springframework.scheduling.quartz.CronTriggerBean"&gt;  
19        &lt;property name="jobDetail"&gt;  
20            &lt;ref local="saveProjectJob" /&gt;  
21        &lt;/property&gt;  
22        &lt;property name="cronexpression\_r"&gt;  
23            &lt;value&gt;0/30 * * * * ?&lt;/value&gt;  
24        &lt;/property&gt;  
25    &lt;/bean&gt;  
26    &lt;bean id="scheduler"  
27        class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt;  
28        &lt;property name="triggers"&gt;  
29            &lt;ref local="saveCron" /&gt;  
30        &lt;/property&gt;  
31    &lt;/bean&gt;

注意上述红色字体&lt;map&gt;结点，这里主要是为了在定时任务需要使用到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.2 借助于Spring 的 org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean 类，使用此方法使开发人员对Quartz完全透明，需要实现定时任务的方法只是一个普通方法

Spring配置文件如下：  
1&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
2&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"&gt;  
3&lt;beans&gt;  
4  
5    &lt;bean id="saveProjectJob"  
6        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"&gt;  
7        &lt;property name="targetObject"&gt;  
8            &lt;ref local="parseQuartz" /&gt;  
9  
10        &lt;/property&gt;  
11        &lt;property name="targetMethod"&gt;  
12            &lt;value&gt;execute&lt;/value&gt;  
13        &lt;/property&gt;  
14  
15  
16    &lt;/bean&gt;  
17    &lt;bean id="parseQuartz" class="com.gresoft.components.fileupload.service.ParseFileQuartzOther"&gt;  
18        &lt;property name="readXmlService" ref="readXmlService" /&gt;  
19    &lt;/bean&gt;     
20&lt;bean id="saveCron"  
21        class="org.springframework.scheduling.quartz.CronTriggerBean"&gt;  
22        &lt;property name="jobDetail"&gt;  
23            &lt;ref local="saveProjectJob" /&gt;  
24        &lt;/property&gt;  
25        &lt;property name="cronexpression\_r"&gt;  
26            &lt;value&gt;0/30 * * * * ?&lt;/value&gt;  
27        &lt;/property&gt;  
28    &lt;/bean&gt;  
29    &lt;bean id="scheduler"  
30        class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt;  
31        &lt;property name="triggers"&gt;  
32            &lt;ref local="saveCron" /&gt;  
33        &lt;/property&gt;  
34    &lt;/bean&gt;  
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集成内容时，有两点需要注意  
１、在&lt;Beans&gt;中不能够设置default-lazy-init="true",否则定时任务不触发，如果不明确指明default-lazy-init的值，默认是false。  
２、在&lt;Beans&gt;中不能够设置default-autowire="byName"的属性，否则后台会报 org.springframework.beans.factory.BeanCreationException错误，这样就不能通过Bean名称自动注入，必须通过明确引用注入。

比如上例中的parseQuartz就是明确声明注入的方式  
1&lt;bean id="parseQuartz" class="com.gresoft.components.fileupload.service.ParseFileQuartzOther"&gt;  
2        &lt;property name="readXmlService" ref="readXmlService" /&gt;  
3    &lt;/bean&gt;

以上就是结合Spring使用Quartz编写定时任务两种方式，Spring很好的封装使用Quartz的细节，第一种方式是利用SPring封装的Quartz类进行特定方法的实现，第二种是通过透明的使用Quartz达到定时任务开发的目的，总体说第二种对开发人员更方便！