Liferay

Scheduler in Liferay 7/DXP

Bhargav Vaghasiya
Bhargav VaghasiyaJan 28, 2020
Scheduler in Liferay 7/DXP

Introduction

Liferay provides an easy way to implement scheduler. Scheduler is a very important aspect of software development these days. Using Liferay Scheduler we can automate certain processes and execute it on every specified time interval. i.e send an email after every day, send monthly newsletters, etc.

Prerequisites

  • JDK 8
  • Liferay portal 7/7.x

Assuming that you have already created a Liferay module project in eclipse IDE.

1) Create scheduler class

1@Component(
2	immediate = true,
3	property = {
4		"cron.expression= 0 0/05 1/1 1/1 * ?"   // scheduler runs every 5 min.
5	},
6	service = EmailSchedulear.class
7)
8public class EmailSchedulear extends BaseMessageListener {
9
10}

Here, you can specify your cron expression in ‘cron.expression’ property.
Cron expressions are based on the timezone of your server. You can learn more about corn expression here https://www.freeformatter.com/cron-expression-generator-quartz.html

2) Override the doReceive method in your scheduler class

This method will be called periodically based on your cron expression. You can write your business logic here which you want to execute periodically. i.e. you can write email sending and newsletter sending code here.

1@Override
2protected void doReceive(Message message) throws Exception {
3	log.info("Scheduled task executed...");
4}

3) Register The scheduler

1@Activate
2@Modified
3protected void activate(Map<String, Object> properties) throws SchedulerException {
4
5	try {
6		String cronExpression = GetterUtil.getString(properties.get("cron.expression"), "cronExpression");
7		log.info(" cronExpression: " + cronExpression);
8
9		String listenerClass = getClass().getName();
10		Trigger jobTrigger = TriggerFactoryUtil.createTrigger(listenerClass, listenerClass, new Date(), null, cronExpression);
11
12		SchedulerEntryImpl schedulerEntryImpl = new SchedulerEntryImpl();
13		schedulerEntryImpl.setEventListenerClass(listenerClass);
14		schedulerEntryImpl.setTrigger(jobTrigger);
15
16		SchedulerEngineHelperUtil.register(this, schedulerEntryImpl, DestinationNames.SCHEDULER_DISPATCH);
17
18	} catch (Exception e) {
19		log.error(e);
20	}
21}

4) Deactivate the scheduler

1@Deactivate
2protected void deactive() {
3	SchedulerEngineHelperUtil.unregister(this);
4}

© 2026 IGNEK. All rights reserved.

Ignek on LinkedInIgnek on InstagramIgnek on FacebookIgnek on YouTubeIgnek on X