views:

24

answers:

1

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

+2  A: 

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();
    }
}
Vivien Barousse
@Vivien: Thanks, am able to see my cronexpression back. BTW, a small correction in your code sample: `Cron**T**rigger cronTrigger = (CronTrigger) trigger;`.
Gnanam
@Gnanam: Thanks, I corrected that :-)
Vivien Barousse