java 如何获得给定作业名称和组名称的 cron 表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3641575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:47:30  来源:igfitidea点击:

How to get cron expression given job name and group name?

javacronquartz-schedulerschedulercrontrigger

提问by Gnanam

I'm using Quartz Scheduler v.1.8.0.

我正在使用 Quartz Scheduler v.1.8.0。

How do I get the cron expression which was assigned/attached to a Job and scheduled using CronTrigger? I have the job name and group name in this case. Though many Triggers can point to the same Job, in my case it is only one.

如何获取分配/附加到作业并使用CronTrigger调度的 cron 表达式?在这种情况下,我有作业名称和组名称。尽管许多触发器可以指向同一个 Job,但在我的情况下它只有一个。

There is a method available in Scheduler class, Scheduler.getTriggersOfJob(jobName, groupName), but it returns only Triggerarray.

Scheduler 类中有一个可用的方法,Scheduler.getTriggersOfJob(jobName, groupName),但它只返回Trigger数组。

Example cronexpression: 0 /5 10-20 * * ?

示例 cron 表达式: 0 /5 10-20 * * ?

NOTE:Class CronTriggerextends Trigger

注意:CronTrigger类扩展了触发器

回答by Vivien Barousse

You can use Scheduler.getTriggerOfJob. This class returns all triggers for a given jobName and groupName, in a Trigger[].

您可以使用 Scheduler.getTriggerOfJob。此类在 Trigger[] 中返回给定 jobName 和 groupName 的所有触发器。

Then, analyse the content of this array, test if the Trigger is a CronTrigger, and cast it to get the CronTrigger instance. Then, the getCronExpression() method should return what you are looking for.

然后,分析这个数组的内容,测试Trigger是否是CronTrigger,并强制转换得到CronTrigger实例。然后, getCronExpression() 方法应该返回您要查找的内容。

Here is a code sample:

这是一个代码示例:

Trigger[] triggers = // ... (getTriggersOfJob)
for (Trigger trigger : triggers) {
    if (trigger instanceof CronTrigger) {
        CronTrigger cronTrigger = (CronTrigger) trigger;
        String cronExpr = cronTrigger.getCronExpression();
    }
}