In this small blog we will give an understanding of how can we write a Scheduler
to run a cron job for performing a background job on the portal.
Step 1 : Create a scheduler class in your portlet
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.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;
public class NewsLetterScheduler implements MessageListener {
public void receive(Message arg0) throws MessageListenerException {
System.out.println("newsletter received!!!");
/* Add your business logic here*/
}
}
Step 2 : Specify the path in the liferay-portlet.xml.
The class name specified in the liferay-portlet.xml must match with scheduler class.
<portlet>
<portlet-name>newsletter-portlet</portlet-name>
<icon>/icon.png</icon>
<scheduler-entry>
<scheduler-description>
scheduler will run on every 15 minutes
</scheduler-description>
<scheduler-event-listener-class>
<portlet-name>newsletter-portlet</portlet-name>
<icon>/icon.png</icon>
<scheduler-entry>
<scheduler-description>
scheduler will run on every 15 minutes
</scheduler-description>
<scheduler-event-listener-class>
com.sa.newsletter.trigger.NewsLetterScheduler</scheduler-event-listener-class>
<trigger>
<simple>
<simple-trigger-value>15</simple-trigger-value>
<time-unit>minute</time-unit>
</simple>
<simple>
<simple-trigger-value>15</simple-trigger-value>
<time-unit>minute</time-unit>
</simple>
<!--<cron>
<cron-trigger-value>0 0 0 * * ?</cron-trigger-value>
</cron> -->
<cron-trigger-value>0 0 0 * * ?</cron-trigger-value>
</cron> -->
</trigger>
</scheduler-entry>
</portlet>
</scheduler-entry>
</portlet>
This (<cron-trigger-value>0 0 0 * *
?</cron-trigger-value>) indicates that the scheduler will run
every night at 00 hrs.
<cron>
<cron-trigger-value>0 0 0 * * ?</cron-trigger-value>
</cron>
<cron-trigger-value>0 0 0 * * ?</cron-trigger-value>
</cron>
I Hope this small blog helps you a lot!!!
No comments:
Post a Comment