表达式预测到下一个工作日

表达式预测到下一个工作日

本文介绍了将 Quartz 表达式预测到下一个工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个 Quartz cron 表达式,它在每​​月的第 10 天触发如果是工作日,否则在下一天或之后的第二天触发下一个.

I need to create a Quartz cron expression that fires every 10th day of the month if it is a weekday, otherwise fires to the next day or the day after the next.

  • 例如2014 年 8 月 10 日是星期日,星期一 11 日开火
  • 例如2014 年 9 月 10 日是星期三,星期三 10 日开火
  • 例如2015 年 1 月 10 日是星期六,星期一 12 日开火

我强调了第三种情况:我尝试将 0 0 0 10W * ? 作为表达式,但它在 2015 年 1 月 8 日星期五触发.我需要稍后触发它.

I highlighted the third case: I tried 0 0 0 10W * ? as expression but it fires Friday Jan 8th 2015. I need it to be fired later.

我该如何告诉 Quartz 呢?我想做三个表达,例如如果是工作日,则为 10 号",如果是星期一,则为 11 号"和如果是星期一,则为 12 号"但我无法将月份和星期几字段结合起来(我总是得到一个无效的 cron 表达式错误)

How do I tell Quartz to? I wanted to make three expressions, e.g. "the 10th if it is a weekday", "11th if it is a monday" and "12th if it is a monday" but I can't combine day-of-month and day-of-week fields (I always get an Invalid cron expression error)

推荐答案

我不认为您可以使用简单的 cron 表达式做到这一点,但您可以使用以下解决方案.

I don't think you can do that with a simple cron expression, but you can use the following solution.

首先使用经典的 Cron 触发器安排您的作业

First schedule your Job with a classic Cron Trigger

    CronScheduleBuilder sh = cronSchedule("0 0 0 10 * ?");
    TriggerBuilder<Trigger> tb = newTrigger()
        .withSchedule(sh)
        .withIdentity("MyTrigger");

并安排您的触发器.然后创建一个 Quartz TriggerListener 来实现你的机制

And schedule your Trigger. Then create a Quartz TriggerListener that implements your mecanism

    public class MyTriggerListener implements TriggerListener {
         [...]

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

              // You check if it is your Cron Trigger
              if ("MyTrigger".equals(trigger.getKey.getName()) {
                   Calendar cal = Calendar.getInstance();

                   // If current date is sunday, then another one-time trigger
                   //   is created to delay the job firing
                   if (Calendar.SUNDAY.equals(cal.get(Calendar.DAY_OF_WEEK))) {

                        cal.add(Calendar.DATE, 1); // One day later

                        TriggerBuilder<Trigger> tb = newTrigger()
                            .startAt(cal.getTime());
                            .withIdentity("MyTrigger_delayed");
                            .forJob(context.getJobDetail());

                        // TODO schedule it in Quartz the way you want
                        // like : scheduler.scheduleJob(tb.build());

                        return true; // The current fire is vetoed
                   }

                   // If current date is saturday, then another one-time trigger
                   //   is created to delay the job firing
                   else if (Calendar.SATURDAY.equals(cal.get(Calendar.DAY_OF_WEEK))) {

                        cal.add(Calendar.DATE, 2); // Two day later

                        TriggerBuilder<Trigger> tb = newTrigger()
                            .startAt(cal.getTime());
                            .withIdentity("MyTrigger_delayed");
                            .forJob(context.getJobDetail());

                        // TODO schedule it in Quartz the way you want
                        // like : scheduler.scheduleJob(tb.build());

                        return true;// The current fire is vetoed
                   }
                   // If it is a weekday, the job fires normally
                   else {
                        return false;
                   }
              }

              // It is not your Cron Trigger
              return false;
         }
    }

此代码实现了一种机制,可以延迟您的工作触发,以便在 工作日 开始工作.您可以使用

This code implement a mecanism to delayed your job firing to start the job a weekday. You can add this Listener to your Scheduler with

        TriggerListener myTriggerListener = new MyTriggerListener("MyTriggerListener");
        scheduler.getListenerManager().addTriggerListener(myTriggerListener, allTriggers());

这篇关于将 Quartz 表达式预测到下一个工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 01:06