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();



Comments

  1. 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?

    ReplyDelete
    Replies
    1. What about this cron expression "0 0 11 1/1 * ? * "

      Delete
    2. I have tried this also.. Same result.

      Delete
    3. http://stackoverflow.com/questions/33755696/liferay-scheduler-not-triggering-at-the-given-time

      Delete
    4. Jus now i have tested, Can you try below snipets,



      try {
      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());

      }

      }

      Delete
    5. Also are you facing the same for different crontime also? i mean every hour or every second?

      I have tested the above code for every second.

      Delete
    6. Thanks a lot for helping me out. That worked. :-) Hope others can also make use of this code.

      Delete
    7. You're welcome, sounds goods it helped you.

      Delete
  2. when i am using other cron string it is not executed.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Can we set Dynamic Scheduler Based on entity(Alert Date) changed at that time?

    ReplyDelete

Post a Comment

Popular posts from this blog

How to know which liferay version we are using

Viewing the SQL Query in liferay : debugging the SQL Query in Hibernate

Opening portlet in popup