Monthly Archives: March 2016

Java Quartz scheduler – Job Parameters

There are two ways to pass an object that can be retrieved when a Quartz job executes:

Pass the instance in the data map. When you set the job up, add your instance to the map with a key like this:

// Create job etc...
var MyClass _myInstance;
statusJob.JobDataMap.Put("myKey", _myInstance);
// Schedule job...

Retrieve the instance in the job’s Execute() method like this:

public void Execute(IJobExecutionContext context)
        {
            var dataMap = context.MergedJobDataMap;
            var myInstance = (MyClass)dataMap["myKey"];
        }

OR

Add the instance to the scheduler context when you set the job up, like this:

  ISchedulerFactory schedFact = new StdSchedulerFactory();
  _sched = schedFact.GetScheduler();
  _sched.Start();
  // Create job etc...
  var MyClass _myInstance;
  _sched.Context.Put("myKey", myInstance);
  // Schedule job...

Retrieve the instance in the job’s Execute() method like this:

public void Execute(IJobExecutionContext context)
        {
            var schedulerContext = context.Scheduler.Context;
            var myInstance = (MyClass)schedulerContext.Get("myKey");
        }

References :
http://stackoverflow.com/questions/7137960/quartz-scheduler-how-to-pass-custom-objects-as-jobparameter

Java Quartz scheduler – Check Job Exists

        DateTime dateTime=new DateTime();
        dateTime =dateTime.plusMinutes(1);

        JobDetail job= JobBuilder.newJob(Task1.class).withIdentity("job1","group1").build();

        System.out.println(job.getKey().toString());

        SimpleTrigger trigger= (SimpleTrigger) TriggerBuilder.newTrigger()
                .withIdentity("trigger1","group1")
                .startAt(dateTime.toDate())
                .build();


        System.out.println(String.format("Start : %s",new DateTime().toString()));
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);

        System.out.println(scheduler.checkExists(new JobKey("job1","group1")));

References :
http://stackoverflow.com/questions/26523285/checking-if-the-job-exists-without-looping-through-all-the-jobs

Java Quartz scheduler – Getting Started

package com.mkyong.quartz;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class SimpleTriggerExample {
	public static void main(String[] args) throws Exception {
		
		// Quartz 1.6.3
		// JobDetail job = new JobDetail();
		// job.setName("dummyJobName");
		// job.setJobClass(HelloJob.class);

		JobDetail job = JobBuilder.newJob(HelloJob.class)
			.withIdentity("dummyJobName", "group1").build();

                //Quartz 1.6.3
		// SimpleTrigger trigger = new SimpleTrigger();
		// trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
		// trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
		// trigger.setRepeatInterval(30000);

		// Trigger the job to run on the next round minute
		Trigger trigger = TriggerBuilder
			.newTrigger()
			.withIdentity("dummyTriggerName", "group1")
			.withSchedule(
				SimpleScheduleBuilder.simpleSchedule()
					.withIntervalInSeconds(5).repeatForever())
			.build();

		// schedule it
		Scheduler scheduler = new StdSchedulerFactory().getScheduler();
		scheduler.start();
		scheduler.scheduleJob(job, trigger);

	}
}

References :
http://www.mkyong.com/java/quartz-2-scheduler-tutorial/