Liferay Quartz Scheduler
liferay quartz scheduler tutorial will give you details on “How to configure quartz jobs in liferay?”. Liferay has out of the box support of Quartz Scheduler at portlet level and multiple CRON jobs can be configured for each portlet.
High Level Steps:
- Configure Cron Jobs for portlet in liferay-portlet.xml
- Provide scheduler-event-listener-class that implements com.liferay.portal.kernel.messaging.MessageListener and override below method
-
@Override public void receive(Message arg0) throws MessageListenerException
-
- sometimes, liferay cron will not run even though nothing wrong in your implementation, but need to check what time zone server is running and change cron expression accordingly. By default, Liferay runs on GMT time zone that can be found in setenv.sh file.
There are two ways you can configure cron in liferay:
- Cron Expression
-
<scheduler-entry> <scheduler-description>Job at 10'c cloccl</scheduler-description> <scheduler-event-listener-class>org.javasavvy.LiferayCronTutorial</scheduler-event-listener-class> <trigger> <cron> <cron-trigger-value>00 00 22 * * ? *</cron-trigger-value> </cron> </trigger> </scheduler-entry>
- cron-trigger-value can be loaded from portlet.properties file like <cron-trigger-value>liferay.cron.job1</cron-trigger-value>
- In portlet.properties file :
- liferay.cron.job1=00 00 22 * * ? *
-
- Simple Cron:
- Time unit: Valid values for this element are “day”,”hour”, “minute”, “second”, or “week”. The default value is “second”.
- Simple-trigger-value is : value
-
<trigger> <simple> <simple-trigger-value>10</simple-trigger-value> <time-unit>minute</time-unit> </simple> </trigger>
-
Lets jump into tutorial:
Step1: Set Liferay Quartz Scheduler Cron in liferay-portlet.xml
<portlet> <portlet-name>javasavvy-cron</portlet-name> <icon>/icon.png</icon> <scheduler-entry> <scheduler-description>Cron at 10 PM</scheduler-description> <scheduler-event-listener-class>org.javasavvy.LiferayCronListener</scheduler-event-listener-class> <trigger> <cron> <cron-trigger-value>00 00 22 * * ? *</cron-trigger-value> </cron> </trigger> </scheduler-entry>
Step2: Implement MessageListener class
In scheduler-event-listener-class, give LiferayCronListener.java, that implements com.liferay.portal.kernel.messaging.MessageListener and as shown in below:
public class LiferayCronListener implements MessageListener{
@Override
public void receive(Message arg0) throws MessageListenerException {
LOG.info(" State Job Started at"+new Date());
}
}
Now deploy and you configured CRON.
Sample CRON Expressions:
- Job Runs daily at 11:45 PM
- <cron-trigger-value>0 45 23 1/1 * ? *</cron-trigger-value>
- Job Runs at every 10 minutes
- <cron-trigger-value>0 0/10 1/1 1/1 * ? *</cron-trigger-value>
- Job Run for every two hours
- <cron-trigger-value>0 0 0/2 1/1 * ? *</cron-trigger-value>
Hope this helps