Can you run two different Quartz job instances sequentially?

跟風遠走 提交于 2019-12-12 02:16:07

问题


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.


回答1:


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();

}


来源:https://stackoverflow.com/questions/42536771/can-you-run-two-different-quartz-job-instances-sequentially

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!