Thursday 4 March 2021

quartz: JobKey: To uniquely identify a job

 

JobKey is used to uniquely identify a JobDetail.

 

How to compose the keys?

Keys can be composed using two fields.

a.   Name of the key

b.   Group: Name of the key must be unique in the group

 

If only a name is specified then the default group name will be used.

 

Example

Jobkey jobKey = new JobKey(JOB_NAME, GROUP_NAME);

 

This job key is used to

a.   Uniquely identify a job

b.   Pause a job

c.    Resume the job

d.   Update the job

 

How to set a job key?

jobDetail.setKey(new JobKey(JOB_KEY));

 

How to pause a job?

scheduler.pauseJob(new JobKey(JOB_KEY));

 

How to resume the job?

scheduler.resumeJob(new JobKey(JOB_KEY));

 

How to update the job?

jobDetail.setKey(new JobKey(JOB_KEY));

scheduler.addJob(jobDetail, true);

 

Since I set the second argument to true, it replaces the job that matchis to this jobkey.

 

How to delete the job?

scheduler.deleteJob(new JobKey(JOB_KEY));

 

Find the below working application.

 

App.java

package com.sample.app;

import java.text.ParseException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerKey;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

import com.sample.app.jobs.HelloJob;
import com.sample.app.jobs.TimerJob;

public class App {
	private static final String JOB_KEY = "My_Timer_Job";

	public static void main(String args[]) throws ParseException, SchedulerException, InterruptedException {
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = schedulerFactory.getScheduler();

		JobDetailImpl jobDetail = new JobDetailImpl();
		jobDetail.setName("First Job");
		jobDetail.setJobClass(TimerJob.class);
		jobDetail.setDescription("Simple Task Application");
		jobDetail.setRequestsRecovery(true);
		jobDetail.setDurability(true);
		jobDetail.setKey(new JobKey(JOB_KEY));

		CronTriggerImpl cronTrigger = new CronTriggerImpl();
		cronTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
		cronTrigger.setCronExpression("0/3 * * * * ?");
		cronTrigger.setName("FirstTrigger");
		cronTrigger.setDescription("Simple Trigger");
		cronTrigger.setKey(new TriggerKey("Trigger1"));

		scheduler.start();
		scheduler.scheduleJob(jobDetail, cronTrigger);

		TimeUnit.SECONDS.sleep(6);

		System.out.println("\nPausing job for 6 seconds");
		scheduler.pauseJob(new JobKey(JOB_KEY));

		TimeUnit.SECONDS.sleep(20);

		System.out.println("\nResuming job");
		scheduler.resumeJob(new JobKey(JOB_KEY));

		// Update the job
		System.out.println("\nUpdating timer job with hello job");
		jobDetail = new JobDetailImpl();
		jobDetail.setName("First Job");
		jobDetail.setJobClass(HelloJob.class);
		jobDetail.setDescription("Simple Task Application");
		jobDetail.setRequestsRecovery(true);
		jobDetail.setDurability(true);
		jobDetail.setKey(new JobKey(JOB_KEY));
		scheduler.addJob(jobDetail, true);
		
		TimeUnit.SECONDS.sleep(10);
		System.out.println("\nDeleting the job");
		scheduler.deleteJob(new JobKey(JOB_KEY));
	}
}

 

Output

Thu Mar 04 14:01:09 IST 2021
Thu Mar 04 14:01:12 IST 2021

Pausing job for 6 seconds

Resuming job

Updating timer job with hello job
Thu Mar 04 14:01:33 IST 2021
Hello How are you!!!!
Hello How are you!!!!
Hello How are you!!!!
Hello How are you!!!!
Hello How are you!!!!
Hello How are you!!!!
Hello How are you!!!!
Hello How are you!!!!
Hello How are you!!!!

Deleting the job

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment