Quartz Scheduler当前的工作正在增加

时间:2017-06-15 16:39:45

标签: java quartz-scheduler

我用石英创建了一个示例程序。如果主程序将触发一个每3秒运行一次的控制器作业,并且从该控制器中我已经安排了一个以cron计划运行的作业。

我的要求是,如果一个作业实例正在运行,则下一个实例不应该启动。所以我从调度程序上下文当前活动的作业迭代了jobcontext列表,并检查是否存在与当前作业实例匹配的作业的任何实例,然后返回。

为了测试这个,我已经在相应的作业中实现了一个线程睡眠,这超过了预定的时间。作业实例并未按预期并行触发,但我的调度程序上下文当前活动作业列表不断增加。我需要帮助减少/检查当前活动作业的大小。

我的节目如下。

主程序:

package com.test.objectpool;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class CronTriggerExample {

    public static void main(String[] args) throws Exception {
        JobDetail job = JobBuilder.newJob(QuartzSchedulerController.class).withIdentity("job-a-cntrl", "group.12-1")
                .build();
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger.1", "group.12-1")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()).build();

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);

    }
}

控制器工作:

package com.test.objectpool;

import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;

public class QuartzSchedulerController implements Job {

    public void execute(JobExecutionContext context) {

        try {
            context.getScheduler().getCurrentlyExecutingJobs().forEach(job -> {
                if (job.getTrigger().equals(context.getTrigger()) && !job.getJobInstance().equals(this)) {
                    System.out.println("There's another instance running crontroller , so leaving" + this);
                    return;
                }
            });

            Thread.sleep(4000);
            System.out.println("Inside scheduler controller --- >> ");
            Scheduler scheduler = context.getScheduler();

            JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("hello-1.1", "group1-1.2").build();
            JobKey jbK = job.getKey();
            System.out.println("Job key is " + jbK);
            if (!scheduler.checkExists(jbK)) {
                System.out.println("Scheduling hellow world -----");
                Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger.1", "group1-1.2").withSchedule(
                        CronScheduleBuilder.cronSchedule("0/2 * * * * ?").withMisfireHandlingInstructionDoNothing())
                        .build();

                scheduler.scheduleJob(job, trigger);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

最后由控制器控制的作业类是:

package com.test.objectpool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;

public class HelloJob implements Job {
    private static int count = 0;

    public void execute(JobExecutionContext context) throws JobExecutionException {

        try {
            List<JobExecutionContext> jobs = context.getScheduler().getCurrentlyExecutingJobs();
            System.out.println("The size of the job queue is " + jobs.size());
            for (JobExecutionContext job : jobs) {
                if (job.getTrigger().equals(context.getTrigger()) && !job.getJobInstance().equals(this)) {
                    System.out.println("There's another instance running, so leaving" + this);
                    return;
                }

            }
            Date dt = Calendar.getInstance().getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
            String uniqueID = UUID.randomUUID().toString();
            System.out.println("Hello Quartz!" + context.getScheduledFireTime() + " :With count " + count
                    + " :Current date " + sdf.format(dt) + " UUID =" + uniqueID);

            Thread.sleep(10000);
            // System.out.println("Hello Quartz!"+
            // context.getScheduledFireTime());

            System.out.println("Completed " + "With count " + count + " UUID : " + uniqueID);
            count++;
        } catch (SchedulerException | InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

}

问题是,通过这种方法,context.getScheduler().getCurrentlyExecutingJobs()不断增加。

1 个答案:

答案 0 :(得分:2)

使用@DisallowConcurrentExecution注释您的工作,如下所示:

@DisallowConcurrentExecution
public class QuartzSchedulerController implements Job {
    ...

并且Quartz将负责不运行多个实例。

  

一个注释,它将Job类标记为一个不能同时执行多个实例的注释(实例基于JobDetail定义 - 或者换句话说基于JobKey })。