Wednesday 31 July 2019

Quartz: Shutdown Scheduler


'org.quartz.Scheduler' interface provides 'shutdown' method to shutdown a scheduler.

'shutdown' method is available in two overloaded forms.

void shutdown() throws SchedulerException;
It is equivalent to shutdown(false) method.

void shutdown(boolean waitForJobsToComplete)throws SchedulerException;
Stop the scheduler from firing of triggers. If waitForJobsToComplete is true, then the scheduler will not allow this method to return until all currently executing jobs have completed.

HelloJob.java
package com.sample.jobs;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;

public class HelloJob implements Job {
 public void execute(JobExecutionContext jec) throws JobExecutionException {
  String schedulerName = null;
  String schedulerInstanceId = null;

  String userName = null;
  String token = null;

  try {

   Scheduler scheduler = jec.getScheduler();

   schedulerName = scheduler.getSchedulerName();
   schedulerInstanceId = scheduler.getSchedulerInstanceId();

   userName = (String) scheduler.getContext().get("userName");
   token = (String) scheduler.getContext().get("token");

  } catch (SchedulerException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  System.out.println("**************************************************");
  System.out.println("Hello!!!");
  System.out.println("Job is triggered by scheduler : " + schedulerName);
  System.out.println("Scheduler instance id : " + schedulerInstanceId);
  System.out.println("User name : " + userName);
  System.out.println("Token : " + token);
  System.out.println("**************************************************");
 }
}


QuartzSchedulerEx.java
package com.sample.app;

import java.util.Date;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.SimpleTriggerImpl;

import com.sample.jobs.HelloJob;

public class QuartzSchedulerEx {
 public static void main(String args[]) throws SchedulerException, InterruptedException {

  // Initiate a Schedule Factory
  SchedulerFactory schedulerFactory = new StdSchedulerFactory();

  // Retrieve a scheduler from schedule factory
  Scheduler scheduler = schedulerFactory.getScheduler();

  scheduler.getContext().put("userName", "Krishna");
  scheduler.getContext().put("token", "token1234");

  // Delay the execution of scheduler for 5 seconds
  scheduler.start();

  JobDetailImpl helloJobDetails = new JobDetailImpl();
  helloJobDetails.setName("Hello Job");
  helloJobDetails.setJobClass(HelloJob.class);

  // Creating schedule time with trigger
  SimpleTriggerImpl trigger1 = new SimpleTriggerImpl();
  trigger1.setStartTime(new Date(System.currentTimeMillis() + 500));
  trigger1.setRepeatCount(-1);
  trigger1.setRepeatInterval(3000);
  trigger1.setName("First Trigger");
  trigger1.setPriority(10); // Setting trigger 1 priority to 10

  scheduler.scheduleJob(helloJobDetails, trigger1);

  System.out.println("Going to shutdown scheduler in 5 seconds....");
  Thread.sleep(5000);
  scheduler.shutdown();
  System.out.println("Scheduler is shutdowned");
 }
}

Output
Going to shutdown scheduler in 5 seconds....
**************************************************
Hello!!!
Job is triggered by scheduler : DefaultQuartzScheduler
Scheduler instance id : NON_CLUSTERED
User name : Krishna
Token : token1234
**************************************************
**************************************************
Hello!!!
Job is triggered by scheduler : DefaultQuartzScheduler
Scheduler instance id : NON_CLUSTERED
User name : Krishna
Token : token1234
**************************************************
Scheduler is shutdowned



Previous                                                    Next                                                    Home

No comments:

Post a Comment