In two ways a job can be scheduled in Liferay.
- Configure in liferay-portlet.xml
- Implement it in code.
- Override the property 'scheduler.enabled = true' in portal-ext.properties.
- Create a new dummy portlet in liferay-portlet.xml as follows,
<portlet> <portlet-name>job</portlet-name> <scheduler-entry> <scheduler-event-listener-class>com.madrone.portal.job.PortalJob</scheduler-event-listener-class> <trigger> <simple> <simple-trigger-value>5</simple-trigger-value> <time-unit>minute</time-unit> </simple> <!-- cron> <property-key>0 0 0 * * ?</property-key> </cron--> </trigger> </scheduler-entry> </portlet>
- Add this portlet in portlet.xml
<portlet> <portlet-name> job</portlet-name> <display-name>Job</display-name> <portlet-class> com.madrone.portal.job.PortalJob </portlet-class> <supports> <mime-type>text/html</mime-type> <portlet-mode>view</portlet-mode> </supports> <security-role-ref> <role-name>administrator</role-name> </security-role-ref> <security-role-ref> <role-name>guest</role-name> </security-role-ref> <security-role-ref> <role-name>power-user</role-name> </security-role-ref> <security-role-ref> <role-name>user</role-name> </security-role-ref> </portlet>
- Create a job listener
public class PortalJob implements MessageListener { private static final Log LOG = LogFactory.getLog(PortalJob.class); @Override public void receive(Message arg0) { LOG.info("PortalJob: Starts" ); // your code LOG.info("PortalJob: Stops" ); } }
- Override the property 'scheduler.enabled = true' in portal-ext.properties.
- Create an action class which is run during server startup
public class PortalStartupAction extends SimpleAction { private static final Log LOG = LogFactory.getLog(PortalStartupAction .class ); @Override public void run(String[] arg0) throws ActionException { LOG.info("Preparing to Schedule the job: PortalJob" ); Thread thread = Thread. currentThread(); try { SchedulerEngineUtil. schedule(PortalJob.SCHEDULER_ENTRY, thread.getContextClassLoader()); } catch (SchedulerException e) { throw new ActionException(e); } LOG.info("PortalJob scheduled successfully." ); } }
- Configure the above action class in portal-ext.properties
# # Application startup event that runs once for every web site instance of # the portal that initializes. # application.startup.events=com.madrone.portal.util.PortalStartupAction
- Create a job
public class PortalJob implements MessageListener { private static final Log LOG = LogFactory.getLog(PortalJob.class); @SuppressWarnings("serial" ) public static final SchedulerEntry SCHEDULER_ENTRY = new SchedulerEntryImpl() { { setDescription( "Job that get triggered every 5 minutes"); setTriggerType(TriggerType. SIMPLE); setTimeUnit(TimeUnit. MINUTE); setTriggerValue( "5"); //setTriggerType(TriggerType.CRON); //setTriggerValue("0 0 0 * * ?"); // Fire at 12:00 AM every midnight // Reference: http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger setEventListenerClass(PortalJob.class.getName()); } }; @Override public void receive(Message arg0) { LOG.info("CRPortalERPJob: Starts" ); // your code LOG.info("CRPortalERPJob: Stops" ); } }
1 comment:
how to set both date and time in setTriggerValue() ?
Post a Comment