Most of the applications require the need of automated procedure to be executed at certain period of time. The automated process will perform multiple operations on the user data on a predefined set of interval.

Quartz is Job scheduling Library defined to execute system process, actions at certain interval for ex: - if notification is to be sent to mass users then quartz scheduler is the best way to implement without having manual sending notification.

In this article we will go through the Scheduler implementation in Liferay with short description to create new scheduler.

 

In Liferay 6, the design of scheduler engine has been built with the combination of JMS. Let me share the small class cum collaboration diagram, to understand the implementation in detail. 

 

 

Liferay implements JMS messaging with Quartz scheduler. The Job class for liferay is com.liferay.portal.scheduler.job.MessageSenderJob and listener class & package details are com.liferay.portal.kernel.messaging.MessageListener which is to be implemented for handling the multiple jobs.

 

Lifeary schedulerEnginUtil class will register the listener with defined destination Queue and also triggering the new schedule as defined in the SchedulerEntry with MessageSenderJob.

When the new trigger is fired then the Job class will sends message to Queue which will eventually sends to Listener.

In current implementation Liferay has provided exposure to Message which can contain some payload data if needed for job execution.

 

By default the method schedule(SchedulerEntry schedulerEntry, ClassLoader classLoader)from SchedulerEngineUtil sends null Message. So, we might need to copy and pass some payload object if needed. 

Now it’s time to understand that how to create a Scheduler to run a cron job for performing a background job on the portal.

Below are the steps for creation:-

 

1)  Create Scheduler Class by specify the path in liferay-portlet.xml

<portlet>

<portlet-name>riiteshportlet</portlet-name>

<icon>/icon.png</icon>

<scheduler-entry>

<scheduler-description>

This scheduler is used to run my own job

</scheduler-description>

<scheduler-event-listener-class>com.portlet.myportlet.trigger.MyScheduler</scheduler-event-listener-class>

  1. We can have a simple scheduler running every hour,minute,seonds by specifying it as below:-

<trigger>

<simple>

<simple-trigger-value>15</simple-trigger-value>

<time-unit>minute</time-unit>

</simple>

 

  1. This (<cron-trigger-value>0 0 0 * * ?</cron-trigger-value>) indicates that the scheduler will run every night at 00 hrs.

<cron>

<cron-trigger-value>0 0 0 * * ?</cron-trigger-value>

</cron>

</trigger>

</scheduler-entry>

</portlet>

 

 

Step 2: Now let’s create a Scheduler class.

The class name specified in the liferay-portlet.xml has to be same as scheduler class

public class MyScheduler implements MessageListener {

 public void receive(Message arg0) throws MessageListenerException {

//Here is the business logic to be written as per your requirement

System.out.println(“scheduler has been started”);

} }

 

Liferay will run the scheduler based on the entry defined in the liferay-portlet.xml.