Wednesday 31 July 2019

Quartz: Scheduler: getMetaData: Get metadata of the scheduler


'org.quartz.Scheduler' interface provides 'getMetaData' method to get the settings and capabilities of scheduler instance.

Find the below working example.

HelloJob.java
package com.sample.jobs;

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

public class HelloJob implements Job {
 public void execute(JobExecutionContext jec) throws JobExecutionException {
  SchedulerMetaData metaData = null;
  try {
   metaData = jec.getScheduler().getMetaData();
  } catch (SchedulerException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println("****************************");
  System.out.println(metaData);
  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();

  // 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); // Run indefinite times
  trigger1.setRepeatInterval(3000);
  trigger1.setName("First Trigger");
  trigger1.setPriority(10); // Setting trigger 1 priority to 10

  scheduler.scheduleJob(helloJobDetails, trigger1);
 }
}

Output
****************************
Quartz Scheduler (v2.3.0) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.impl.StdScheduler' - running locally.
  Running since: Fri Jul 20 23:53:19 IST 2018
  Not currently in standby mode.
  Number of jobs executed: 1
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

****************************
****************************
Quartz Scheduler (v2.3.0) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.impl.StdScheduler' - running locally.
  Running since: Fri Jul 20 23:53:19 IST 2018
  Not currently in standby mode.
  Number of jobs executed: 2
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.


****************************




Previous                                                    Next                                                    Home

No comments:

Post a Comment