最近用到quartz做集群,需求每隔一段時間執行一次任務。
發現在配置較短的時間間隔,比如15秒鐘執行一次時,會出現,服務器啟動會有多個線程(不等,有時候2個,多的時候4個,quartz線程數目用的默認的10個)重復執行該任務。
控制台信息如下:
而當我配置成1min執行一次的時候,就沒有問題,不會重發執行。
主要的配置文件如下,求解,感謝感謝。
<bean id="autoRepeatRequestTask"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass">
<value>com..job.AutoRequestRepeatJob</value>
</property>
<property name="durability" value="true" />
<property name="requestsRecovery" value="true" />
</bean>
<!-- 定義觸發時間 -->
<bean id="autoRepeatRequestDoTime"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="autoRepeatRequestTask" />
</property>
<!-- cron表達式 -->
<property name="cronExpression">
<value>0 * * * * ?</value>
<!--每分鐘執行一次 -->
</property>
</bean>
<!-- 總管理類 如果將lazy-init='false'那麼容器啟動就會執行調度程序 -->
<bean id="startQuartz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
destroy-method="destroy">
<property name="configLocation" value="classpath:quartz.properties" />
<property name="triggers">
<list>
<ref bean="autoRepeatRequestDoTime" />
</list>
</property>
<property name="startupDelay" value="10" />
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
</bean>
執行的任務類:
@PersistJobDataAfterExecution
@DisallowConcurrentExecution // 不允許並發執行
public class AutoRequestRepeatJob extends QuartzJobBean {
private static final Logger LOGGER = LoggerFactory.getLogger(AutoRequestRepeatJob.class);
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println(Thread.currentThread().getName()+"------初始化了"+DateTimeUtils.getNowHHmmss());
RequestRepeatService requestRepeatService = getApplicationContext(context).getBean(RequestRepeatService.class);
requestRepeatService.autoRepeatRequest();
}
private ApplicationContext getApplicationContext(final JobExecutionContext jobexecutioncontext) {
try {
return (ApplicationContext) jobexecutioncontext.getScheduler().getContext().get("applicationContextKey");
} catch (SchedulerException e) {
LOGGER.error("jobexecutioncontext.getScheduler().getContext() error!", e);
throw new RuntimeException(e);
}
}
}
quartz集群配置
#==============================================================
#Configure Main Scheduler Properties
#==============================================================
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.instanceName=AUTO_REPEAT_REQUEST
#============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.isClustered=true
org.quartz.jobStore.clusterCheckinInterval=15000
org.quartz.jobStore.misfireThreshold = 60000
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.useProperties = false
org.quartz.jobStore.dataSource = myDS
#============================================================================
# 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
#============================================================================
# Configure Datasources
#============================================================================