Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 995
Gnanam Avatar asked Sep 04 '10 09:09

Gnanam


1 Answers

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();
    }
}
like image 124
Vivien Barousse Avatar answered Oct 15 '22 13:10

Vivien Barousse