论坛首页 Java企业应用论坛

Terracotta Quartz集群环境下StatefulJob在Failover时状态数据丢失

浏览 2401 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-10-16  
我在Terracotta集群环境下使用Quartz集群(两个Server实例A和B),采用StatefulJob,使用jobdataMap记录任务执行次数(count)。

A和B启动后,job在A结点执行,当A结点宕机后,B结点开始执行job,但是此时获取的jobdataMap中没有任何数据,也就重新开始计数。

相同的代码配置jdbcJobStore时,failover后计数正确。

代码如下:

quartz.properties:
org.quartz.scheduler.instanceName = ReportControlScheduler
org.quartz.scheduler.instanceId = AUTO
orgorg.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5
org.quartz.threadPool.threadPriority = 4
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz_job.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true 

#TerracottaJobStore
#org.quartz.jobStore.class = org.terracotta.quartz.TerracottaJobStore
org.quartz.jobStore.class = org.terracotta.quartz.EnterpriseTerracottaJobStore
org.quartz.jobStore.tcConfigUrl = 10.4.46.121:9510


quartz_job.xml:
<?xml version='1.0' encoding='utf-8'?>
<job-scheduling-data
	xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_2_0.xsd"
	version="2.0">
	<schedule>
		<job>
			<name>ReportControlJob</name>
			<group>Report</group>
			<description>测试</description>
			<job-class>com.neusoft.acap.eaf.QuartzJob</job-class>
			 <durability>false</durability>
			<recover>false</recover>  
			<job-data-map>
				<entry>
				<key>count</key>
				<value>0</value>
				</entry>
			</job-data-map>
		</job>
		<trigger>
			<cron>
				<name>report-trigger</name>
				<group>Report_Group</group>
				<description>Trigger</description>
				<job-name>ReportControlJob</job-name>
				<job-group>Report</job-group>
				<cron-expression>0/15 * * * * ?</cron-expression>
			</cron>
			
		</trigger>
		
	</schedule>
	
</job-scheduling-data>


job代码:
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.quartz.SchedulerException;


@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class QuartzJob implements Job {

	public void execute(JobExecutionContext context) throws JobExecutionException {
		
		int count = 0;
		System.out.println(context.isRecovering());
		try {
			JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();

			if(jobDataMap.containsKey("count")){
				count = jobDataMap.getInt("count");
			}

			//显示job信息,count自动加1 System.out.println(context.getJobDetail().getDescription()+"--"+context.getTrigger().getDescription()+"--"+context.getScheduler().getSchedulerInstanceId()+"--"+context.getFireTime()+"---"+context.getNextFireTime()+"---"+count++);
			jobDataMap.put("count", count);
			
                       //在下列等待中关闭任意结点,自动failover
			System.out.println("开始等待");
			 try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 System.out.println("结束等待");
			
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics