How to create scheduler in portlet with Quartz
Version Liferay-5.2.3
Using Liferay SDK, create a scheduler job in a portlet with Quartz
First create a class that contains actual task that needs to be executed:
Second write a class that implements Scheduler interface.
This job is executed every day at 00h00m.For more detail and examples of Quartz see: Quartz
Last just register your scheduler in liferay-portlet.xml
Using Liferay SDK, create a scheduler job in a portlet with Quartz
First create a class that contains actual task that needs to be executed:
package com.portlet.struts;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SabiasQueJob implements org.quartz.Job{
private static Log log = LogFactory.getLog(SabiasQueJob.class);
public SabiasQueJob() {}
public void execute(org.quartz.JobExecutionContext arg0) throws org.quartz.JobExecutionException {
try {
System.out.println("THIS IS THE ACTUAL TASK!");
} catch (Exception e) {
log.error("Portlet, Caught:" + e);
}
}
}
Second write a class that implements Scheduler interface.
package com.portlet.struts;
import com.liferay.portal.kernel.job.Scheduler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class SabiasQueSchedulerInterface implements Scheduler {
private static Log _log = LogFactory
.getLog(SabiasQueSchedulerInterface.class);
public void NewSchedule() throws Exception {
SchedulerFactory sf = new StdSchedulerFactory();
org.quartz.Scheduler sched = sf.getScheduler();
JobDetail jd2 = new JobDetail("myjob",
org.quartz.Scheduler.DEFAULT_GROUP, SabiasQueJob.class);
CronTrigger trigger = new CronTrigger("mytrigger",
org.quartz.Scheduler.DEFAULT_GROUP, "myjob",
org.quartz.Scheduler.DEFAULT_GROUP, "0 0 0 * * ?");
sched.scheduleJob(jd2, trigger);
sched.start();
}
public void schedule() {
if (_log.isInfoEnabled()) {
_log.info("Schedule");
}
try {
NewSchedule();
} catch (Exception ex) {
_log.error(ex);
}
}
public void unschedule() {
if (_log.isInfoEnabled()) {
_log.info("Unschedule");
}
}
}
This job is executed every day at 00h00m.For more detail and examples of Quartz see: Quartz
Last just register your scheduler in liferay-portlet.xml
<liferay-portlet-app>
<portlet>
<portlet-name>eventosSearch-portlet</portlet-name>
<scheduler-class>com.portlet.struts.SabiasQueSchedulerInterface</scheduler-class>
</portlet>
<role-mapper>
<role-name>administrator</role-name>
<role-link>Administrator</role-link>
</role-mapper>
</liferay-portlet-app>

Post a Comment