Spring quartz cluster configuration
Quartz is an open-source job scheduling framework. It is fully written in Java and designed for J2SE and J2EE applications. It provides great flexibility without sacrificing simplicity. You can use it to create simple or complex scheduling for executing a job.
A large number of background tasks need to be scheduled and executed in the project, such as indexing, statistical reports, and periodic data synchronization. The task scheduling system is required to have high availability and load balancing features, it is very convenient to use Quartz.
The following describes how to integrate spring and quartz and support cluster deployment. The quartz cluster supports task scheduling through databases.
1. version used: spring 3.1.2 + quartz 1.8.6
2. Import the table used by quartz to the database. In the source code compressed package of quartz, you can find the corresponding SQL file and import it.
3. Configure the database connection pool
Use the datasource configured in spring.
4. Configure quartz. properties
# Configuration file # org. Quartz. schedame. instanceName attribute required for quartz scheduling tasks can be any value, which is used to uniquely identify the instance in JDBC JobStore, but must be the same among all cluster nodes. Org. quartz. schedame. instanceName = instanceScheduler # org. quartz. schedid. instanceId is set to AUTO. instance IDs are generated based on host names and timestamps. Org. quartz. scheduler. instanceId = AUTO org. quartz. threadPool. class = org. quartz. simpl. simpleThreadPool org. quartz. threadPool. threadCount = 10 org. quartz. threadPool. threadPriority = 5 org. quartz. threadPool. threadsInheritContextClassLoaderOfInitializingThread = true org. quartz. jobStore. misfireThreshold = 60000 # org. quartz. jobStore. the class attribute is JobStoreTX, which persists the task to the data. # Because the nodes in the cluster depend on the database to spread the status of the Scheduler instance, you can only apply the Quartz cluster when using JDBC JobStore. # This means you must use JobStoreTX or JobStoreCMT for Job storage; you cannot use RAMJobStore in a cluster. Org. quartz. jobStore. class = org. quartz. impl. jdbcjobstore. jobStoreTX org. quartz. jobStore. driverDelegateClass = org. quartz. impl. jdbcjobstore. stdJDBCDelegate org. quartz. jobStore. tablePrefix = QRTZ _ org. quartz. jobStore. maxMisfiresToHandleAtATime = 10 # org. quartz. jobStore. if the isClustered attribute is true, you tell the Scheduler instance to participate in a cluster. # This attribute runs throughout the scheduling framework and is used to modify the default actions of operations in the cluster environment. The org. quartz. jobStore. isClustered = true # org. quartz. jobStore. clusterCheckinInterval attribute defines the frequency (in milliseconds) when a Scheduler instance is checked into the database ). # Scheduler check if other instances are not checked when they are checked; this indicates a failed Scheduler instance, scheduler takes over any jobs that fail to be executed and can be recovered. # Scheduler updates its status record through the check-in operation. The smaller clusterChedkinInterval, the more frequently Scheduler instances fail to be checked by Scheduler nodes. The default value is 15000 (15 seconds ). Org. quartz. jobStore. clusterCheckinInterval = 20000
5. Configure timerTask. xml
MinuteProcessor
Related timerTask. properties files
background.secondExecute=0/1 * * * * ?background.minuteExecute=0 0/1 * * * ?background.hourExecute=0 0 0/1 * * ?background.dayStartExecute=59 59 23 * * ?background.dayExecute=23 23 4 * * ?background.weekStartExecute=0 0 0 ? * MON
6. compile related classes
1) Compile the IBatchProcessor Interface
public interface IBatchProcessor { void execute( int times);}
2) implementation class MinuteProcessor
public class MinuteProcessor implements IBatchProcessor{ @Override public void execute(int times) { System. out.println(MinuteProcessor times : + times); }}
3) Configure job tasks in two ways: Inherit the QuartzJobBean class and override executeInternal (); Use org. springframework. scheduling. quartz. MethodInvokingJobDetailFactoryBean to specify classes and methods.
Method 1:
Public class MinuteQuartzJob extends QuartzJobBean {private Logger logger = Logger. getLogger (getClass (); private List
TaskExecuters;/*** minute-level counter */private static int minuterCounter = 1; private static long lastMinuterTaskTime =-1; private boolean isWorking; private Lock minuteLock = new ReentrantLock (); public List
GetTaskExecuters () {return taskExecuters;} public void setTaskExecuters (List
TaskExecuters) {this. taskExecuters = taskExecuters;} protected void executeInternal (JobExecutionContext jobCtx) throws JobExecutionException {if (! IsWorking) return; if (minuteLock. tryLock () {try {SchedulerContext schedCtx = jobCtx. getScheduler (). getContext (); ApplicationContext appCtx = (ApplicationContext) schedCtx. get (applicationContext); long currTime = System. currentTimeMillis (); if (lastMinuterTaskTime! =-1) {// a new minute-level task is triggered in less than 55 seconds without executing if (currTime-lastMinuterTaskTime <55000) {logger. warn (the trigger time of a minute-level task is incorrect); return ;}} lastMinuterTaskTime = currTime; for (String taskItem: taskExecuters) {IBatchProcessor proc = (IBatchProcessor) appCtx. getBean (taskItem); proc.exe cute (minuterCounter);} minuterCounter ++;} catch (Exception ex) {ex. printStackTrace ();} finally {minuteLock. unlock () ;}} els E {logger. warn (unable to obtain the minute-level background task lock !!!!!!) ;}} Public boolean getIsWorking () {return isWorking;} public void setIsWorking (boolean isWorking) {this. isWorking = isWorking ;}}
Method 2:
A java. io. NotSerializableException exception is reported when you use the SDK directly. Disgusting! Haha, if this method is implemented, check the solution by yourself (rewrite MethodInvokingJobDetailFactoryBean ).