如何跳过cron触发firetime

时间:2011-05-27 17:22:24

标签: quartz-scheduler

我正在从远程主机上的目录传输文件,并且当间隔到达此作业时触发器会触发作业。但是我想确保当一个作业仍在处理存储(下载文件尚未完成)时触发火时间到了,石英会跳过这个间隔。我试着用它 c r on_trigger.MISFIRE_INSTRUCTION_DO_NOTHING但我似乎只是因为没有可用的工作线程。

public CronTrigger scheduleJob(RemoteJob job, String cronExpression,Date firstFireTime) throws SchedulerException, ParseException {
    JobDetail jobDetail = new JobDetail(job.getDescription(), job.getName(), job.getClass());
    CronTrigger crTrigger = new CronTrigger(
            "cronTrigger", job.getName(), cronExpression);       
    scheduler.scheduleJob(jobDetail, crTrigger);    
    crTrigger.setStartTime(firstFireTime);
    crTrigger.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);
    return crTrigger;

}

3 个答案:

答案 0 :(得分:2)

创建一个TriggerListener,用于跟踪您的某个下载作业是否正在运行,然后返回true以否决执行其他类型的作业。

答案 1 :(得分:1)

我稍微修改了上面的代码,就可以了。

/**
 * not for cluster
 */
@Override
public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
    try {
        List<JobExecutionContext> currentlyExecutingJobs = context.getScheduler().getCurrentlyExecutingJobs();
        for (JobExecutionContext jobContext : currentlyExecutingJobs) {
            if (jobContext.getTrigger().equals(trigger) &&
                    jobContext.getJobDetail().getKey().equals(trigger.getJobKey())) {
                return true;
            }
        }
    } catch (SchedulerException ex) {
        return true;
    }
    return false;
}

答案 2 :(得分:0)

我按照你的说法做了

public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) { 

    try {

        List<JobExecutionContext> jobs = 
context.getScheduler().getCurrentlyExecutingJobs();

        for (JobExecutionContext job : jobs) {


            if (job.getTrigger().equals(context.getTrigger()) && 
!job.getJobInstance().equals(this)) {

                _logger.info("There's another instance running,So job discarded  " + context.getJobDetail().getGroup()+ ":"+context.getJobDetail().getName());
                return true;
            }
        }
    } catch (SchedulerException ex) {

     return  true;
    }

    return false;
}