In order to create a scheduler,the entry needs to be created in
liferay-portlet.xml
provding scheduler class and trigger value for timing of scheduler triggering
<portlet-name>GetSetGo</portlet-name>
<icon>/icon.png</icon>
<scheduler-entry>
<scheduler-description>This scheduler logs User count from portal</scheduler-description>
<scheduler-event-listener-class>com.example.scheduler.SchedulerSample</scheduler-event-listener-class>
<trigger>
<simple>
<simple-trigger-value>
5
</simple-trigger-value>
<time-unit>minute</time-unit>
</simple>
</trigger>
</scheduler-entry>
The given entry provides
Scheduler description
Class name,which implements MessageListener class
Trigger,which provides intervals for defining trigger point for scheduler
-Using Cron
-Using Simple trigger value
In the given example,the scheduler will trigger after every 5 mins.
Next up we need to create scheduler class
package com.example.scheduler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
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.service.UserLocalServiceUtil;
public class SchedulerSample implements MessageListener {
@Override
public void receive(Message arg0) throws MessageListenerException {
Log log=LogFactoryUtil.getLog(SchedulerSample.class);
try {
log.info("User Count for portal:"+UserLocalServiceUtil.getUsersCount());
} catch (SystemException e) {
log.info("User count is currently unavailable");
}
}
}
This scheduler simply displays output portal user count after every trigger interval to server console.