quartz利用自带的表实现分布式,有一个节点执行的时候其他节点不会发生执行。
1.下载quartz-2.2.1包,quartz-2.2.1\docs\dbTables里面有想要执行的脚本文件。我用的是tables_mysql_innodb.sql进行执行,记得表结构要改成innerDB,不然会报错。然后maven引入quartz(2.2.3),quartz-jobs(2.2.3),spring-context-suppor(4.2.6.RELEASE)t包
2.创建quartz.properties文件
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.instanceId: AUTO
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 15
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
org.quartz.jobStore.misfireThreshold: 60000
org.quartz.jobStore.maxMisfiresToHandleAtATime: 1
#org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
# for cluster
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.isClustered: true
org.quartz.jobStore.clusterCheckinInterval: 5000
3.在spring-quartz.xml里面添加相关配置信息
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.test.service.JobTask"/>
<property name="durability" value="true"/>
<property name="requestsRecovery" value="true"/>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"/>
<property name="cronExpression" value="0/10 * * * * ?"/>
</bean>
<bean name="quartzScheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="quartzProperties">
<props>
<prop key="org.quartz.scheduler.instanceName">SpringQuartzCluster</prop>
<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
<!-- 线程池配置
--> <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 配置
--> <prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>
<!-- 集群配置
--> <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>
<prop key="org.quartz.jobStore.tablePrefix">QRTZ_</prop>
</props>
</property>
<property name="schedulerName" value="SpringQuartzCluster"/>
<!-- 必须的,QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动
--> <property name="startupDelay" value="30"/>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
<!-- 可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
--> <property name="overwriteExistingJobs" value="true"/>
<!-- 设置自动启动 -->
<property name="autoStartup" value="true"/>
<property name="configLocation" value="classpath:quartz.properties"/>
<property name="jobDetails">
<list>
<ref bean="jobDetail"/>
</list>
</property>
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
4.java代码
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Service
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-config.xml")
public class JobTask extends QuartzJobBean {
private Log log = LogFactory.getLog(JobTask.class);
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("#########################################定時器執行"+DateUtil.getSimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"#############################################");
}
}
另外推荐大牛作品《分布式任务调度平台XXL--JOB》,地址https://github.com/xuxueli/xxl-job