Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample Scheduler Portlet
package com.liferay.support.scheduler;
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.portlet.LiferayPortletConfig;
import com.liferay.portal.kernel.scheduler.CronTrigger;
import com.liferay.portal.kernel.scheduler.SchedulerEngine;
import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
import com.liferay.portal.kernel.scheduler.SchedulerEntry;
import com.liferay.portal.kernel.scheduler.SchedulerEntryImpl;
import com.liferay.portal.kernel.scheduler.SchedulerException;
import com.liferay.portal.kernel.scheduler.StorageType;
import com.liferay.portal.kernel.scheduler.Trigger;
import com.liferay.portal.kernel.scheduler.TriggerType;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
/**
* @author lcsontos
*/
public class SchedulerPortlet extends GenericPortlet {
@Override
public void destroy() {
// Scheduler #2
try {
SchedulerEngineUtil.unschedule(
_JOB_NAME, _GROUP_NAME, StorageType.PERSISTED);
_log.info(_JOB_NAME + " unscheduled for " + _portletId);
}
catch (SchedulerException se) {
_log.error(se);
}
// Scheduler #1
try {
SchedulerEngineUtil.unschedule(
_schedulerEntry, StorageType.PERSISTED);
_log.info(_schedulerEntry + " unscheduled for " + _portletId);
}
catch (SchedulerException se) {
_log.error(se);
}
super.destroy();
}
@Override
public void init() throws PortletException {
super.init();
LiferayPortletConfig portletConfig =
(LiferayPortletConfig)getPortletConfig();
_portletId = portletConfig.getPortletId();
// Scheduler #1
_schedulerEntry = new SchedulerEntryImpl();
_schedulerEntry.setEventListenerClass(MessageListener1.class.getName());
_schedulerEntry.setTriggerType(TriggerType.CRON);
_schedulerEntry.setTriggerValue(_CRON_PATTERN);
try {
SchedulerEngineUtil.schedule(
_schedulerEntry, StorageType.PERSISTED, _portletId, 0);
_log.info(_schedulerEntry + " scheduled for " + _portletId);
}
catch (SchedulerException se) {
_log.error(se);
}
// Scheduler #2
Message message = new Message();
message.put(
SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME,
MessageListener2.class.getName());
message.put(SchedulerEngine.PORTLET_ID, _portletId);
Trigger trigger = new CronTrigger(
_JOB_NAME, _GROUP_NAME, _CRON_PATTERN);
try {
SchedulerEngineUtil.schedule(
trigger, StorageType.PERSISTED, "Test Schedule",
DestinationNames.SCHEDULER_DISPATCH, message, 0);
_log.info(_JOB_NAME + " scheduled for " + _portletId);
}
catch (SchedulerException se) {
_log.error(se);
}
}
private static Log _log = LogFactoryUtil.getLog(SchedulerPortlet.class);
private static final String _CRON_PATTERN = "0/10 * * ? * *";
private static final String _GROUP_NAME = "The Group Name";
private static final String _JOB_NAME = "The Job Name";
private static String _portletId;
private static SchedulerEntry _schedulerEntry;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment