10

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

Zefira
  • 4,329
  • 3
  • 25
  • 31
Gnanam
  • 10,613
  • 19
  • 54
  • 72

1 Answers1

21

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
  • 20,555
  • 2
  • 63
  • 64
  • Thanks, am able to see my cronexpression back. BTW, a small correction in your code sample: `Cron**T**rigger cronTrigger = (CronTrigger) trigger;`. – Gnanam Sep 04 '10 at 09:37