Creating the scheduler dynamically in liferay
We can create the scheduler by defining the entries in the liferay-portlet.xml with specfying the only particular entries. what if scheduler what to change based on the user input.
so here is the code,
String cron = "0 0/1 * 1/1 * ? *";
Portlet portlet = PortletLocalServiceUtil.getPortletById("new_WAR_UserSearchportlet");
Trigger trigger = null;
try {
trigger = TriggerFactoryUtil.buildTrigger(TriggerType.CRON, Scheduler.class.getName(), Scheduler.class.getName(), new Date(), null, cron);
} catch (SchedulerException e) {
e.printStackTrace();
}
Message message = new Message();
message.put(SchedulerEngine.CONTEXT_PATH, portlet.getContextPath());
message.put(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME, Scheduler.class.getName());
message.put(SchedulerEngine.PORTLET_ID, portlet.getPortletId());
try {
SchedulerEngineUtil.schedule(trigger, StorageType.PERSISTED, "", "liferay/scheduler_dispatch", message, 5);
} catch (SchedulerException e) {
e.printStackTrace();
}
Here we have cron which we be trigerred for every second. ie. String cron = "0 0/1 * 1/1 * ? *";
Creating the cron trigger with scheduler class
TriggerFactoryUtil.buildTrigger(TriggerType.CRON,Scheduler.class.getName(), Scheduler.class.getName(), new Date(), null, cron);
public class Scheduler implements MessageListener {
@Override
public void receive(Message message) throws MessageListenerException {
System.out.println("In Message Listener");
}
Finally the last statement is scheduling. SchedulerEngineUtil.schedule(-,-,-);
We can also delete, pause and resume
SchedulerEngineUtil.delete();
SchedulerEngineUtil.pause();
SchedulerEngineUtil.resume();
I had the above scenario (to change scheduling time based on the user input). So I implemented the above code with cron time set to "0 0 11 ? * * *". But the scheduler starts before 11. And it keeps on invoking every 10-20 seconds when it should be invoked only once in a day. What could be the issue?
ReplyDeleteWhat about this cron expression "0 0 11 1/1 * ? * "
DeleteI have tried this also.. Same result.
Deletehttp://stackoverflow.com/questions/33755696/liferay-scheduler-not-triggering-at-the-given-time
DeleteJus now i have tested, Can you try below snipets,
Deletetry {
String cronText = "0 0/1 * 1/1 * ? *";
String description = SchedulerConstant.DESCRIPTION;
String destinationName = SchedulerConstant.DESTINATION_NAME;
int exceptionsMaxSize = SchedulerConstant.EXCEPTION_MAX_SIZE;
String portletId = PortalUtil.getPortletId(arg0);
Message message = new Message();
message.put(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME,SchedulerConstant.JOB_NAME);
message.put(SchedulerEngine.PORTLET_ID, portletId);
Trigger trigger = new CronTrigger(SchedulerConstant.JOB_NAME, SchedulerConstant.GROUP_NAME, cronText);
SchedulerEngineHelperUtil.schedule(trigger,SchedulerConstant.STORE_TYPE, description, destinationName, message, exceptionsMaxSize);
System.out.println("in end of the render");
}catch (SchedulerException e) {
e.printStackTrace();
}
===============================================================
public class SchedulerConstant {
public static final String JOB_NAME = TestSchedulerListener.class.getName();
public static final String GROUP_NAME = TestSchedulerListener.class.getName();
public static final String DESTINATION_NAME = DestinationNames.SCHEDULER_DISPATCH;
public static final int EXCEPTION_MAX_SIZE = 0;
public static final String DESCRIPTION = "Message Scheduler Description";
public static final StorageType STORE_TYPE = StorageType.PERSISTED;
}
===========================================
public class TestSchedulerListener implements MessageListener{
@Override
public void receive(Message arg0) throws MessageListenerException {
System.out.println("Message Listener implemented"+new Date());
}
}
Also are you facing the same for different crontime also? i mean every hour or every second?
DeleteI have tested the above code for every second.
Thanks a lot for helping me out. That worked. :-) Hope others can also make use of this code.
DeleteYou're welcome, sounds goods it helped you.
Deletewhen i am using other cron string it is not executed.
ReplyDeleteWhich cron? Please provide that , i will execute from my side
Delete"0 5 15 24 11 ? *"
DeleteThis comment has been removed by the author.
DeleteAre you getting any exception?
DeleteHave you resolved this issue?
DeleteThis comment has been removed by the author.
ReplyDeleteCan we set Dynamic Scheduler Based on entity(Alert Date) changed at that time?
ReplyDelete