quartz集群分布式(并发)部署解决方案-Spring
Posted Never be the same
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了quartz集群分布式(并发)部署解决方案-Spring相关的知识,希望对你有一定的参考价值。
项目中使用分布式并发部署定时任务,多台跨JVM,按照常理逻辑每个JVM的定时任务会各自运行,这样就会存在问题,多台分布式JVM机器的应用服务同时干活,一个是加重服务负担,另外一个是存在严重的逻辑问题,比如需要回滚的数据,就回滚了多次,刚好quartz提供很好的解决方案。
集群分布式并发环境中使用QUARTZ定时任务调度,会在各个节点会上报任务,存到数据库中,执行时会从数据库中取出触发器来执行,如果触发器的名称和执行时间相同,则只有一个节点去执行此任务。
如果此节点执行失败,则此任务则会被分派到另一节点执行,中途也会自动检查失效的定时调度,发现不成功的,其他节点立马接过来继续完成定时任务。对应的定时任务调度表比较多,有11个。
此方法是结合Spring与quartz,实际解决方案如下:
####applicationContext-scheduler.xml### spring 3.11
<?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: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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.dennis.walking.api.web.schedule" />
<bean id="rollbackOrderStatus" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.dennis.walking.api.web.schedule.ReleaseQtyAndUpdateOrderStatusSchedule" />
<property name="durability" value="true" />
</bean>
<bean id="rollbackOrderStatusTrigger" class="com.dennis.walking.api.web.schedule.PersistableCronTriggerFactoryBean">
<property name="jobDetail" ref="rollbackOrderStatus" />
<property name="cronExpression">
<value>0 0/5 * * * ?</value>
</property>
<property name="timeZone">
<value>GMT+8:00</value>
</property>
</bean>
<bean id="quartzScheduler" parent="baseQuartzScheduler">
<property name="configLocation" value="classpath:quartz.properties" />
<property name="autoStartup" value="true" />
<!-- This name is persisted as SCHED_NAME in db. for local testing could change to unique name to avoid collision with dev server -->
<property name="schedulerName" value="apiQuartzScheduler" />
<!-- NOTE: Must add both the jobDetail and trigger to the scheduler! -->
<property name="triggers">
<list>
<ref bean="rollbackOrderStatusTrigger" />
</list>
</property>
<property name="jobDetails">
<list>
<ref bean="rollbackOrderStatus" />
</list>
</property>
</bean>
<bean id="baseQuartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!--
<property name="configLocation" value="classpath:quartz.properties" />
-->
<property name="dataSource" ref=