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.
Source: http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html
Source: http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html
--activate: OSGi module activation process --deactivate: OSGi module stop processing --receive: business logic
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;
}
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