【问题标题】:Can you run two different Quartz job instances sequentially?你可以顺序运行两个不同的 Quartz 作业实例吗?
【发布时间】:2017-03-01 16:17:06
【问题描述】:

您好,我有一个 Job 1 触发,每分钟触发一次, Job 2 触发触发,每 5 分钟触发一次。因此,每隔五分钟,两个作业将同时运行,我想避免这种情况并强制第二个作业触发以等待另一个作业完成后再开始。我见过@DisallowConcurrentExecution,但这只会避免相同作业的两个实例的并行运行,而不是不同作业之间的并行运行。

【问题讨论】:

    标签: java quartz


    【解决方案1】:

    对于那些感兴趣的人,我设法将两个工作融合为一个工作,但有两个不同的触发器指向同一个工作。每个触发器都有自己的触发时间,并且参数现在保存在每个触发器的数据映射中,而不是作业数据映射中。此外,误击策略已更改为 MisfireHandlingInstructionFireAndProceed

    这是代码:

    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 + " * * * ?";
    
        }
    
    }
    

    这里是工作类

    @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." );
        }
    
    }
    

    处理器类:

    public abstract class MessagePrinter {
        public MessagePrinter() {
    
        }
    
        public abstract void print();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多