Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find if a job is running in Quartz1.6

I would like to clarify details of the scheduler.getCurrentlyExecutingJobs() method in Quartz1.6. I have a job that should have only one instance running at any given moment. It can be triggered to "run Now" from a UI but if a job instance already running for this job - nothing should happen.

This is how I check whether there is a job running that interests me:

    List<JobExecutionContext> currentJobs = scheduler.getCurrentlyExecutingJobs();
    for (JobExecutionContext jobCtx: currentJobs){
    jobName = jobCtx.getJobDetail().getName();
    groupName = jobCtx.getJobDetail().getGroup();
    if (jobName.equalsIgnoreCase("job_I_am_looking_for_name") &&
        groupName.equalsIgnoreCase("job_group_I_am_looking_for_name")) {
        //found it!
        logger.warn("the job is already running - do nothing");
    }               
}

then, to test this, I have a unit test that tries to schedule two instances of this job one after the other. I was expecting to see the warning when trying to schedule the second job, however, instead, I'm getting this exception:

org.quartz.ObjectAlreadyExistsException: Unable to store Job with name:
 'job_I_am_looking_for_name' and group: 'job_group_I_am_looking_for_name', 
 because one already exists with this identification.

When I run this unit test in a debug mode, with the break on this line:

List currentJobs = scheduler.getCurrentlyExecutingJobs();

I see the the list is empty - so the scheduler does not see this job as running , but it still fails to schedule it again - which tells me the job was indeed running at the time...

Am I missing some finer points with this scheduler method?

Thanks!

Marina

like image 375
Marina Avatar asked Feb 06 '12 02:02

Marina


1 Answers

For the benefit of others, I'm posting an answer to the issue I was having - I got help from the Terracotta Forum's Zemian Deng: posting on Terracotta's forum

Here is the re-cap: The actual checking of the running jobs was working fine - it was just timing in the Unit tests, of course. I've added some sleeping in the job, and tweaked unit tests to schedule the second job while the first one is still running - and verified that I could indeed find the first job still running.

The exception I was getting was because I was trying to schedule a new job with the same name, rather than try to trigger the already stored in the scheduler job. The following code worked exactly as I needed:

List<JobExecutionContext> currentJobs = scheduler.getCurrentlyExecutingJobs();
for (JobExecutionContext jobCtx: currentJobs){
    jobName = jobCtx.getJobDetail().getName();
    groupName = jobCtx.getJobDetail().getGroup();
    if (jobName.equalsIgnoreCase("job_I_am_looking_for_name") && groupName.equalsIgnoreCase("job_group_I_am_looking_for_name")) {
        //found it!
        logger.warn("the job is already running - do nothing");
                 return;
    }               
}
      // check if this job is already stored in the scheduler
JobDetail emailJob;
emailJob = scheduler.getJobDetail("job_I_am_looking_for_name", "job_group_I_am_looking_for_name");
if (emailJob == null){
       // this job is not in the scheduler yet
  // create JobDetail object for my job 
  emailJob = jobFactory.getObject();
  emailJob.setName("job_I_am_looking_for_name");
  emailJob.setGroup("job_group_I_am_looking_for_name");
  scheduler.addJob(emailJob, true);             
}

// this job is in the scheduler and it is not running right now - run it now
scheduler.triggerJob("job_I_am_looking_for_name", "job_group_I_am_looking_for_name");

Thanks! Marina

like image 122
Marina Avatar answered Oct 26 '22 00:10

Marina