0

Hi i have a Job 1 triggered to fire very minute and a job 2 triggered to fire every 5 min. So a at every five minutes the wo jobs will run at the same time, i want to avoid this and force the second job to fire to wait for the other one to finish before starting. I have seen @DisallowConcurrentExecution but that will only avoid parallel running for the two instances of the same jobs and not between different jobs.

user2133558
  • 524
  • 2
  • 10
  • 29

1 Answers1

0

For those interested, what i managed to do is to fusion both jobs in a single one but with two different triggers pointing to the same job. Each trigger has its own time for firing and is the parametrs now are being held in each trogger's data map and not the job data map. Also the misfiring policy has been changed to MisfireHandlingInstructionFireAndProceed

This is the code:

public class QuartzTest {

    public static final String PROCESS_TRIGGER_MAP_KEY = "process";

    public static void main( String[] args ) throws SchedulerException, InterruptedException {
    SchedulerFactory schedulerFactory = new StdSchedulerFactory();
    Scheduler scheduler = schedulerFactory.getScheduler();
    scheduler.start();

    JobDetail job1 = newJob( TestJob.class ).withIdentity( "job1", "group1" ).build();
    CronTrigger trigger1 = newTrigger().withIdentity( "trigger1", "group1" ).startAt( new Date() ).withSchedule( cronSchedule( getCronExpression( 1 ) ).withMisfireHandlingInstructionFireAndProceed() ).build();
    trigger1.getJobDataMap().put( PROCESS_TRIGGER_MAP_KEY, new MessagePrinter() {

        @Override
        public void print() {
        System.out.println( new Timestamp( System.currentTimeMillis() ) + " This is process 1" );
        }
    } );
    scheduler.scheduleJob( job1, trigger1 );

    CronTrigger trigger2 = newTrigger().withIdentity( "trigger2", "group1" ).forJob( job1 ).startAt( new Date() ).withSchedule( cronSchedule( getCronExpression( 2 ) ).withMisfireHandlingInstructionFireAndProceed() ).build();
    trigger2.getJobDataMap().put( PROCESS_TRIGGER_MAP_KEY, new MessagePrinter() {

        @Override
        public void print() {
        System.out.println( new Timestamp( System.currentTimeMillis() ) + " This is process 2" );
        }
    } );
    scheduler.scheduleJob( trigger2 );

    Thread.sleep( 5 * 60 * 1000 );
    }

    private static String getCronExpression( int interval ) {
    return "0 */" + interval + " * * * ?";

    }

}

Here is the job class

@DisallowConcurrentExecution
public class TestJob implements Job {

    @Override
    public void execute( JobExecutionContext context ) throws JobExecutionException {
    MessagePrinter mp = (MessagePrinter) context.getTrigger().getJobDataMap().get( QuartzTest.PROCESS_TRIGGER_MAP_KEY );
    if ( mp != null ) {
        mp.print();
    } else {
        System.out.println( new Timestamp( System.currentTimeMillis() ) + " Job started" );
    }
    System.out.println( new Timestamp( System.currentTimeMillis() ) + " Job sleeping 10s..." );
    try {
        Thread.sleep( 10 * 1000 );
    } catch ( InterruptedException e ) {
        e.printStackTrace();
    }
    System.out.println( new Timestamp( System.currentTimeMillis() ) + " Job finished." );
    }

}

And the processor class:

public abstract class MessagePrinter {
    public MessagePrinter() {

    }

    public abstract void print();

}
user2133558
  • 524
  • 2
  • 10
  • 29