Saturday, 19 January 2013

Quartz : Java Job Scheduler...A brief Hands-On..

The text contents in this post are an elegant Copy-Paste from (here) . The codes that follow are 100 % genuine and reflects my awesomeness.....Having said that , lets start :)

What is Quartz ?
Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as support for JTA transactions and clustering.

Where to get Quartz from ?
If you don't get it at your local grocery store , try getting it  (here)

The Example Application Codes ?
The application polls a directory after every 30 seconds , and looks for new files that has been placed in the directory , since the last time the poller was executed. In case any new file is found , it picks up the absolute path and logs it (into a Data Base or where ever you find appropriate).

MainScheduler.java :

package com.soham.QuartzPolling;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import com.POC.Jobs.DirectoryPollerJob;

public class MainScheduler {
public static long lastTimeStamp=0;
public static void main(String[] args) {
  
  try {
   
   // specify the job' s details..
   JobDetail job = JobBuilder.newJob(DirectoryPollerJob.class).withIdentity("directoryPollerJob").build();
   job.getJobDataMap().put("lastTimeStamp", lastTimeStamp);
   
   // specify the running period of the job
   Trigger trigger = TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.simpleSchedule().
       withIntervalInSeconds(30).repeatForever())  
                             .build();  
      
   //schedule the job
   SchedulerFactory schFactory = new StdSchedulerFactory();
   Scheduler sch = schFactory.getScheduler();
      sch.start();      
      sch.scheduleJob(job, trigger);  
  
  } catch (SchedulerException e) {
   e.printStackTrace();
  }
 }

}

DirectoryPollerJob.java :

package com.soham.QuartzPolling;

import java.io.File;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class DirectoryPollerJob implements Job{

 public void execute(JobExecutionContext arg0) throws JobExecutionException {
  File[] targetDirFiles=new File("D:\\HOME\\QUARTZ\\TEST_FOLDER").listFiles();
  arg0.getJobDetail().getJobDataMap().get("lastTimeStamp");
  StartFileIndexing(arg0.getPreviousFireTime(),targetDirFiles);
 }

 public static void StartFileIndexing(Date prevFireTime, File[] targetDirFiles)
 {
  for(File file: targetDirFiles){
   if(file.isDirectory())
    StartFileIndexing(prevFireTime, file.listFiles());
   else{
    
    if(file.lastModified()>prevFireTime.getTime())
     {
      System.out.println("dsdsds :: "+file.getAbsolutePath());
     }
   }
  }
 }
}

No comments:

Post a Comment