Yunus Einsteinium
Yunus Einsteinium

Reputation: 1180

Quartz @DisallowConcurrentExecution not working as expected

I am using java quartz schedular. I am able to schedule jobs perfectly, though what i want is wait for job to finish before runing the second round because the time it takes to run each job varies.

I used @DisallowConcurrentExecution, what it did is only make the job to run once and never again. From job listener shows that the job finished successfully once.

Job
=============================================================

@DisallowConcurrentExecution
public class SalesJob implements Job{
    List<Transaction> unsentTransaction = new ArrayList<Transaction>();
    List<Sale> sales = new ArrayList<Sale>();

    public void execute(JobExecutionContext jec) throws JobExecutionException {
        System.out.println("Sales Job. . .");
    }
}

Job Listener:

public class SalesJobListener implements JobListener{
public static final String LISTENER_NAME = "dummyJobListenerName";

public String getName() {
    return LISTENER_NAME;
}

public void jobToBeExecuted(JobExecutionContext context) {
    String jobName = context.getJobDetail().getKey().toString();
System.out.println("jobToBeExecuted");
System.out.println("Job : " + jobName + " is going to start...");
}

public void jobExecutionVetoed(JobExecutionContext jec) {
    System.out.println("jobExecutionVetoed");
}

public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
    System.out.println("jobWasExecuted");
    String jobName = context.getJobDetail().getKey().toString();
    System.out.println("Job : " + jobName + " is finished...");
    System.out.println("=====================================");
    System.out.println("==========" + new Date() + "===========");

    if (!jobException.getMessage().equals("")) {
            System.out.println(
                    "Exception thrown by: " + jobName + " Exception: " + jobException.getMessage());
    }
}

}

This is the schedular

JobKey salesJobKey = new JobKey("salesJob", "group1");
    JobDetail salesJob = JobBuilder.newJob(SalesJob.class)
    .withIdentity(salesJobKey).build();

    Trigger salesTrigger = TriggerBuilder
                            .newTrigger()
                            .withIdentity("salesTrigger", "group1")
                            .withSchedule(
                                    CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                            .build();

    Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    scheduler.getListenerManager().addJobListener(
        new SalesJobListener(), KeyMatcher.keyEquals(salesJobKey)
    );

    scheduler.start();
    scheduler.scheduleJob(salesJob, salesTrigger);

Upvotes: 1

Views: 4385

Answers (2)

Samik_htik
Samik_htik

Reputation: 11

Just Add the below null check

if(null != jobException) {
                if (!jobException.getMessage().equals("")) {
                    logger.debug("Exception thrown by: " + jobName
                            + " Exception: " + jobException.getMessage());
                }           
            }

Your code will work

Upvotes: 0

Jordi Castilla
Jordi Castilla

Reputation: 26961

PROBLEM

it executed this time, Wed Nov 25 12:01:15 EAT 2015 and now is Wed Nov 25 12:32 2015, so basically i have waited > 30 mins. . . and there is no another job

That is saying the Scheduler is not working.

WHY?

  • you cannot execute a job at second 15 of a minute because the pattern: 0/5 * * * * ? makes scheduler to run ONLY at seconds 0 and 5 of each minute.
  • Using @DisallowConcurrentExecution will prevent execution a Job if another one of same type is already running.

SOLUTION:

The mistake is in the order of your code, you execute then scheduler (scheduler.start();) before tell that it must schedule a job (scheduler.scheduleJob(salesJob, salesTrigger);):

scheduler.start();
scheduler.scheduleJob(salesJob, salesTrigger);

Check this example and swap your lines:

scheduler.scheduleJob(salesJob, salesTrigger);
scheduler.start();

That's all...

Upvotes: 1

Related Questions