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

Gnanam picture Gnanam · Sep 4, 2010 · Viewed 11k times · Source

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

Answer

Vivien Barousse picture Vivien Barousse · Sep 4, 2010

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