使用spring4.0.6 和 quartz 2.2.3 集群
与非集群的区别主要在于spring-quartz.xml配置文件和多了一个quartz.properties配置文件。
spring-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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<bean name="analysisScheduleTask-bean" class="com.websystique.spring.scheduling.AnalysisScheduleTask"></bean>
<bean id="createTableTask-jobdetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- 是否持久化。 -->
<property name="durability" value="true" />
<!-- 属性必须设置为 true,当Quartz服务被中止后,再次启动或集群中
其他机器接手任务时会尝试恢复执行之前未完成的所有任务。 -->
<property name="requestsRecovery" value="true" />
<property name="jobClass" value="com.tsinghua.test.MyDetailQuartzJobBean"/>
<property name="jobDataAsMap">
<map>
<entry key="targetObject" value="analysisScheduleTask-bean" />
<entry key="targetMethod" value="createTableTask" />
</map>
</property>
<property name="description" value="创建表信息任务" />
</bean>
<bean id="createTableTask-trigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="createTableTask-jobdetail" />
<property name="cronExpression" value="0/1 * * * * ?" />
<property name="description" value="创建表信息" />
</bean>
<bean id="mapScheduler" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy">
<!-- applicationContextSchedulerContextKey:
是org.springframework.scheduling.quartz.SchedulerFactoryBean这个类中把spring
上下文以key/value的方式存放在了quartz的上下文中了,
可以用applicationContextSchedulerContextKey所定义的key得到对应的spring上下文-->
<property name="applicationContextSchedulerContextKey" value="applicationContext" />
<!-- configLocation:用于指明quartz的配置文件的位置,
如果不用spring配置quartz的话,本身quartz是有一个默认配置文件,
默认名称是quartz.properties,在jar包中。 -->
<property name="configLocation" value="classpath:quartz.properties" />
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- overwriteExistingJobs:覆盖任务调度器中同名的jobDetail,避免只修改了CronExpression所造成的不能重新生效情况 -->
<property name="overwriteExistingJobs" value="true" />
<!-- 在SchedulerFactoryBean初始化完成后,延迟5秒启动Scheduler,以便让Spring能够更快初始化容器中剩余的Bean。-->
<property name="startupDelay" value="5" />
<!-- 是否自动启动 -->
<property name="autoStartup" value="true" />
<property name="triggers">
<list>
<ref bean="createTableTask-trigger" />
</list>
</property>
</bean>
</beans>
quartz.properties配置文件
#==============================================================
#Configure Main Scheduler Properties
#==============================================================
org.quartz.scheduler.instanceName = quartzScheduler
org.quartz.scheduler.instanceId = AUTO
#==============================================================
#Configure JobStore
#==============================================================
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.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
#==============================================================
#Configure ThreadPool
#==============================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
properties文件详解见:
测试截图
我的电脑切换比较慢,不知道怎么回事大概10秒左右才会切换