• Liferay Quartz Scheduler

    Posted on December 22, 2014 by Hamidul Islam in Liferay.

    Quartz Scheduler


    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.


     Creating Quartz Scheduler in Liferay

    quartz scheduler

    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
    	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. 


    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. 


    Cron Expression: 

    We can also use CRON expression in liferay-portlet.xml file. Below is the sample example 

    			<cron-trigger-value>0 30 06 ? * MON-FRI</cron-trigger-value>

    According to the above expression the task will be executed in daily basic from Monday to Friday. For more CRON expression 


    Download Source Code

    Post Tagged with ,

2 Responses so far.

  1. Vaseem says:

    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.

  2. Kamilia says:

    Hey Vaseem,
    Have you find the way to do it?
    I have exactly the same problem

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.