spring+ Listener +quartz Cluster (1)--Basic configuration

Source: Internet
Author: User
Tags log log

First of all, the project background, because of business needs, the project has a lot of scheduled tasks, which naturally used quartz this open source product. The quartz used before is primarily memory-based, when the application starts, a timer task is created by the listener, in order to prevent multiple applications from recreating the task, the listener can only be disabled in another Web. XML when it is published. Such a system release becomes cumbersome because the configuration of different applications is different.

In addition to publishing trouble, there is no advantage of clustering, and once the server running the task crashes, other servers in the cluster cannot accept scheduled tasks.

In order to solve the above two problems, the use of quartz cluster in the system, while taking into account the system to create a lot of time tasks, so, in a unified management perspective, set up a unified management of scheduled tasks.

First: Task management list

Editing tasks:

The following step-by-step instructions on how to implement this feature under the Quartz cluster. (The system architecture is springmvc+spring+hibernate)

1, the introduction of the corresponding JAR package

If Maven is used, the quartz dependency is loaded in Pom.xml

<span style= "FONT-SIZE:14PX;" ><dependency>  <groupId>org.quartz-scheduler</groupId>  <artifactid>quartz </artifactId>  <version>2.2.1</version></dependency></span>


or directly introduce the Quartz.2.2.1.jar package and its dependent packages. The need for major this 2.2.1 version requires Spring 3.0.2 or more versions.

2. Introduction of quartz configuration file in spring configuration file

<span style= "FONT-SIZE:14PX;" ><?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ SPRING-BEANS-3.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsd "default-lazy-init=" false "default-autowire=" ByName "><!--clusterdemojob--><bean ID = "Clusterdemojob" class= "Com.cnpc.framework.quartz.ClusterDemoJob"/><!--clustertesterjobdetail-->< Bean id= "Clusterdemojobdetail" class= " Com.test.framework.quartz.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">< Property name= "Concurrent" value= "true"/><property name= "Shouldrecover" value= "true"/><property name= " TargetObject "ref=" clusterdemojob "/><property name"= "Targetmethod" value= "execute"/></bean><!--clusterdemosimpletriggerbean--><bean id= " Clusterdemosimpletriggerbean "class=" Org.springframework.scheduling.quartz.SimpleTriggerBean "><property Name= "Startdelay" value= "10000"/><property name= "Repeatinterval" value= "10000"/><property name= " Jobdetail "ref=" Clusterdemojobdetail "/></bean><!--clusterdemocrontriggerbean--><bean id=" Clusterdemocrontriggerbean "class=" Org.springframework.scheduling.quartz.CronTriggerBean "><property name=" Jobdetail "ref=" Clusterdemojobdetail "/><property name=" cronexpression "value=" 0/10 * * * *? * "/></bean><!--clusterdemoschedulerfactoryb--><bean id=" Schedulerfactorybean "class=" Org.springframework.scheduling.quartz.SchedulerFactoryBean "><property name=" DataSource "><ref bean=" DataSource "/><!--a data source reference that contains all 12 table--></property>< required for the cluster!-- Applicationcontextschedulercontextkey: It's org.springframework.Scheduling.quartz.SchedulerFactoryBean This class stores the spring context in a key/value way in the quartz's upper and lower questions, You can use the key defined by Applicationcontextschedulercontextkey to get the corresponding spring context--><!--<property name= " Applicationcontextschedulercontextkey "value=" Applicationcontextkey "/>--><property name=" configLocation "Value=" classpath:quartz.properties "/><!--autostartup: Indicates whether the schedule starts automatically with the project, if False indicates that it does not start automatically, You need to call Scheduler.start () to start--><property name= "Autostartup" value= "true"/><property name= "triggers" > <list><ref bean= "Clusterdemosimpletriggerbean"/> <ref bean= "Clusterdemocrontriggerbean"/></ List></property></bean></beans></span>

In this XML can be configured some tasks, here can say trigger is a trigger, job is a task, trigger is divided into relatively simple simpletrigger, can define the execution times, interval and other tasks, as well as more complex use of time expression Crontrigger. A trigger can only correspond to one job, and a job may have multiple trigger. The job above has two trigger corresponding to each other. Where the Clusterdemojob code is as follows:

<span style= "FONT-SIZE:14PX;" >package Com.test.framework.quartz.job;import java.io.Serializable; Import Org.apache.commons.logging.log;import Org.apache.commons.logging.logfactory;import com.test.framework.utils.dateutil;/** * Clusterdemojob. *  * @author *  */public class Clusterdemojob implements Serializable {/** * log */private static log log = Logfacto Ry.getlog (clusterdemojob.class);/** * Execute. *  * @throws interruptedexception */public void execute () {System.out.println ("Hello World-------Start"); Log.debug (Dateutil.getcurrdatetimestr () + "Cluster Demo execute begin!"); /thread.sleep (+); Log.debug ("Cluster demo execute end!"); System.out.println ("Hello World-------End");}} </span>

In the actual configuration, most of the tasks may be complex, the time expression to be based on a certain logical dynamic formation, so in the XML configuration is a few relatively fixed tasks. These tasks are serialized into the database when the system is started (if the cluster is configured). In my project, our scheduling task is more complex, and second, in order to make the memory-based task scheduling changes to the task of cluster-based tasks to make the least changes, so the listener initialization of the scheduling method. In fact, the configuration of XML is this:

<span style= "FONT-SIZE:14PX;" ><?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ SPRING-BEANS-3.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsd "default-lazy-init=" false "default-autowire=" ByName "><bean id=" Schedulerfactorybean " class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" ><property name= "DataSource" ><ref Bean= "DataSource"/><!--data source reference, including all 12 tables required for the cluster--></property><property name= "Configlocation" Value= "Classpath:quartz.properties"/></BEAN></BEANS></SPAN> 
<span style= "FONT-SIZE:14PX;" > </span>

Where DataSource is a uniformly configured data source in spring:

<span style= "FONT-SIZE:14PX;" ><context:property-placeholder location= "classpath:jdbc.properties"/><!--data source--><bean id= " DataSource "class=" Org.apache.commons.dbcp.BasicDataSource "destroy-method=" Close "><property name=" Driverclassname "value=" ${datasource.driverclassname} "/><property name=" url "value=" ${dataSource.url} "/> <property name= "username" value= "${datasource.username}"/><property name= "password" value= "${ Datasource.password} "/><property name=" InitialSize "value=" ${datasource.initialsize} "/><property name = "Maxactive" value= "${datasource.maxactive}"/><property name= "Maxidle" value= "${datasource.maxidle}"/> <property name= "Minidle" value= "${datasource.minidle}"/></bean></span>


3. Quartz.properties Configuration

By looking at the source code of the quartz, we have included the Quartz.properties configuration file, but the default is memory-based configuration, if you want to support the cluster, you need the following configuration:

#============================================================================# Configure Main Scheduler Properties  #============================================================================org.quartz.scheduler.instancename = DefaultQuartzSchedulerorg.quartz.scheduler.instanceId = AUTOorg.quartz.scheduler.rmi.export = Falseorg.quartz.scheduler.rmi.proxy = Falseorg.quartz.scheduler.wrapJobExecutionInUserTransaction = false#======== ====================================================================# Configure ThreadPool #===================== =======================================================org.quartz.threadpool.class = Org.quartz.simpl.SimpleThreadPoolorg.quartz.threadPool.threadCount = 10org.quartz.threadpool.threadpriority = 5org.quartz.threadpool.threadsinheritcontextclassloaderofinitializingthread = true#============================= ===============================================# Configure Jobstore # project uses Oralce database #============================= ===============================================org.quartz.jobstore.class = Org.quartz.impl.jdbcjobstore.JobStoreTXorg.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.oracle.oracledelegate#org.quartz.jobstore.driverdelegateclass= Org.quartz.impl.jdbcjobstore.StdJDBCDelegate Org.quartz.jobStore.misfireThreshold = 60000org.quartz.jobstore.useproperties = Falseorg.quartz.jobStore.tablePrefix = qrtz_# Org.quartz.jobStore.dataSource = myDSorg.quartz.jobStore.isClustered = true# through the check-in operation, Scheduler also updates its own status record. The smaller the clusterchedkininterval, the more frequently the Scheduler node checks for Scheduler instances that fail. The default value is 15000 (that is, 15 seconds). Org.quartz.jobStore.clusterCheckinInterval = 15000#============================================================= ===============# Configure datasource#========================================================================== = = #org. Quartz.dataSource.myDS.driver = Oracle.jdbc.driver.oracledriver#org.quartz.datasource.myds.url = Jdbc:o Racle:thin: @localhost: 1521:orcl#org.quartz.datasource.myds.user = Users#org.quartz.dataSource.myDS.password = Admin#org.quartz.datasource.myds.maxconnections = 10 

Because the cluster is going to serialize the task into the database, you need to use 11 tables, and the next article will attach these 11 tables to create SQL.

spring+ Listener +quartz Cluster (1)--Basic configuration

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.