Quick-tech: creating a CronTrigger using a properties file

This post concerns how to set up a Quartz CronTrigger using a properties file. (If you are unsure what Quartz is, take a look at this O’Reilly article)

A recent project I was on needed to set up a job to run at periodic time intervals using Quartz. Quartz has some how-tos on programmatically setting up Triggers and some info on using properties files to set up Quartz, but none that merges the two. So I thought I would make one, seeing that there are always more people searching for info like this😉

Basically, what I wanted to achieve was to be able to configure the time intervals without resorting to changing the code. You can follow the tutorials on how to set up Quartz using quartz.properties – I am not going into that. What I did was creating a new file, configuration.properties, where I created some setup variables:

cronTriggerExpression=0/10 * * * * ?
otherPropFoo=bar
otherPropBar=baz
...

In your main method (or wherever you might setup the Quartz job) you will then have a bit of code resembling the following:

ResourceBundle config = ResourceBundle.getBundle("configuration");
String cronTriggerExpression = config.getString("cronTriggerExpression");
CronTrigger trigger = new CronTrigger();
trigger.setCronExpression(cronTriggerExpression);
trigger.setName("CronTrigger");

That’s basically it! You can now proceed to schedule a job using this Trigger and be assured that it will run at the predefined intervals:
sched.scheduleJob(jobDetail, trigger);

Legg igjen en kommentar

Fyll inn i feltene under, eller klikk på et ikon for å logge inn:

WordPress.com-logo

Du kommenterer med bruk av din WordPress.com konto. Logg ut / Endre )

Twitter picture

Du kommenterer med bruk av din Twitter konto. Logg ut / Endre )

Facebookbilde

Du kommenterer med bruk av din Facebook konto. Logg ut / Endre )

Google+ photo

Du kommenterer med bruk av din Google+ konto. Logg ut / Endre )

Kobler til %s

%d bloggers like this: