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

12,412

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

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.

Here is a code sample:

Trigger[] triggers = // ... (getTriggersOfJob)
for (Trigger trigger : triggers) {
    if (trigger instanceof CronTrigger) {
        CronTrigger cronTrigger = (CronTrigger) trigger;
        String cronExpr = cronTrigger.getCronExpression();
    }
}
Share:
12,412
Gnanam
Author by

Gnanam

Updated on September 15, 2022

Comments

  • Gnanam
    Gnanam over 1 year

    I'm using 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.

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

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

    NOTE: Class CronTrigger extends Trigger

  • Gnanam
    Gnanam over 13 years
    Thanks, am able to see my cronexpression back. BTW, a small correction in your code sample: Cron**T**rigger cronTrigger = (CronTrigger) trigger;.