Liferay Job Scheduler: A Comprehensive Guide

Liferay Job Scheduler: A Comprehensive Guide

Liferay Job Scheduler: A Comprehensive Guide
lurantech-author
Yamish R. Bhalara
blog-display-date
Thu Dec 12 2024
Liferay Job Scheduler: A Comprehensive Guide

The Liferay Job Scheduler is a flexible and powerful framework built on Liferay's scheduler engine. It allows users to run and schedule business logic efficiently within the Liferay platform. By utilizing this framework, developers can create automated tasks that execute at predefined times, enabling processes like data imports, report generation, and more without manual intervention.


When to Use Job Scheduler

Job Scheduler is a great tool for automating recurring tasks that need to run periodically or at specific times. Here are some scenarios when you might want to use it:

  • Automating Data Imports/Exports: Schedule tasks to import or export data at regular intervals, such as syncing with external systems.
  • Generating Reports: Schedule tasks to generate reports daily, weekly, or monthly and send them to relevant stakeholders.
  • Periodic Cleanup: Set up jobs to clean up temporary data or perform maintenance tasks on a recurring schedule.
  • Sending Notifications: Schedule tasks that send notifications or alerts to users based on specific events or time-based conditions.

Using the Job Scheduler in Liferay

Setting up and managing tasks through the Liferay Job Scheduler is straightforward. Here are the key steps:

1. Adding a New Job Scheduler Task

To begin, you need to navigate to the Job Scheduler section in the Liferay Control Panel:

  • Open the Global Menu and go to Control Panel.
  • Navigate to Configuration → Job Scheduler.
  • Click the Add button and select a template for your task.

The selected template will define the core logic of the job. You can customize the task by setting specific properties in the settings editor, which will be injected at runtime without requiring code changes or redeployment.

2. Scheduling the Job

Once you've created the Job Scheduler task, the next step is to schedule when it will run. To activate the trigger and define its schedule:

  • Go to the Job Scheduler Triggers tab.
  • Click the desired task and go to the Job Scheduler Trigger tab.
  • Define the cron expression, start and end dates, time zone, and whether overlap is allowed.
  • Activate the trigger by saving the settings.

Code Example

Create a taskExecuter class which extends BaseDispatchTaskExecutor class and define your task inside 'doExecute' method.

@Component(
    property = {
        "dispatch.task.executor.name=customtask", "dispatch.task.executor.type=customtask"
    },
    service = DispatchTaskExecutor.class
)
public class CustomTaskExecutor extends BaseDispatchTaskExecutor{

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "customtask";
	}

	@Override
	public void doExecute(DispatchTrigger dispatchTrigger, DispatchTaskExecutorOutput dispatchTaskExecutorOutput)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("Task is being Execute ...");		
	}
}

Deploy above module in Liferay and follow the below steps:

Go to Control Panel - click on Job Scheduler.

Create a New Scheduler - click on Add New.

Select our task - in our case that would be 'customtask'.

liferay-scheduler-1

Give a name - here we give name 'My Custom Task' and click on save.

Open our task - as we save, reopen our task. Now you are able to see the tab for 'Job Scheduler Trigger'. Click on it.

liferay-scheduler-2

Active the scheduler and Set The time - use cron expression for setting up time and you can also run the job manually.

liferay-scheduler-3

Conclusion

The Liferay Job Scheduler is an essential tool for automating tasks and improving workflow efficiency. Whether you're managing data integration tasks, generating reports, or performing periodic maintenance, this tool allows you to schedule, monitor, and execute tasks with ease.

Happy coding! 😉

For any query or more information related our blog don't hesitate to Reach us out.

Ready to Work, Let's Chat
Our team of experts is ready to collaborate with
you every step of the way, from initial consultation to implementation.
Contact Us Today!