Implement Scheduler in Liferay 6.1

3 comments
Many times we want our logic to be executed at certain Date/Time .Liferay 6.X comes with in built capability of scheduler. Here is the example which can help you ..!!
1) Make below entry in Liferay-portlet.xml
<scheduler-entry>
<scheduler-description>test-scheduler</scheduler-description>
<scheduler-event-listener-class>
com.test.scheduler.UploadJob
</scheduler-event-listener-class>
<trigger>
<cron>
<cron-trigger-value>0 0 18 * * ? *</cron-trigger-value>
</cron>
</trigger>
</scheduler-entry>

Following cron expression indicates that the scheduler will run every day at 18:00 PM.
<cron-trigger-value>0 0 18 * * ? *<cron-trigger-value>

To know how to write cron expression you can visit below links.
Cron Trigger
Cron Maker

2) create scheduler class.

package com.test.scheduler;
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.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
public class UploadJob implements MessageListener
{
private static final Log LOGGER = LogFactoryUtil.getLog(UploadJob.class);
/**
* Job that we need to run on scheduler
*/
public void receive(Message arg0) throws MessageListenerException {
LOGGER.info("Scheduler----&gt; receive()");
//write your logic.
}
}


Next PostNewer Post Previous PostOlder Post Home

3 comments:

  1. Along with the basics,the links are pretty resourceful

    ReplyDelete
  2. Good to know that it helped you . Please follow this blog to get more updates.

    Thank you
    Jitendra Rajput

    ReplyDelete
  3. So I have all of this setup and working, but now I am trying to getNextFireTime displayed in my console. This works great IF my scheduler has run. Currently, I have it set to run every 10 minutes. So after a new code deployment or after a server restart, the scheduler has not run and I cannot retrieve the nextFireTime. Is there a way to call this class from another class, as to force the scheduler to run?

    ReplyDelete