Spring4 + Quartz Scheduler 执行定时任务例子

在spring 3 时代,只能用quartz 1.x 版本,那时候还不支持quartz 2.x 的版本, 进入spring 4时代之后,自然想到是不是可以用 quartx 2.x 版本了,做了一个小小的实验。

我试过用spring4,quartz 1.x 版本,但最近测试,总报有一些类找不到,所以还是用quartz 2.x 以上版本了。



一个小小的maven工程。
程序代码 程序代码

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.websystique.spring</groupId>
  <artifactId>SpringQuartzIntegrationExample</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>
  <name>SpringQuartzIntegrationExample</name>

    <properties>
        <springframework.version>4.0.6.RELEASE</springframework.version>
        <quartz.version>2.2.1</quartz.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <!-- Transaction dependency is required with Quartz integration -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        
        <!-- Quartz framework -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>${quartz.version}</version>
        </dependency>

    </dependencies>
</project>


定时执行任务配置文件
程序代码 程序代码

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.websystique.spring" />


    <!-- For times when you just need to invoke a method on a specific object -->
    <bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myBean" />
        <property name="targetMethod" value="printMessage" />
    </bean>


    <!-- For times when you need more complex processing, passing data to the scheduled job -->
    <bean name="complexJobDetail"     class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.websystique.spring.quartz.ScheduledJob" />
        <property name="jobDataMap">
            <map>
                <entry key="anotherBean" value-ref="anotherBean" />
            </map>
        </property>
        <property name="durability" value="true" />
    </bean>


    <!-- Run the job every 2 seconds with initial delay of 1 second -->
    <bean id="simpleTrigger"  class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="simpleJobDetail" />
        <property name="startDelay" value="1000" />
        <property name="repeatInterval" value="2000" />
    </bean>


    <!-- Run the job every 5 seconds only on weekends -->
    <bean id="cronTrigger"  class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="complexJobDetail" />
        <property name="cronExpression" value="0/5 * * ? * SAT-SUN" />
    </bean>


    <!-- Scheduler factory bean to glue together jobDetails and triggers to Configure Quartz Scheduler -->
    <bean  class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="simpleJobDetail" />
                <ref bean="complexJobDetail" />
            </list>
        </property>

        <property name="triggers">
            <list>
                <ref bean="simpleTrigger" />
                <ref bean="cronTrigger" />
            </list>
        </property>
    </bean>

</beans>


里面配置了两个简单的定时触发时间,一个是 simpleTrigger, 一个是 cronTrigger, 看看配置就基本明白什么意思吧,

直接上代码吧,更直接:

下载文件 spring + quartz sample free download


除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: spring quartz
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.