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
Following cron expression indicates that the scheduler will run every day at 18:00 PM.
To know how to write cron expression you can visit below links.
Cron Trigger
Cron Maker
2) create scheduler class.
1) Make below entry in Liferay-portlet.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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----> receive()"); | |
//write your logic. | |
} | |
} |
Along with the basics,the links are pretty resourceful
ReplyDeleteGood to know that it helped you . Please follow this blog to get more updates.
ReplyDeleteThank you
Jitendra Rajput
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