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
Need little more info.
What if i want to have number of jobs with different intervals to be schudeled instead writing in XML how do i schudule dynamically in web browser.
Hey Vaseem,
Have you find the way to do it?
I have exactly the same problem
Thanks