Running a scheduled task in Liferay 6.2 ga6

早过忘川 提交于 2020-01-07 02:54:09

问题


I want to know how I can do one thing in liferay. For example I have a portlet, where user can set scheduling for the task (for example do somethind every week, or once at the scheduled time). I found this tutorial, but I want to configure trigger every time user changes his preferences in portlet. In other word I want to config trigger directly from my controller, not from xml config. Can someone tell me how I can do it?


回答1:


The following code creates a scheduled task.

String pId = "portlet id here";  // TODO put portlet ID here

Message msg = new Message();
msg.put(SchedulerEngine.PORTLET_ID, pId);
msg.put(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME, MyListener.class.getName());

Trigger listener = new IntervalTrigger(MyListener.class.getName(), MyListener.class.getName(), 50);
SchedulerEngineHelperUtil.schedule(
    listener, StorageType.PERSISTED, "some description",
    DestinationNames.SCHEDULER_DISPATCH, msg, 0);

The referenced MyListener has to implement com.liferay.portal.kernel.messaging.MessageListener and will be called automatically.

The other option for creating a "static" schedule would be to add a scheduler to the liferay-portlet.xml.

<scheduler-entry>
   <scheduler-event-listener-class>de.test.MyListener</scheduler-event-listener-class>
   <trigger>
      <simple>
         <simple-trigger-value>20</simple-trigger-value>
         <time-unit>minute</time-unit>
      </simple>
   </trigger>
</scheduler-entry>

The above triggers every 20 minutes. As an alternative you can use a cron trigger. As the name says, this uses cron syntax, (see http://www.cronmaker.com/):

<trigger>
   <cron>
      <cron-trigger-value>0 0 6 ? * MON *</cron-trigger-value>
   </cron>
</trigger>


来源:https://stackoverflow.com/questions/35194627/running-a-scheduled-task-in-liferay-6-2-ga6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!