RSS
Showing posts with label create scheduler in portlet. Show all posts
Showing posts with label create scheduler in portlet. Show all posts

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:

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>

How to create scheduler in portlet

Version Liferay-5.2.3
Using Liferay SDK, create a scheduler job in a portlet.

First create a class that contains actual task that needs to be executed:

package com.portlet;

import com.liferay.portal.kernel.job.IntervalJob;
import com.liferay.portal.kernel.job.JobExecutionContext;
import com.liferay.portal.kernel.job.JobExecutionException;
import com.liferay.portal.kernel.util.Time;


public class SyncDataJob implements IntervalJob{
 public SyncDataJob() {
  _interval = Time.DAY;
 }
 public long getInterval() {
  return _interval;
 }
 public void execute(JobExecutionContext context)
  throws JobExecutionException {
  try {
   System.out.println("THIS IS THE ACTUAL TASK!");
  }
  catch (Exception e) {
  }
 }
 private long _interval;
}

Second write a class that implements Scheduler interface.

package com.portlet.struts;

import com.portlet.SyncDataJob;
import com.liferay.portal.kernel.job.JobSchedulerUtil;
import com.liferay.portal.kernel.job.Scheduler;

public class EventHistoricoAction implements Scheduler {

 public void schedule() {

  JobSchedulerUtil.schedule(_testIntervalJob);
 }

 public void unschedule() {

  JobSchedulerUtil.unschedule(_testIntervalJob);
 }


 private SyncDataJob _testIntervalJob = new SyncDataJob();

}

Last just register your scheduler in liferay-portlet.xml

<liferay-portlet-app>
    <portlet>
        <portlet-name>eventosSearch-portlet</portlet-name>
        <scheduler-class>com.portlet.struts.EventHistoricoAction</scheduler-class>
    </portlet>
    <role-mapper>
        <role-name>administrator</role-name>
        <role-link>Administrator</role-link>
    </role-mapper>
</liferay-portlet-app>