Wednesday 31 July 2019

Quartz: Scheduler: getCurrentlyExecutingJobs: Get Currently Executing jobs


‘org.quartz.Scheduler’ class provides ‘getCurrentlyExecutingJobs’ method, it returns list of JobExecutionContext objects, that represent all currently executing Jobs in this Scheduler instance.

List<JobExecutionContext> jobExecContexts = scheduler.getCurrentlyExecutingJobs();

System.out.println("********************************************");
System.out.println("Currently Executing Jobs");
for (JobExecutionContext context : jobExecContexts) {
         System.out.println(((JobDetailImpl) context.getJobDetail()).getName());
}
System.out.println("********************************************");

Find the below working application.

HelloJob.java
package com.sample.jobs;

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

public class HelloJob implements Job {
 public void execute(JobExecutionContext jec) throws JobExecutionException {
  try {
   Thread.sleep(2000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println("Hello World.........");
 }
}


PrintJob.java
package com.sample.jobs;

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

public class PrintJob implements Job {
 public void execute(JobExecutionContext jec) throws JobExecutionException {
  try {
   Thread.sleep(2000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println("Printing.......");

 }
}


QuartzSchedulerEx.java
package com.sample.app;

import java.util.Date;
import java.util.List;

import org.quartz.JobExecutionContext;
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;
import com.sample.jobs.PrintJob;

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();

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

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

  JobDetailImpl printJobDetails = new JobDetailImpl();
  printJobDetails.setName("Print Job");
  printJobDetails.setGroup("DemoJobGroup");
  printJobDetails.setJobClass(PrintJob.class);

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

  // Creating schedule time with trigger
  SimpleTriggerImpl trigger2 = new SimpleTriggerImpl();
  trigger2.setStartTime(new Date(System.currentTimeMillis() + 500));
  trigger2.setRepeatCount(-1); // Run indefinite times
  trigger2.setRepeatInterval(2000);
  trigger2.setName("Second Trigger");
  trigger2.setPriority(10); // Setting trigger 1 priority to 10

  scheduler.scheduleJob(helloJobDetails, trigger1);
  scheduler.scheduleJob(printJobDetails, trigger2);

  /* Thread prints currently executing jobs */
  Thread t1 = new Thread() {
   public void run() {

    while (true) {
     try {
      Thread.sleep(1000);
      List<JobExecutionContext> jobExecContexts = scheduler.getCurrentlyExecutingJobs();

      System.out.println("********************************************");
      System.out.println("Currently Executing Jobs");
      for (JobExecutionContext context : jobExecContexts) {
       System.out.println(((JobDetailImpl) context.getJobDetail()).getName());
      }
      System.out.println("********************************************");
     } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }

   }
  };

  t1.start();
 }
}

Run QuartzSchedulerEx.java, you can see below messages in the console.

********************************************
Currently Executing Jobs
Hello Job
Print Job
********************************************
********************************************
Currently Executing Jobs
Hello Job
Print Job
********************************************
Hello World.........
Printing.......
********************************************
.........
.........




Previous                                                    Next                                                    Home

No comments:

Post a Comment