Spring4 Combat (ii)-quartz 2.2 integrated

Source: Internet
Author: User
Tags xmlns log4j

The previous article describes the simple usage of quartz, and spring provides classes to simplify the use of quartz. This article will show you how to integrate quartz in spring. the development tools and technologies involved Spring 4.2.4.RELEASE Quartz 2.2.2 Maven 3 JDK 1.7 Eclipse JUNO project directory structure

The Eclipse Maven Project final directory structure is as follows:
integration Process

Each detail of the integration is explained in detail below
1. Maven Dependency Configuration

<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.ricky.quartz</groupId> <artifactid>int

    Egrity</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>integrity</name> <url>http://maven.apache.org</url> <properties> &L T;quartz.version>2.2.2</quartz.version> <springframework.version>4.2.4.release</ Springframework.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &lt ;/properties> <dependencies> <dependency> <groupid>org.springframework</g Roupid> <artifactid>spring-core</artifactid> <version>${springframework.version}</version> </dependency> <de Pendency> <groupId>org.springframework</groupId> <artifactid>spring-context-s Upport</artifactid> <version>${springframework.version}</version> </dependency&gt

        ; <!--Transaction dependency is required with Quartz integration-<dependency> <group Id>org.springframework</groupid> <artifactId>spring-tx</artifactId> <versi
        On>${springframework.version}</version> </dependency> <!--Quartz Framework-- <dependency> <groupId>org.quartz-scheduler</groupId> <artifactid>quart z</artifactid> <version>${quartz.version}</version> </dependency> &L t;dependency&Gt <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2 .17</version> </dependency> <dependency> <groupid>org.slf4j</groupi
        D> <artifactId>slf4j-log4j12</artifactId> <version>1.7.13</version> </dependency> <dependency> <groupId>junit</groupId> <arti Factid>junit</artifactid> <version>4.12</version> <scope>test</scope > </dependency> </dependencies> <build> <plugins> <plug In> <groupId>org.apache.maven.plugins</groupId> <artifactid>maven-com 
       Piler-plugin</artifactid> <configuration> <source>1.7</source>             <target>1.7</target> </configuration> </plugin> &
 Lt;/plugins> </build> </project>

2. Configure Quartz Job
There are two ways to configure Quartz Job in spring
1) Use Methodinvokingjobdetailfactorybean

<bean id= "Simplejobdetail" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
        <property name= "TargetObject" ref= "Simplebean"/>
        <property name= "Targetmethod" value= " Printmessage "/>
    </bean>

Very simple, just specify a bean and the method that needs to be executed.

2) Use Jobdetailfactorybean

<bean name= "Complexjobdetail"  class= "Org.springframework.scheduling.quartz.JobDetailFactoryBean" >
        <property name= "Jobclass" value= "Com.ricky.quartz.integrity.complex.ComplexScheduledJob"/>
        < Property name= "Jobdatamap" >
            <map>
                <entry key= "Complexbean" value-ref= "Complexbean"/>
            </map>
        </property>
        <property name= "Durability" value= "true"/>
    </bean>

Where Jobclass needs to specify a Quartzjobbean subclass (extends Quartzjobbean), Jobdatamap can pass parameters to the job bean specified by Jobclass.
The following is the specified Quartzjobbean implementation class for Jobclass

Package Com.ricky.quartz.integrity.complex;

Import Org.quartz.JobExecutionContext;
Import org.quartz.JobExecutionException;
Import Org.springframework.scheduling.quartz.QuartzJobBean;

public class Complexscheduledjob extends Quartzjobbean {

    private complexbean Complexbean;

    @Override
    protected void executeinternal (Jobexecutioncontext context)
            throws Jobexecutionexception {

        Complexbean.printmessage ();
    }

    public void Setcomplexbean (Complexbean complexbean) {
        This.complexbean = Complexbean;
    }

}

3, Configuration quartz Trigger
Trigger is to define when scheduler executes your job, there are two ways to configure it
1) Simple Trigger, use Simpletriggerfactorybean configuration

<bean id= "Simpletrigger"  class= "Org.springframework.scheduling.quartz.SimpleTriggerFactoryBean" >
        <property name= "Jobdetail" ref= "Simplejobdetail"/> <property
        name= "Startdelay" value= "$"/>
        <property name= "Repeatinterval" value= "$"/> <property
        name= "RepeatCount" value= "5"/>
    </ Bean>

2) Cron Trigger, using Crontriggerfactorybean configuration

<bean id= "Crontrigger"  class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean" >
        < Property Name= "Jobdetail" ref= "Complexjobdetail"/>
        <property name= "cronexpression" value= "0/5 * * * * *?"/>
    </bean>

4, Configuration Schedulerfactorybean

<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>

        <property name= "configlocation" value= "classpath:quartz.properties"/>  
    </bean>

The Quartz-context.xml is fully configured as follows:

<?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.ricky.quartz.integrity "/> <bean id=" Simplejobdetail "class=" Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean "> <property name=" TargetObject "re f= "Simplebean"/> <property name= "Targetmethod" value= "Printmessage"/> </bean> <bean N Ame= "Complexjobdetail" class= "Org.springframework.scheduling.quartz.JobDetailFactoryBean" > <property name= " JobcLass "value=" Com.ricky.quartz.integrity.complex.ComplexScheduledJob "/> <property name=" Jobdatamap ">
        <map> <entry key= "Complexbean" value-ref= "Complexbean"/> </map> </property> <property name= "Durability" value= "true"/> </bean> <bean id= "Simp Letrigger "class=" Org.springframework.scheduling.quartz.SimpleTriggerFactoryBean "> <property name=" Jobdetai L "ref=" Simplejobdetail "/> <property name=" Startdelay "value=" "/> <property name=" repeat Interval "value=" "/> <property name=" RepeatCount "value=" 5 "/> </bean> <bean id=" Crontrigger "class=" Org.springframework.scheduling.quartz.CronTriggerFactoryBean "> <property name=" Jobdetai


    L "ref=" Complexjobdetail "/> <property name=" cronexpression "value=" 0/5 * * * *? "/> </bean> <bean class= "orG.springframework.scheduling.quartz.schedulerfactorybean "> <property name=" jobdetails "> <l
            ist> <ref bean= "Simplejobdetail"/> <ref bean= "Complexjobdetail"/>
                </list> </property> <property name= "triggers" > <list>
        <ref bean= "Simpletrigger"/> <ref bean= "Crontrigger"/> </list> </property> <property name= "configlocation" value= "classpath:quartz.properties"/> </bean&

Gt </beans>

5. Simple POJO ' s Task Beans

Com.ricky.quartz.integrity.simple.SimpleBean

Package com.ricky.quartz.integrity.simple;

Import java.util.Date;
Import org.springframework.stereotype.Component;

@Component public
class Simplebean {public

    void Printmessage () {
        System.out.println ("Simple task invoked By using Simpletriggerfactorybean "+new Date ());
    }
}

Com.ricky.quartz.integrity.complex.ComplexBean

Package Com.ricky.quartz.integrity.complex;

Import java.util.Date;
Import org.springframework.stereotype.Component;

@Component public
class Complexbean {public

    void Printmessage () {

        System.out.println ("Complex task Invoked by using Crontriggerfactorybean "+new Date ())}
}   

6. Start the Spring container

package com.ricky.quartz.integrity;
Import Org.springframework.context.ApplicationContext;

Import Org.springframework.context.support.ClassPathXmlApplicationContext;

        /** * Spring Quartz Integration Example * */public class App {public static void main (string[] args) {
    ApplicationContext CTX = new Classpathxmlapplicationcontext ("Quartz-context.xml"); }
}

The results of the operation are as follows:
Complex task invoked by using Crontriggerfactorybeantue Jan 19:32:05 CST 2016
Simple task invoked by using Simpletriggerfactorybeantue Jan 19:32:05 CST 2016
Simple task invoked by using Simpletriggerfactorybeantue Jan 19:32:07 CST 2016
Simple task invoked by using Simpletriggerfactorybeantue Jan 19:32:09 CST 2016
Complex task invoked by using Crontriggerfactorybeantue Jan 19:32:10 CST 2016
Simple task invoked by using Simpletriggerfactorybeantue Jan 19:32:11 CST 2016
Simple task invoked by using Simpletriggerfactorybeantue Jan 19:32:13 CST 2016
Complex task invoked by using Crontriggerfactorybeantue Jan 19:32:15 CST 2016
Simple task invoked by using Simpletriggerfactorybeantue Jan 19:32:15 CST 2016
Complex task invoked by using Crontriggerfactorybeantue Jan 19:32:20 CST 2016

Demo source GitHub Address: https://github.com/fbing/spring-quartz–integration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.