[JAVA] Creating a batch of Liferay

Introduction

Batch processing that runs on a regular basis is common in systems. Quartz is a well-known Java scheduler library. quartz:http://www.quartz-scheduler.org/

Liferay's scheduler also uses Quaryz.

Here's a quick summary of how to do it in Liferay.

Quartz Cron configuration format

image.png

Source: http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html

Quartz Cron configuration sample

image.png

Source: http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html

Liferay batch implementation Required methods

--activate: OSGi module activation process --deactivate: OSGi module stop processing --receive: business logic

sample

TestBatch.java


package com.test.batch;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.messaging.DestinationNames;
import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;
import com.liferay.portal.kernel.scheduler.SchedulerEngineHelper;
import com.liferay.portal.kernel.scheduler.SchedulerEntryImpl;
import com.liferay.portal.kernel.scheduler.TimeUnit;
import com.liferay.portal.kernel.scheduler.Trigger;
import com.liferay.portal.kernel.scheduler.TriggerFactoryUtil;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;

@Component(immediate = true, property = {
}, service =TestBatch.class)
public class TestBatch implements MessageListener {

	private static Log _log = LogFactoryUtil.getLog(TestBatch.class);

	//Batch startup interval
	private static int INTERVAL_TIME = 15;

	@Override
	public void receive(Message message) throws MessageListenerException {
		
		_log.info("Batch start");
		
		// do something
		
		_log.info("Batch end");
	}
	
	@Activate
	@Modified
	protected void activate() {
		String batchClassName = this.getClass().getName();
        //Trigger settings
		Trigger trigger = TriggerFactoryUtil.createTrigger(batchClassName, batchClassName, INTERVAL_TIME, TimeUnit.MINUTE);
		SchedulerEntryImpl schedulerEntryImpl = new SchedulerEntryImpl(batchClassName, trigger);

		_schedulerEngineHelper.register(this, schedulerEntryImpl, DestinationNames.SCHEDULER_DISPATCH);

		_log.info("Batch is activated.");
	}

	@Deactivate
	protected void deactivate() {
		_schedulerEngineHelper.unregister(this);
	}

	@Reference(unbind = "-")
	private volatile SchedulerEngineHelper _schedulerEngineHelper;

}

Creating a key trigger

image.png It is provided in the TriggerFactoryUtil class, so you can easily create it.

CronTrigger: http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html Reference article: https://portal.liferay.dev/docs/7-1/tutorials/-/knowledge_base/t/message-listeners

that's all

Recommended Posts

Creating a batch of Liferay
Rails Basics of creating a new application
Creating a local repository
Creating a test case
Creating a Scala custom ExecutionContext
Creating a calendar using Ruby
[Rails] Creating a search box
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
A brief explanation of commitAllowingStateLoss
Dummy Factory, a large data batch creation tool for Liferay 7 / DXP
Creating a mixed conditional expression of Rails if statement and unless
Think of a Java update strategy
[Rails] Creating a new project with rails new
A list of rawValues for UITextContentType.
Creating a timer app with a muddy
Sort a List of Java objects
Run a batch file from Java
A memorandum of the FizzBuzz problem
A memorandum of personal phpenv install
A brief description of JAVA dependencies
Docker [Batch deletion of <none> tags]
History of a certain in-house SE
A little understanding of lambda expressions
A story about creating a service that tells a story of a portfolio by developing alone