Reputation: 5565
I have been trying to implement a scheduler job in Liferay. My controller looks like shown below.
public class MyController {
@RenderMapping
public String defaultView() {
String cron = "0 0 11 1/1 * ? *";
Date dt = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(dt);
calendar.add(Calendar.MINUTE, 2);
Trigger trigger = null;
try {
trigger = TriggerFactoryUtil.buildTrigger(TriggerType.CRON, Scheduler.class.getName(), Scheduler.class.getName(), calendar.getTime(), null, cron);
} catch (SchedulerException e) {
e.printStackTrace();
}
Portlet portlet = PortletLocalServiceUtil.getPortletById("portlet_id");
Message message = new Message();
message.put("CONTEXT_PATH", portlet.getContextPath());
message.put(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME, Scheduler.class.getName());
message.put(SchedulerEngine.PORTLET_ID, portlet.getPortletId());
Scheduler scheduler = new Scheduler();
MessageBusUtil.registerMessageListener(DestinationNames.SCHEDULER_DISPATCH, scheduler);
try {
SchedulerEngineHelperUtil.schedule( trigger, StorageType.PERSISTED, "", DestinationNames.SCHEDULER_DISPATCH, message, 5);
} catch (SchedulerException e) {
e.printStackTrace();
}
return "view";
}
}
The Scheduler
class is defined as follows
public class Scheduler implements MessageListener {
@Override
public void receive(Message message) throws MessageListenerException {
myMethodAtScheduledTime();
}
public void myMethodAtScheduledTime() {
System.out.println("Invoked at " + new Date());
}
}
I did not provide the cron time in liferay-portlet.xml
as I need it to be configured by the user. So for testing, I set the cron time to trigger at a 11.00 every day.
But, even though I set my cron time to 11.00am, the job starts the next second itself (even before 11.00) and it never ends. It keeps on getting invoked every 10-20 seconds. I want to restrict the job invocation exactly to 11.00 and it should run only once. What is the mistake am I doing here?
Upvotes: 0
Views: 1433
Reputation: 1
Also this url, can get more details about the scheduler
http://liferaytutorial.blogspot.in/2014/07/creating-scheduler-dynamically-in.html
Upvotes: 0
Reputation: 5565
I have fixed the issue. I am not really sure where the mistake was. But when I replaced the following code instead of the above controller code, I was able to trigger the scheduler at the exact time.
public class MyController {
@RenderMapping
public String defaultView() {
try {
String cron = "0 0 11 1/1 * ? *";
String description = "";
String destinationName = DestinationNames.SCHEDULER_DISPATCH;
int exceptionsMaxSize = 0;
String portletId = "portlet_id";
Message message = new Message();
message.put(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME, Scheduler.class.getName());
message.put(SchedulerEngine.PORTLET_ID, portletId);
Trigger trigger = new CronTrigger(Scheduler.class.getName(), Scheduler.class.getName(), cron);
SchedulerEngineHelperUtil.schedule(trigger, StorageType.PERSISTED, description, destinationName, message, exceptionsMaxSize);
} catch (SchedulerException e) {
e.printStackTrace();
}
return "view";
}
}
The issue should be either that I was making the wrong Trigger
object or a wrong invocation of registerMessageListener
method.
Upvotes: 1