In simple Quartz Scheduler is to run offline job. Quartz Scheduler is well integrated in Liferay portal. It can be used in Liferay plugin portlet to execute any kind of back-end task in an asynchronous fashion without using the front end of the application. The scheduler can be triggered based on either specific intervals or on CRON expressions.
According to Wikipedia:
The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates or intervals.
http://en.wikipedia.org/wiki/Cron
Creating Quartz Scheduler in Liferay
1. Create a portlet:
In this step create any portlet using any framework. It may be JSP Portlet, Liferay MVC Portlet or any other portlet.
2. Create Scheduler Class:
Create a class inside the portlet which implements the interface com.liferay.portal.kernel.messaging.MessageListener. This is how we create quartz scheduler in Liferay.
3. Implement receive() method:
Inside the scheduler class implement receive() method. See the below code
package com.proliferay.demo.quartz; import com.liferay.portal.kernel.messaging.Message; import com.liferay.portal.kernel.messaging.MessageListener; import com.liferay.portal.kernel.messaging.MessageListenerException; /** * * @author Hamidul Islam * */ public class MessageListenerDemo implements MessageListener { //This method will be called periodically according to configuration @Override public void receive(Message message) throws MessageListenerException { System.out.println("This is a simple scheduler class"); } }
In the above code MessageListenerDemo is our scheduler class.
Printing default content of message object:
The message object has some default contents. If you want display the contents then write the below code inside the receive method.
Map<String, Object> map = message.getValues(); for (Map.Entry<String, Object> entry : map.entrySet()) { System.out.println(entry.getKey() + "/" + entry.getValue()); }
4. Register the scheduler
Just writing the quartz scheduler class is not enough. We need to register it in Liferay. This is done by adding an entry in liferay-portlet.xml after icon tag.
<scheduler-entry> <scheduler-event-listener-class>com.proliferay.demo.quartz.MessageListenerDemo</scheduler-event-listener-class> <trigger> <simple> <simple-trigger-value> 5 </simple-trigger-value> <time-unit>minute</time-unit> </simple> </trigger> </scheduler-entry>
Note 1: Time unit can be second, minute, hour, day and week. In the above the scheduler will run every 5 minutes.
Note 2: As shown in the above, the tag simple-trigger-value can be replaced by property-key tag to read time from properties file. For example the same can be written as bellow. Here simple-trigger-time is the key that will be red from portlet.properties file.
<trigger> <simple> <property-key>simple-trigger-time</property-key> <time-unit>second</time-unit> </simple> </trigger>
Cron Expression:
We can also use CRON expression in liferay-portlet.xml file. Below is the sample example
<scheduler-entry> <scheduler-event-listener-class>com.proliferay.demo.quartz.MessageListenerDemo</scheduler-event-listener-class> <trigger> <cron> <cron-trigger-value>0 30 06 ? * MON-FRI</cron-trigger-value> </cron> </trigger> </scheduler-entry>
According to the above expression the task will be executed in daily basic from Monday to Friday. For more CRON expression
http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger