April 27, 2022
Often, there are requirements to automate certain process and execute it on regular interval. Scheduler programming helps for all such requirement. Liferay provides a flexible way to implement scheduler. In this post, I am going to talk about, steps to create scheduler in Liferay DXP/7.
Following are the prerequisite to start implementation,
Pre-requisites:
Starting point to implement Scheduler in Liferay DXP/7 is to create module in Liferay workspace.
Create module in Liferay workspace:
To create Liferay modules, Right Click → New → Liferay Module Project. Give appropriate name and click finish.
You should see project structure as follow,
Now that you have created module in Liferay development environment, next step is to create class for scheduler. In this example I created class with name ‘LiferayScheduler’ under package com.enprowess.schedulerclass.
You need to extend BaseSchedulerEntryMessageListener class. This is abstract class contains default implementation for scheduler. You need to override two methods doReceive() and activate().
doReceive() : Implement the logic to execute on regular interval in this method. Ex,
@Override
protected void doReceive(Message message) throws Exception {
log.info("Add your business logic for schedule here…");
}
activate() : In this method you need to define schedule time to execute scheduler. Ex.
@Activate
@Modified
protected void activate() {
schedulerEntryImpl.setTrigger (
TriggerFactoryUtil.createTrigger(getEventListenerClass(), getEventListenerClass(), 24, TimeUnit.HOUR));
_schedulerEngineHelper.register(this, schedulerEntryImpl, DestinationNames.SCHEDULER_DISPATCH);
}
In above example, I am setting scheduler to execute on every 24 hours. Following are the method signatures of TriggerFactoryUtil to configure scheduler time interval,
I hope this post will give you fair idea how to implement scheduler in Liferay DXP/7. Happy coding!
Blog by,
Maitrik Panchal