Spring and Quartz integration details

First of all, there is no need to talk about all the dependencies. First, download the Quazrt release package, unzip it, and run the tables_mysql_innodb.sql script in the doc folder (E:\Quartz\quartz-2.2.3\docs\dbTables) to create a
new job
/ /MyJob is a normal job, this one does not need to implement org.quartz.Job
package job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyJob {
    Logger log = LoggerFactory.getLogger(MyJob.class);
    public void work () {
        // This task only prints logs for debugging and observation
    	log.info("==================:"+this.getClass().getName() + " was just triggered...");
    }  
}

//MyQuartzJob, this job needs to implement org.quartz.Job because we want to persist the job to RDB
package job;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyQuartzJob implements Job{
	Logger log = LoggerFactory.getLogger(MyQuartzJob.class);
	@Override
	public void execute(JobExecutionContext jobexecutioncontext)
			throws JobExecutionException {
		log.info("==================:"+this.getClass().getName() + " was just triggered...");
	}
}

Spring and Quartz integration configuration
//SchedulerFactoryBean configuration
quartz.xml
<?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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- Dispatch Factory -->
	<bean id="quartzScheduler"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="quartzProperties">
			<props>
				<prop key="org.quartz.scheduler.instanceName">defaultScheduler</prop>
				<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
				<!-- Thread pool configuration-->
				<prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
				<prop key="org.quartz.threadPool.threadCount">20</prop>
				<prop key="org.quartz.threadPool.threadPriority">5</prop>
				<!-- JobStore Configuration-->
				<prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>
				<!-- Cluster Configuration -->
				<prop key="org.quartz.jobStore.isClustered">true</prop>
				<prop key="org.quartz.jobStore.clusterCheckinInterval">15000</prop>
				<prop key="org.quartz.jobStore.maxMisfiresToHandleAtATime">1</prop>
				<prop key="org.quartz.jobStore.misfireThreshold">120000</prop>
				<!-- Table name configuration-->
				<prop key="org.quartz.jobStore.tablePrefix">QRTZ_</prop>
			</props>
		</property>
		<property name="schedulerName" value="defaultScheduler" />
		<!-- Required, QuartzScheduler starts delayed, QuartzScheduler restarts after application starts -->
		<property name="startupDelay" value="30" />
		<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
		<!-- Optional, QuartzScheduler updates the existing Job when it starts, so that you don't need to delete the corresponding record in the qrtz_job_details table every time you modify the targetObject -->
		<property name="overwriteExistingJobs" value="true" />
		<!-- Set autostart-->
		<property name="autoStartup" value="true" />
		<!-- Register Trigger-->
		<property name="triggers">
			<ref bean="defaultTriggers" />
		</property>
		<!-- register jobDetail -->
		<property name="jobDetails">
			<ref bean="defaultJobDetails" />
		</property>
		<!-- Register listener -->
		<property name="schedulerListeners">
			<ref bean="defaultSchedulerListeners" />
		</property>
	</bean>
	<import resource="quartz-task.xml" />
</beans>

//Timed task configuration
quartz-task.xm
<?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:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd">
	<!-- working bean -->
	<bean id="myJob" class="job.MyJob" />
	<!-- job configuration starts -->
	<bean id="myJobDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="myJob" />
		<property name="targetMethod" value="work" />
		<!-- <property name="concurrent" value="false" /> -->
	</bean>
	<bean id="myQuartzJobDetail"
		class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
		<property name="jobClass" value="job.MyQuartzJob" />
		<property name="name" value="myQuartzJob" />
		<!-- Whether the task is still retained in the database after the task is completed, the default is false -->
		<property name="durability" value="true" />
	</bean>
	<!-- The end of job configuration-->
	<!-- Scheduling task configuration start-->
	<bean id="cronTestJobTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="myJobDetail" />
		<!-- Cron expression "0/5 * * * * ?" means: execute every 5 seconds. -->
		<property name="cronExpression" value="0/5 * * * * ?" />
	</bean>
	<bean id="cronTestQuartzJobTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="myQuartzJobDetail" />
		<property name="cronExpression" value="0/5 * * * * ?" />
	</bean>
	<!-- default scheduler (defaultScheduler) trigger configuration -->
	<util:list id="defaultTriggers">
		<!-- <ref bean="crontestJobTrigger"/> -->
		<ref bean="cronTestQuartzJobTrigger" />
	</util:list>
	<!-- Default Scheduler (defaultScheduler) Job instance configuration -->
	<util:list id="defaultJobDetails">
	</util:list>
	<!-- default scheduler (defaultScheduler) listener configuration -->
	<util:list id="defaultSchedulerListeners">
	</util:list>
	<!-- JobDetail-Configuration Example-->
	<!-- <bean id="demoJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
			<property name="jobClass" value="job.MyQuartzJob" />
			<property name="jobDataAsMap">
				<map>
					<entry key="key" value="value" />
				</map>
			</property>
		 </bean> -->
</beans>

启动web控制台输出:
信息: Server startup in 11783 ms
2016-09-09 17:44:00 -32594 [org.springframework.scheduling.quartz.SchedulerFactoryBean] INFO    - Starting Quartz Scheduler now, after delay of 30 seconds
2016-09-09 17:44:00 -32660 [org.quartz.core.QuartzScheduler] INFO    - Scheduler defaultScheduler_$_donaldHP1473414210553 started.
2016-09-09 17:44:01 -32758 [job.MyQuartzJob] INFO    - ==================:job.MyQuartzJob was just triggered...
2016-09-09 17:44:01 -32802 [job.MyQuartzJob] INFO    - ==================:job.MyQuartzJob was just triggered...
2016-09-09 17:44:01 -32842 [job.MyQuartzJob] INFO    - ==================:job.MyQuartzJob was just triggered...
查看数据库Quartz,job,trriger相关信息:
mysql> use test;
Database changed
mysql> select * from qrtz_cron_triggers;
+------------------+--------------------------+---------------+-----------------+---------------+
| SCHED_NAME       | TRIGGER_NAME             | TRIGGER_GROUP | CRON_EXPRESSION | TIME_ZONE_ID  |
+------------------+--------------------------+---------------+-----------------+---------------+
| defaultScheduler | cronTestQuartzJobTrigger | DEFAULT       | 0/5 * * * * ?  | Asia/Shanghai |
+------------------+--------------------------+---------------+-----------------+---------------+
1 row in set

mysql> select * from qrtz_fired_triggers;
+------------------+------------------------------------+--------------------------+---------------+-----------------------+---------------+---------------+----------+----------+----------+-----------+------------------+-------------------+
| SCHED_NAME       | ENTRY_ID                           | TRIGGER_NAME             | TRIGGER_GROUP | INSTANCE_NAME         | FIRED_TIME    | SCHED_TIME    | PRIORITY | STATE    | JOB_NAME | JOB_GROUP | IS_NONCONCURRENT | REQUESTS_RECOVERY |
+------------------+------------------------------------+--------------------------+---------------+-----------------------+---------------+---------------+----------+----------+----------+-----------+------------------+-------------------+
| defaultScheduler | donaldHP14734142105531473414210556 | cronTestQuartzJobTrigger | DEFAULT       | donaldHP1473414210553 | 1473414385029 | 1473414390000 |        0 | ACQUIRED | NULL     | NULL      | 0                | 0                 |
+------------------+------------------------------------+--------------------------+---------------+-----------------------+---------------+---------------+----------+----------+----------+-----------+------------------+-------------------+
1 row in set

mysql> select * from qrtz_triggers;
+------------------+--------------------------+---------------+-------------+-----------+-------------+----------------+----------------+----------+---------------+--------------+---------------+----------+---------------+---------------+----------+
| SCHED_NAME       | TRIGGER_NAME             | TRIGGER_GROUP | JOB_NAME    | JOB_GROUP | DESCRIPTION | NEXT_FIRE_TIME | PREV_FIRE_TIME | PRIORITY | TRIGGER_STATE | TRIGGER_TYPE | START_TIME    | END_TIME | CALENDAR_NAME | MISFIRE_INSTR | JOB_DATA |
+------------------+--------------------------+---------------+-------------+-----------+-------------+----------------+----------------+----------+---------------+--------------+---------------+----------+---------------+---------------+----------+
|defaultScheduler | cronTestQuartzJobTrigger | DEFAULT       | myQuartzJob | DEFAULT   | NULL        |  1473414390000 |  1473414385000 |        0 | ACQUIRED      | CRON         | 1473414210000 |        0 | NULL          |             0 |          |
+------------------+--------------------------+---------------+-------------+-----------+-------------+----------------+----------------+----------+---------------+--------------+---------------+----------+---------------+---------------+----------+
1 row in set

mysql> select * from qrtz_job_details;
+------------------+-------------+------------------+-------------+-----------------+------------+------------------+----------------+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| SCHED_NAME       | JOB_NAME    | JOB_GROUP        | DESCRIPTION | JOB_CLASS_NAME  | IS_DURABLE | IS_NONCONCURRENT | IS_UPDATE_DATA | REQUESTS_RECOVERY | JOB_DATA                                                                          

| defaultScheduler | myQuartzJob | DEFAULT          | NULL        | job.MyQuartzJob | 1          | 0                | 0              | 0                 | 
?
loadFactorI


mysql> select * from qrtz_locks;
+------------------+----------------+
| SCHED_NAME       | LOCK_NAME      |
+------------------+----------------+
| defaultScheduler | STATE_ACCESS   |
| defaultScheduler | TRIGGER_ACCESS |
+------------------+----------------+
2 rows in set

mysql> select * from qrtz_scheduler_state;
+-----------------+------------------- ----+-------------------+-------------------+
| SCHED_NAME | INSTANCE_NAME | LAST_CHECKIN_TIME | CHECKIN_INTERVAL |
+------------------+-----------------------+--- ----------------+--------------------------------+
| defaultScheduler | donaldHP1473414210553 | 1473414375985 | 15000 |
+---- --------------+-------------+------------ --------+------------------+
1 row in set

Description:
There are two ways to configure timed tasks.
The first one: MethodInvokingJobDetailFactoryBean
<bean id="myJobDetail"	class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="myJob" />
		<property name="targetMethod" value="work" />
		<!-- <property name="concurrent" value="false" /> -->
</bean>

If you use this method, you need to shield the configuration
<property name="dataSource" ref="dataSource" />
		<property name="quartzProperties">
			<props>
				<prop key="org.quartz.scheduler.instanceName">defaultScheduler</prop>
				<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
				<!-- Thread pool configuration-->
				<prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
				<prop key="org.quartz.threadPool.threadCount">20</prop>
				<prop key="org.quartz.threadPool.threadPriority">5</prop>
				<!-- JobStore Configuration-->
				<prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>
				<!-- Cluster Configuration -->
				<prop key="org.quartz.jobStore.isClustered">true</prop>
				<prop key="org.quartz.jobStore.clusterCheckinInterval">15000</prop>
				<prop key="org.quartz.jobStore.maxMisfiresToHandleAtATime">1</prop>
				<prop key="org.quartz.jobStore.misfireThreshold">120000</prop>
				<!-- Table name configuration-->
				<prop key="org.quartz.jobStore.tablePrefix">QRTZ_</prop>
			</props>
		</property>

The second: JobDetailFactoryBean
<bean id="myQuartzJobDetail"
		class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
		<property name="jobClass" value="job.MyQuartzJob" />
		<property name="name" value="myQuartzJob" />
		<!-- Whether the task is still retained in the database after the task is completed, the default is false -->
		<property name="durability" value="true" />
</bean>

The main reason is that the implementation of JobDetailFactory is different. The first one does not need to implement org.quartz.Job, while the second one needs to implement org.quartz.Job; why are there two ways? One way is to store scheduled tasks in RAM. When the application starts, task information and task scheduling information are easily lost. To avoid task information and task scheduling information, it is necessary to persist task-related information to RDB. This is why the second way of job To implement the org.quartz.Job reason, because saving to the database requires job-bean related information.
To learn more about task persistence to RDB, please refer to:
Quartz Task Store JobStoreTX Persistence RDB-Source Analysis: http://donald-draper.iteye.com/admin/blogs/2323409

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327045167&siteId=291194637