A Cronjob is a scheduler where we can schedule a particular Job to be performed at a specified time or over a period of time.
The 3 parts of a Cronjob are:
For understanding each of these clearly I would take an example:
First of all you need to identify what task you CronJob need to do.
In this case lets say that your cronjob needs to send NewsletterEmail periodically with a personalized message.
You would create a NewsletterEmailCronJob item in your items.xml file having attributes for storing a personalizedMessage
<itemtype code=”NewsletterEmailCronJob” autocreate=”true” generate=”true” extends=”CronJob”
<description>Cronjob for sending Newsletter Email</description>
<attribute qualifier=”personalizedMessage” type=”java.lang.String”>
Now the above code would create the required Model class when you save it and run Ant Build script.
Job is where your business logic goes and it uses the CronJob Model you created as part of CronJob above.
Here you would create a class say NewsletterEmailJob which would extend AbstractJobPerformable and would work on NewsletterEmailCronJobModel.
You would override the perform method to write the business logic in it.
public class NewsletterEmailJob extends AbstractJobPerformable<NewsletterEmailCronJobModel>
public PerformResult perform(final NewsletterEmailCronJobModel cronJob)
….. Logic for sending News letter Email with Personalized Message ….
Also, you would define a bean for the above class in your spring.xml file as follows:
<bean id=”newsletterEmailJob” class=”com.email.NewsletterEmailJob”
Trigger defines the time event when the Job would be executed.
The trigger is linked to the Job via CronJob. You would first create a CronJob item and associate the bean id of the Job to it. Then you would create a Trigger item which contains the CronJob item reference and the time schedule for execution. The time schedule expression is written using Quartz Job Scheduler which can found at http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger
INSERT_UPDATE NewsletterEmailCronJob; code[unique=true];job(code); ;newsletterEmailCronJob;newsletterEmailJob;
INSERT_UPDATE Trigger;cronJob(code)[unique=true];active;year;month;day;hour;minute;second;maxAcceptableDelay;relative;weekInterval;daysOfWeek ;newsletterEmailCronJob;true;-1;-1;-1;-1;10;-1;1;true;