Quartz+spring+maven集成实例(解决Job实现类注入bean为空的问题)

环境:

  jdk1.7、eclipse、maven、quartz2.2.1

包的架构:

  技术分享

流程:

  1、创建项目并在pom中加入相应的jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zluo.quartz</groupId>
    <artifactId>quartz-demo</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>quartz-demo Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>3.1.2.RELEASE</spring.version>
        <aspectj.version>1.7.2</aspectj.version>
        <quartz.version>2.2.1</quartz.version>
        <slf4j.version>1.7.5</slf4j.version>
    </properties>

    <dependencies>
        <!-- start spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-asm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <!-- end spring -->

        <!-- start quartz -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>${quartz.version}</version>
        </dependency>
        <!-- end quartz -->

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>quartz-demo</finalName>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.10</version>
                <configuration>
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>8080</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

  2、加入spring、quartz的相应配置文件

#quartz.properies
#===============================================================
# Configure Main Scheduler Properties
#===============================================================
org.quartz.scheduler.instanceName = QuartzScheduler
org.quartz.scheduler.instanceId = AUTO
#===============================================================
# Configure ThreadPool
#===============================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount =  5
org.quartz.threadPool.threadPriority = 5
#===============================================================
# Configure JobStore
#===============================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
<?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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                        http://www.springframework.org/schema/context 
                         http://www.springframework.org/schema/context/spring-context-2.5.xsd
                         http://www.springframework.org/schema/aop
                         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                         http://www.springframework.org/schema/tx
                         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                         http://cxf.apache.org/jaxws
                        http://cxf.apache.org/schemas/jaxws.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.zluo.quartz"/>
    <aop:aspectj-autoproxy/>
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- 定时器-->
    <bean id="jobFactory" class="com.zluo.quartz.factory.MJobFactory"></bean>
     <bean name="scheduleJob"  
        class="org.springframework.scheduling.quartz.JobDetailFactoryBean">  
        <property name="jobClass" value="com.zluo.quartz.job.ScheduleJob"/>
        <property name="durability" value="true"></property>  
    </bean>  
    
    <bean id="scheduleJobTriggers"  
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
        <property name="jobDetail">  
            <ref bean="scheduleJob" />
        </property>  
        <property name="cronExpression">  
            <value>0 0/1 * * * ?</value>  
        </property>  
    </bean>  
    
    <bean id="schedulerFactoryBean"   
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean"  destroy-method="destroy">  
          <property name="jobFactory" ref="jobFactory"/>
        <property name="triggers">  
            <list>  
                <ref bean="scheduleJobTriggers" />
            </list>  
        </property>  
        <property name="applicationContextSchedulerContextKey" value="applicationContext" />  
        <property name="configLocation" value="classpath:quartz.properties" />   
    </bean> 
    
</beans>

  3、开发Job的实现类

package com.zluo.quartz.job;

import javax.annotation.Resource;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.zluo.quartz.service.JobService;

/**
 * 
 * 项目名称:quartz_demo <br> 
 * 类名称:ScheduleJob  <br>
 * 类描述:  <br>
 * 创建人:louzhangjie  <br>
 * 创建时间:2015年5月22日 下午10:01:04  <br>
 * 修改人:louzhangjie <br>
 * 修改时间:2015年5月22日 下午10:01:04  <br>
 * 修改备注:  <br>
 * @version 1.0  <br>
 *
 */
public class ScheduleJob implements Job{

    @Resource
    private JobService jobService;
    
    public void execute(JobExecutionContext context) throws JobExecutionException {
        jobService.work();
    }

}

  4、开发service实体类

package com.zluo.quartz.service;

import org.springframework.stereotype.Service;

@Service
public class JobService {

    public void work(){
        System.out.println("service work");
    }
    
}

  5、扩展spring的factory实现bean的注入(详情:)

package com.zluo.quartz.factory;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;

/**
 * 
 * 项目名称:quartz-demo <br> 
 * 类名称:MJobFactory  <br>
 * 类描述:  用于让spring可以在Job的实现类中进行注入<br>
 * 创建人:louzhangjie  <br>
 * 创建时间:2015年5月22日 下午10:18:12  <br>
 * 修改人:louzhangjie <br>
 * 修改时间:2015年5月22日 下午10:18:12  <br>
 * 修改备注:  <br>
 * @version 1.0  <br>
 *
 */
public class MJobFactory extends AdaptableJobFactory {

    //这个对象Spring会帮我们自动注入进来,也属于Spring技术范畴.
      @Autowired
      private AutowireCapableBeanFactory capableBeanFactory;
      
      protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        //调用父类的方法
        Object jobInstance = super.createJobInstance(bundle);
        //进行注入,这属于Spring的技术,不清楚的可以查看Spring的API.
        capableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
      }

}

  6、使用命令:jetty:run查看测试效果

 

end

good luck

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。