Asset Publisher

Expert Insights Hub

Stay connected with us to discover what top tech minds think about cutting-edge trends, development platforms, team hiring, automation, and innovations shaping the industry. Gain valuable knowledge from industry leaders, explore unique strategies, and stay ahead in the ever-evolving world of technology. Stay informed, stay inspired, and stay ahead of the competition with our exclusive updates!

Blogs

Cron Job Scheduling in Liferay DXP 7.4 : Best Practices and Tips

Caption

Introduction: 

  • The Job Scheduler is a powerful framework built on Liferay's scheduler engine, allowing you to run and schedule business logic seamlessly. A cron job scheduler is a vital feature in any application for automating tasks at specified intervals. 
  • This framework uses the DispatchTaskExecutor interface to create templates with custom logic, which you can use to set up tasks through the Control Panel. After creating a task with the Job Scheduler, you can adjust its settings and schedule when it should run.  
  • cron jobs are often used to perform maintenance tasks, email notifications, clean-up operations, or other background processes. 

Prerequisite: 

  • JDK 11 
  • Liferay Server: liferay-dxp-tomcat-2024.q4.0 
  • Liferay Developer Studio:  3.9.7.202207131011-ga8 

Cron Job Scheduling Workflow in Liferay: 
The cron job configuration process involves several steps that work together to schedule, execute, and track the execution of tasks. Below is a detailed explanation of each step: 

1. Scheduler Framework: 
Objective: Use the Quartz Scheduler to create a trigger based on the cron expression. 
The Quartz Scheduler is a popular and powerful job scheduling library used in Liferay to handle cron jobs. It allows for highly configurable job scheduling. When the cron expression is parsed, a trigger is created that determines when the job should run. 

2. Cron Job Configuration: 
Objective: Define the cron expression and store it in an OSGi component or configuration file. 
A cron expression is a string that represents a time schedule. It is a highly flexible and concise way to define when a job should run 
Example:

  • 0 0 12 * * ? — This means the job will run at 12:00 PM (noon) every day. 
  • 0 0 0 1 * ? — This runs the job at midnight on the first day of each month. 

The cron expression is typically stored in a configuration file or directly in an OSGi service or component in Liferay. These configurations are loaded when the Liferay server starts or can be updated through the Control Panel or custom configurations. 

3. Job Executor:

Objective: Execute the job once the trigger fires. 

  • The Job Executor is a critical component in the scheduled job process. Once the cron expression is processed, and the scheduled trigger fires at the specified time, it is the Job Executor's responsibility to carry out the actual execution of the task. This is where the core logic of the scheduled task is executed. 

  • BaseDispatchTaskExecutor, which is the base class provided by Liferay to create custom task executors. When the scheduled trigger fires, Liferay will invoke the doExecute method. 

4.OSGi Scheduler Service: 

  • Contains the task execution logic, defined as an OSGi service

  • Includes the business logic for the scheduled task, such as database interactions, processing data, and other operations.

  • Business Logic: This step involves performing the core task of the scheduled job. It could involve a variety of operations, such as querying or updating a database, sending emails, performing calculations, processing data, or other application-specific tasks. 

5. Task Logic/Business Logic: 

Objective: Perform the business logic of the job. 

6. Logs:

  • Logs the task execution details such as success/failure, execution time, messages, and errors. 
  • Provides insights into task performance and debugging information. 

7. Database: 

Objective: Persist the task execution results for future reference and auditing. 

1. Persistence of Results: 

  • After the scheduled job (cron task) is executed, it’s important to store the execution results for reference and tracking. This involves saving key details about each execution, such as: 
  • Task Name: The name of the task being executed. This helps in identifying what the task is doing (e.g., "Daily Report Generation" or "Data Backup"). 
  • Status: The status of the task after execution, indicating whether it was successful, failed, or encountered any issues. 
  • Execution Time: The date and time when the task was executed are crucial for tracking the timing and ensuring that tasks run at the correct scheduled intervals. 
  • Error Messages (if any): If the task encounters an error during execution, these error messages or exceptions need to be logged. This helps identify the root cause of any failures and is crucial for debugging.

Steps to Set Up a Cron Job Scheduler in Liferay: 

Follow these detailed steps to create and set up a Cron Job Scheduler in Liferay. 

1. Create a Liferay Workspace Project

  • Open Liferay Developer Studio and start the Liferay Server. 
  • Go to the File menu, select New, and choose Liferay Workspace Project
  • Enter your desired project name (e.g., liferay-cron-job), then click Finish

2. Create a Liferay Module Project 

  • Navigate to File New Liferay Module Project. 
  • Name the project (e.g., liferay-cron-jobs-scheduler). 

2.1 Select Project Template: 

  • Choose the template as mvc-portlet

2.2 Click Next and proceed with the project setup. 
2.3 Enter the Portlet Class and Package Name. Then click Finish to create the module. 

3. Create a Java Class for the Cron Job Logic

  • Right-click the package name in the project and select New → Java Class (e.g., ScheduledJobTaskExecutor). 
  • In the newly created Java class, add the following properties:


 

4. Implement Job Logic 

  • Extend BaseDispatchTaskExecutor to Implement Custom Task Logic: By extending BaseDispatchTaskExecutor, you can create custom logic for your cron job, enabling it to be scheduled, executed, and tracked within Liferay's task scheduler framework. 

  • Override Methods: 
    Implement the following required methods: 
    (a). doExecute(): This is the method where the main logic of the job should be written. When the job is triggered, this method will be executed, and your specific task logic should go inside this method. 
    (b). getName(): This method should return the name or identifier for your task or job. This is useful for identifying which task is being executed or scheduled. It can return a simple string representing the name of the job.

Example:

 

5. Deploy the Portlet 

  • After adding your logic, deploy the portlet to the Liferay server. 

6. Log in to the Liferay Portal

  • Open your browser and log in to the Liferay Portal.
  • Navigate to the Global Menu → Control Panel → Configuration → Job Scheduler.

7. Access Job Scheduler

  • Navigate to the Global Menu → Control Panel → Configuration → Job Scheduler.
     

8. Configure the Cron Job Scheduler 

  • In the Job Scheduler interface, click the + icon to add a new scheduler.

  • Select Scheduled Job Task Executor from the available options.

  • Enter the job name and click Save.


 

9. Test the Cron Job

  • Click the Run Now button to execute your job immediately.

  • You should see a confirmation message indicating that the job has executed successfully. 

10. Manage Your Cron Job 

You can now manage the scheduled job by modifying its configuration, schedule, and other settings to meet your needs. 

10.1 Adjust Time Zone: 

  •  In the Time Zone field, select the desired region to ensure the job runs at the correct time in your preferred time zone. 

10.2 Customize Cron Expression: 

  • In the Cron Expression field, you can enter a specific cron expression that defines when and how often the job should be triggered (e.g., daily, weekly, or specific times). 
  • Optionally, set an End Date and End Time to determine when the job should stop running, allowing for precise control over its lifecycle.

10.3 Define End Date and Time:

  • Optionally, set an End Date and End Time to determine when the job should stop running, allowing for precise control over its lifecycle.

11. Monitor Job Logs 

  • Click on your job to view its details. 

  • Go to the Logs tab to monitor the output and status of the scheduled job.

If you have any query, click here
Blogs