The integration of spring and quartz

Source: Internet
Author: User

Tag: value string cut executes conf name protect xmlns timing

Recent research on the timing task, a better implementation is to use quartz to achieve. Write a note here. The steps for spring to integrate quartz are for reference only.

I'm using Maven to manage the project, first stick to the dependencies you need.

Configuration of the Pom file in Maven:

<?xml version="1.0"encoding="UTF-8"? ><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>QtzTest</groupId> <artifactId>QtzTest</artifactId> <ver Sion>1.0</version> <properties> <springframework.version>3.0.5.            release</springframework.version> </properties> <dependencies> <dependency>            <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId>            <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-context-support</artifactid > <version>${springframework.version}</version> </dependency> <dependency > <groupId>org.springframework</groupId> &LT;ARTIFACTID&GT;SPRING-TX&LT;/ARTIFACTID&G            T            <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> & Lt;version>${springframework.version}</version> </dependency> <dependency> <groupi D>org.quartz-scheduler</groupid> <artifactId>quartz</artifactId> <version&gt ;1.8.5</version> </dependency> </dependencies> <build> <finalname>${project.ar tifactid}</finalname> <plugins> <plugin> <groupid>org.mortbay.jet Ty</groupid> <artifactId>jetty-maven-plugin</artifactId> <version>7.5.4.v20111024</version> <configuration> <scanIntervalSeconds>Ten</scanIntervalSeconds> <webApp> <contextpath>/${project.artifa ctid}</contextpath> </webApp> </configuration> </plugin&        Gt </plugins> </build></project>


Precautions:

Spring and Quartz The version of the integration should be noted that the following versions of spring3.1 must use QUARTZ1.

There are two ways to implement Quartz in spring: The first task class inherits Quartzjobbean, and the second is defining the task class and the method to be executed in the spring configuration file, classes and methods are just ordinary classes and methods, and the second is highlighted here.

JAVA code for the first method:

Package Com.ncs.hj;  Import Org.quartz.JobExecutionContext;  Import org.quartz.JobExecutionException;    Import Org.springframework.scheduling.quartz.QuartzJobBean;  Public classSpringqtz extends quartzjobbean{Private Static intCounter =0; protected voidexecuteinternal (Jobexecutioncontext context) throws Jobexecutionexception {System. out. println (); Longms =System.currenttimemillis (); System. out. println ("\t\t"+NewDate (ms)); System. out. println (MS); System. out. println ("("+ counter++ +")"); String s= (String) context.getmergedjobdatamap ().Get("Service"); System. out. println (s); System. out. println (); }  }  

Spring configuration file:

<!--method One: Using Jobdetailbean, the task class must implement the job interface--<bean id="Myjob" class="Org.springframework.scheduling.quartz.JobDetailBean"> <property name="name"Value="Examplejob"></property> <property name="Jobclass"Value="Com.ncs.hj.SpringQtz"></property> <property name="Jobdataasmap"> <map> <entry key="Service"><value>simple isThe beat</value></entry> </map> </bean>

The second way of JAVA code:

Package Com.ncs.hj;import Org.quartz.jobexecutioncontext;import org.quartz.jobexecutionexception;import Org.springframework.scheduling.quartz.quartzjobbean;import java.util.Date; Public classSpringqtz {Private Static intCounter =0; protected voidExecute () {Longms =System.currenttimemillis (); System. out. println ("\t\t"+NewDate (ms)); System. out. println ("("+ counter++ +")"); }}

Spring's configuration file:

<?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"Xmlns:tx="Http://www.springframework.org/schema/tx"xmlns:aop="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:mvc="Http://www.springframework.org/schema/mvc"xmlns:p="http://www.springframework.org/schema/p"Xmlns:util="Http://www.springframework.org/schema/util"Xmlns:jdbc="Http://www.springframework.org/schema/jdbc"xsi:schemalocation="Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp//Www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsd"><!--register a custom that class--<Bean id= "springqtzjob" class="Com.ncs.hj.SpringQtz"/>    <!--configuring Jobdetail Job Details--<bean id="Myjobdetail" class="Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!--Injection target object--<property name="TargetObject" ref="Springqtzjob"/> <!--injection target Method--<property Name="Targetmethod"Value="Execute"/> </bean> <!--configuration Trigger--<bean id="Mytrigger" class="Org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!--injection Task details-<property name="Jobdetail" ref="Myjobdetail"/> <!--injects a cron expression that specifies the time of the trigger by this expression--<property name="cronexpression"> <!--run every five seconds-<value>0/5* * * *?</value> </property> </bean> <!--configuration scheduling (scheduler) factory---<bean id="Schedulerfactorybean" class="Org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!--injection trigger--<property name="triggers"> <list> <refbean="Mytrigger"/> </list> </property> </bean></beans>

About the cronexpression expression, here's a little bit of explanation:
Special characters allowed for field allowed values
Seconds 0-59,-*/
Sub 0-59,-*/
Hours 0-23,-*/
Date 1-31,-*? /L W C
Month 1-12 or JAN-DEC,-*/
Week 1-7 or Sun-sat,-*? /L C #
Year (optional) leave blank, 1970-2099,-*/
Meaning of an expression
"0 0 12 * *?" trigger 12 o'clock noon every day.
"0 15 10?" * * "trigger 10:15 every day"
"0 15 10 * *?" Daily 10:15 Trigger
"0 15 10 * *?" * "10:15 per day" trigger
"0 15 10 * *?" 2005 "2005-year daily 10:15 Trigger
"0 * 14 * *?" triggers every 1 minutes from 2 o'clock in the afternoon to 2:59 daily
"0 0/5 14 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily
"0 0/5 14,18 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily and from 6 o'clock in the afternoon to 6:55
"0 0-5 14 * *?" triggers every 1 minutes from 2 o'clock in the afternoon to 2:05 daily
"0 10,44 14?" 3 WED "2:10 and 2:44 triggers in Wednesday of every March
"0 15 10?" * Mon-fri "Monday to Friday 10:15 trigger
"0 15 10 15 *?" 15th 10:15 per month
"0 L *?" 10:15 on the last day of the month
"0 15 10?" * 6L "Last month of Friday 10:15 Trigger
"0 15 10?" * 6L 2002-2005 "2002 to 2005 the last of the monthly Friday 10:15 trigger
"0 15 10?" * 6#3 "Monthly third Friday 10:15 trigger
Every morning at 6.
0 6 * * *
Every two hours
0 */2 * * *
Every two hours between 11 o'clock and 8 in the morning, eight in the morning.
0 23-7/2,8 * * *
Every month, number 4th and Monday to Sunday, three a.m., 11.
0 11 4 * 1-3
January 1 morning, 4.
0 4 1 1 *

<?xml version="1.0"encoding="UTF-8"? ><web-app xmlns="Http://java.sun.com/xml/ns/javaee"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="Http://java.sun.com/xml/ns/javaeehttp//java.sun.com/xml/ns/javaee/web-app_2_5.xsd "version="2.5"> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/web -inf/spring-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener></web-app>

Integration of spring and quartz

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.