Thursday, 26 September 2013

Scheduled Tasks With Cron(QUARTZ).

To configure regularly scheduled tasks that operate at defined times or regular
 intervals. 
These tasks are commonly known as cron jobs. Quartz is a open source job scheduling framework,
 that let you scheduler a task to run on a predefine date and time.

When a Quartz application is first launched,the main thread starts the Scheduler.
The QuartzScheduler is created and creates an instance of the  
 org.quartz.core.QuartzSchedulerThread class.The QuartzScheduler Thread contains the processing
 loop for determining when the next job should be triggered.As the name implies,the 
QuartzSchedulerThread is a Java thread.It runs as a nondaemon thread with a normal priority. 
 
When first Tomcat server is on  "Initializing Scheduling PlugIn" will be printed then 
when we schdule the task it will be executed on that particular time.
 
InitializeServlet.java 
package com.cron;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class InitializeServlet extends HttpServlet {

 public void init() throws ServletException {
        try {
        System.out.println("Initializing Scheduling PlugIn");
       CronScheluder objPlugin = new CronScheluder();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}
CronScheluder.java
package com.cron; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.impl.StdSchedulerFactory; public class CronScheluder { public CronScheluder() throws Exception { SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sche = sf.getScheduler(); sche.start(); 
    // MyJob class will be executed at given interval. 
    JobDetail jDetail = new JobDetail("Scheduling", "NJob", MyJob.class);
    //"0 0 12 * * ?" Fire at 12pm (noon) every day
    //"0/2 * * * * ?" Fire at every 2 seconds every day
   CronTrigger crTrigger = new CronTrigger("cronTrigger","NJob","0 0 12 * * ?");
    sche.scheduleJob(jDetail, crTrigger);
  }
}
 
MyJob.java:
 
package com.cron;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class MyJob extends BaseHibernateDAO implements Job {

    public void execute(JobExecutionContext context)
     throws JobExecutionException {

        System.out.println("Cron executing "); 

    }
}
 
If we want to run Quartz parallel to the main thread 
 
CronScheluder.java 
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class CronScheluder extends Thread{

public void run(){
 try{
 SchedulerFactory sf = new StdSchedulerFactory();
 Scheduler sche = sf.getScheduler();
 sche.start();
 JobDetail jDetail = new JobDetail("Newsletter", "NJob", MyJob.class);
 //"0 0 12 * * ?" Fire at 12pm (noon) every day
 //"0/2 * * * * ?" Fire at every 2 seconds every day
 CronTrigger crTrigger = 
        new CronTrigger("cronTrigger", "NJob", "0 0 12 * * ?");
 sche.scheduleJob(jDetail, crTrigger);
 // sche.shutdown();
}catch (Exception e) {

  e.printStackTrace();
             }
}
}
InitializeServlet.java 
 
  try {
        System.out.println("Initializing Scheduling PlugIn");
        CronScheluder objPlugin = new CronScheluder();
        if(!objPlugin.isAlive()){
   objPlugin.start();
        }
      }

No comments: