不要尝试使用石英

时间:2014-12-30 14:32:30

标签: quartz-scheduler

任何人都可以告诉我如何在石英中获得当前的尝试次数。

示例:如果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);


        }
}

2 个答案:

答案 0 :(得分: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"));




}




}

答案 1 :(得分:0)

尝试将@PersistJobDataAfterExecution批注添加到SimpleTriggerExample类:

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