skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Monday, September 17, 2012

Job Scheduling in JAVA using QUARTZ weekly

Posted by Admin at 11:52 AM – 0 comments
 
For implementing this in you need to have 3 java files -
  • Context Listener 
  • Job Listener
  • JobClass

Your Java Class which contains the job to be executed.
Also you need to make the Listener entry in the web.xml and add quartz- 1.8.0 jar.
import java.util.Date;
import java.util.ResourceBundle;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.CronTrigger;


public class xyzContextListener implements ServletContextListener {

 /* (non-Javadoc)
  * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
  */
 
 Scheduler scheduler = null;
 IncidentJobListener myJobListener;
 
 
 
 public void contextDestroyed(ServletContextEvent arg0) {
  
  
  try 
  {
   scheduler.removeGlobalJobListener(myJobListener.getName());
   scheduler.shutdown(false);
   
  }
  catch (SchedulerException e) 
  {
   
   
  }

 }
 /* (non-Javadoc)
  * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
  */
 public void contextInitialized(ServletContextEvent arg0) {


  try {
   // Grab the Scheduler instance from the Factory

   scheduler = StdSchedulerFactory.getDefaultScheduler();
   // scheduler.start();

   

   JobDetail jobDetail = new JobDetail("job name"
     null, // job group (you can also specify 'null' to use the
     // default group)
     JobClass.class); // the java class to execute

   ResourceBundle rb = ResourceBundle
     .getBundle("resources.application");
   String repeatInterval = rb.getString("batchInterval");
   
   CronTrigger ct=new CronTrigger("cronTrigger","group3","0 01 10 ? * MON");
   
   scheduler.scheduleJob(jd,ct);
   scheduler.start();
 
   scheduler.scheduleJob(jd, ct);
   

   myJobListener = new IncidentJobListener();

   scheduler.addGlobalJobListener(myJobListener);
   

   scheduler.start();
   

  } catch (SchedulerException se) {
   

  }

 }

}




Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger