2014-12-30 58 views
0

任何人都可以告訴我如何獲得石英當前的嘗試次數。獲取石英的嘗試次數

示例:如果Quartz調度程序以重複計數5開始。我想獲取當前重複計數。

這裏是我與

public class SimpleTriggerExample implements Job 
{ 
    int count = 0; 
JobDetail job = null; 
JobDataMap data = null; 

public static void main(String[] args) throws Exception 
{ 
    new SimpleTriggerExample().schedule(); 

} 
public void schedule() throws ParseException, SchedulerException{ 
    job = JobBuilder.newJob(SimpleTriggerExample.class) 
      .withIdentity("dummyJobName", "group1").build(); 
     Trigger trigger = TriggerBuilder 
      .newTrigger() 
      .withIdentity("dummyTriggerName", "group1") 
      .withSchedule(SimpleScheduleBuilder.simpleSchedule() 
      .withIntervalInSeconds(10).withRepeatCount(3)) 

      .build(); 
     System.out.println("before in main jobdatamap"); 

     Scheduler scheduler = new StdSchedulerFactory().getScheduler(); 
     scheduler.start(); 
     scheduler.scheduleJob(job, trigger); 
} 
public void execute(JobExecutionContext context) 
     throws JobExecutionException { 

      //count 
    data = context.getJobDetail().getJobDataMap(); 
    System.out.println("after jobdatamap"); 

      int count1 = data.getInt("EXECUTION_COUNT"); 
      System.out.println("count1-->before"+count1); 
      count1++; 
      System.out.println("count1-->after"+count1); 

      job.getJobDataMap().put("EXECUTION_COUNT", count1); 

      count = count1; 

      System.out.println("count"+count); 


     } 
} 

回答

0

使用的JobDataMap與@PersistJobDataAfterExecution註釋一起努力爲例。

確保在修改JobDataMap中的數據時,鍵值應該相同。

如果你這樣做,你可以堅持你的嘗試按照你的要求。

示例代碼片段:

package com.mss.quartz.demo; 

import java.text.SimpleDateFormat; 
import java.util.Date; 

import org.quartz.InterruptableJob; 
import org.quartz.Job; 
    import org.quartz.JobBuilder; 
import org.quartz.JobDataMap; 
import org.quartz.JobDetail; 
import org.quartz.JobExecutionContext; 
import org.quartz.JobExecutionException; 
import org.quartz.JobKey; 
import org.quartz.PersistJobDataAfterExecution; 
import org.quartz.Scheduler; 
import org.quartz.SchedulerContext; 
import org.quartz.SchedulerException; 
import org.quartz.SimpleScheduleBuilder; 
import org.quartz.TriggerBuilder; 
import org.quartz.UnableToInterruptJobException; 
import org.quartz.impl.StdSchedulerFactory; 
@PersistJobDataAfterExecution 
public class HelloJob implements InterruptableJob 
{ 
SchedulerContext schedulerContext = null; 
testQuartz test = new testQuartz(); 

boolean result; 
private boolean _interrupted = false; 
private JobKey _jobKey = null; 
Thread t = null; 
//public static int count = 0; 

public void interrupt() throws UnableToInterruptJobException { 
    System.out.println("---" + this._jobKey + " -- INTERRUPTING --"); 
    this._interrupted = true; 
} 
public void execute(JobExecutionContext context) 
throws JobExecutionException { 
    Scheduler scd = context.getScheduler(); 


    JobDataMap dataMap = context.getJobDetail().getJobDataMap(); 

    String jobSays = dataMap.getString("test1"); 

    int myFloatValue = dataMap.getIntValue("id"); 

    System.out.println("In Job Class"+jobSays+ " "+myFloatValue+" Current time in 

    Job class "+new Date().toString()); 
    JobKey jobKey = context.getJobDetail().getKey(); 
    int attemps = dataMap.getInt("attempts"); 
    attemps++; 
    dataMap.put("attempts", attemps); 
    System.out.println("After putting count in job data map:"+dataMap.get("attempts")); 




} 




} 
0

嘗試將@PersistJobDataAfterExecution註釋添加到SimpleTriggerExample類:

@PersistJobDataAfterExecution 
public class SimpleTriggerExample implements Job 
{ ...}