Quartz: skipping duplicate job fires scheduled with same fire time

acc15

I'm new to Quartz and stuck on duplicate job runs. They was caused by two triggers which has overlapping fire times.

Is there any Quartz "out-of-the-box" functionality which will prevent duplicate fires of same job with multiple attached triggers?

Or maybe there is some third-party Trigger implementation which allows to combine schedule's with proper fire time calculation which will prevent duplicate fire times?

Or maybe i can somehow combine multiple schedules?

Any solution will be greatly appreciated.

Thanks

For the clarity:

public static class MyJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("My Job at " + DateUtils.toDateTimeString(context.getFireTime()) +  " by " +
                context.getTrigger().getKey().getName());
    }
}

startScheduler() {

    final Set<Trigger> triggers = new HashSet<Trigger>();
    triggers.add(TriggerBuilder.<CronScheduleBuilder>newTrigger().withIdentity("every 3 secs").
            withSchedule(CronScheduleBuilder.cronSchedule("0/3 * * * * ? *")).build());
    triggers.add(TriggerBuilder.<CronScheduleBuilder>newTrigger().withIdentity("every 2 secs").
            withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ? *")).build());

    final Scheduler scheduler = new StdSchedulerFactory(properties).getScheduler();
    scheduler.scheduleJob(JobBuilder.newJob(MyJob.class).build(), triggers, false);
    scheduler.start();

}

And the logs:

  • My Job at 01.11.2013T23:19:26.000 by every 2 secs
  • My Job at 01.11.2013T23:19:27.000 by every 3 secs
  • My Job at 01.11.2013T23:19:28.000 by every 2 secs
  • My Job at 01.11.2013T23:19:30.000 by every 2 secs
  • My Job at 01.11.2013T23:19:30.001 by every 3 secs
  • My Job at 01.11.2013T23:19:32.000 by every 2 secs
  • My Job at 01.11.2013T23:19:33.000 by every 3 secs
  • My Job at 01.11.2013T23:19:34.000 by every 2 secs
  • My Job at 01.11.2013T23:19:36.000 by every 2 secs
  • My Job at 01.11.2013T23:19:36.001 by every 3 secs
  • My Job at 01.11.2013T23:19:38.000 by every 2 secs
  • My Job at 01.11.2013T23:19:39.000 by every 3 secs
  • My Job at 01.11.2013T23:19:40.000 by every 2 secs
  • My Job at 01.11.2013T23:19:42.001 by every 2 secs
  • My Job at 01.11.2013T23:19:42.002 by every 3 secs
acc15

Found that TriggerListener's suitable for this task:

scheduler.getListenerManager().addTriggerListener(new TriggerListener() {

    private Date lastFireTime = null;

    @Override
    public String getName() {
        return "prevent-duplicate-fires";
    }

    @Override
    public void triggerFired(Trigger trigger, JobExecutionContext context) {
    }

    @Override
    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
        final Date fireTime = context.getScheduledFireTime();
        if (lastFireTime != null && fireTime.equals(lastFireTime)) {
            return true;
        }
        lastFireTime = fireTime;
        return false;
    }

    @Override
    public void triggerMisfired(Trigger trigger) {
    }

    @Override
    public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {
    }
});

Looks like it works:

  • My Job at 01.11.2013T23:46:14.000 by every 2 secs
  • My Job at 01.11.2013T23:46:15.000 by every 3 secs
  • My Job at 01.11.2013T23:46:16.000 by every 2 secs
  • My Job at 01.11.2013T23:46:18.000 by every 2 secs
  • ---> Should fire but skipped - My Job at 01.11.2013T23:46:18.000 by every 3 secs
  • My Job at 01.11.2013T23:46:20.000 by every 2 secs
  • My Job at 01.11.2013T23:46:21.000 by every 3 secs
  • My Job at 01.11.2013T23:46:22.000 by every 2 secs
  • My Job at 01.11.2013T23:46:24.000 by every 2 secs
  • ---> Should fire but skipped - My Job at 01.11.2013T23:46:24.000 by every 3 secs
  • My Job at 01.11.2013T23:46:26.000 by every 2 secs
  • My Job at 01.11.2013T23:46:27.000 by every 3 secs

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Set up a scheduled job?

From Dev

Run Quartz Scheduler Job with specific start, end date and within time constraints

From Dev

$observe multiple attributes at the same time and fire callback only once

From Dev

Same quartz job running twice because of two server instances

From Dev

Grails Transaction in Quartz Job

From Dev

Are 'Scheduled Job" and "Scheduled Task" the same thing in Powershell context?

From Dev

Configuring Quartz.Net to stop a job from executing, if it is taking longer than specified time span

From Dev

jboss drools, multiple same facts, fire rule only one time

From Dev

Can Quartz Scheduler (cron) schedule multiple jobs at the same execution time?

From Dev

One-time scheduled task that fires on logon for another local user in Powershell

From Dev

Why do Android alarms fire at the same time?

From Dev

Quartz job Vs. Thread for immediate one time task

From Dev

Possible to run the same job with different trigger in quartz?

From Dev

Quartz.net - How to skip to next_fire_time?

From Dev

Spring Quartz : Job does not fire

From Dev

Need A Timer To Fire At Specific Time And Every 5 Minutes Until Job Complete

From Dev

Java Quartz scheduled Job - disallow concurrent execution of Job

From Dev

Amazon EC2 - Quartz and Job not running at correct time

From Dev

Running scheduled Spark job

From Dev

Skipping quartz scheduler for a particular time in a particular day

From Dev

Quartz Failure On Job Recovery

From Dev

Quartz.Net Trigger Scheduled Job On Demand

From Dev

Run Quartz job synchronous

From Dev

Update args of a scheduled job

From Dev

Guice scope for scheduled job run

From Dev

how to send an array as parameter to quartz scheduled job in C#?

From Dev

Spring Scheduled cron job

From Dev

Multiple triggers of same Job Quartz.NET

From Dev

Powershell - Manually trigger a scheduled job

Related Related

  1. 1

    Set up a scheduled job?

  2. 2

    Run Quartz Scheduler Job with specific start, end date and within time constraints

  3. 3

    $observe multiple attributes at the same time and fire callback only once

  4. 4

    Same quartz job running twice because of two server instances

  5. 5

    Grails Transaction in Quartz Job

  6. 6

    Are 'Scheduled Job" and "Scheduled Task" the same thing in Powershell context?

  7. 7

    Configuring Quartz.Net to stop a job from executing, if it is taking longer than specified time span

  8. 8

    jboss drools, multiple same facts, fire rule only one time

  9. 9

    Can Quartz Scheduler (cron) schedule multiple jobs at the same execution time?

  10. 10

    One-time scheduled task that fires on logon for another local user in Powershell

  11. 11

    Why do Android alarms fire at the same time?

  12. 12

    Quartz job Vs. Thread for immediate one time task

  13. 13

    Possible to run the same job with different trigger in quartz?

  14. 14

    Quartz.net - How to skip to next_fire_time?

  15. 15

    Spring Quartz : Job does not fire

  16. 16

    Need A Timer To Fire At Specific Time And Every 5 Minutes Until Job Complete

  17. 17

    Java Quartz scheduled Job - disallow concurrent execution of Job

  18. 18

    Amazon EC2 - Quartz and Job not running at correct time

  19. 19

    Running scheduled Spark job

  20. 20

    Skipping quartz scheduler for a particular time in a particular day

  21. 21

    Quartz Failure On Job Recovery

  22. 22

    Quartz.Net Trigger Scheduled Job On Demand

  23. 23

    Run Quartz job synchronous

  24. 24

    Update args of a scheduled job

  25. 25

    Guice scope for scheduled job run

  26. 26

    how to send an array as parameter to quartz scheduled job in C#?

  27. 27

    Spring Scheduled cron job

  28. 28

    Multiple triggers of same Job Quartz.NET

  29. 29

    Powershell - Manually trigger a scheduled job

HotTag

Archive