Create a trigger with the job key (this job key refers to the job that you want to add trigger).
CronTriggerImpl cronTrigger2 = new CronTriggerImpl();
cronTrigger2.setStartTime(new Date(System.currentTimeMillis() + 1000));
cronTrigger2.setCronExpression("0/6 * * * * ?");
cronTrigger2.setName("SecondTrigger");
cronTrigger2.setDescription("Simple Trigger");
cronTrigger2.setKey(new TriggerKey("Trigger2"));
cronTrigger2.setJobKey(new JobKey(JOB_KEY));
Schedule the trigger.
scheduler.scheduleJob(cronTrigger2);
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.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 cronTrigger1 = new CronTriggerImpl();
cronTrigger1.setStartTime(new Date(System.currentTimeMillis() + 1000));
cronTrigger1.setCronExpression("0/3 * * * * ?");
cronTrigger1.setName("FirstTrigger");
cronTrigger1.setDescription("Simple Trigger");
cronTrigger1.setKey(new TriggerKey("Trigger1"));
scheduler.start();
scheduler.scheduleJob(jobDetail, cronTrigger1);
CronTriggerImpl cronTrigger2 = new CronTriggerImpl();
cronTrigger2.setStartTime(new Date(System.currentTimeMillis() + 1000));
cronTrigger2.setCronExpression("0/6 * * * * ?");
cronTrigger2.setName("SecondTrigger");
cronTrigger2.setDescription("Simple Trigger");
cronTrigger2.setKey(new TriggerKey("Trigger2"));
cronTrigger2.setJobKey(new JobKey(JOB_KEY));
scheduler.scheduleJob(cronTrigger2);
TimeUnit.SECONDS.sleep(12);
System.out.println("\nUnscheduling the job of SecondTrigger");
scheduler.unscheduleJob(new TriggerKey("Trigger2"));
}
}
Output
Trigger1 -> Thu Mar 04 14:15:18 IST 2021 Trigger2 -> Thu Mar 04 14:15:18 IST 2021 Trigger1 -> Thu Mar 04 14:15:21 IST 2021 Trigger1 -> Thu Mar 04 14:15:24 IST 2021 Trigger2 -> Thu Mar 04 14:15:24 IST 2021 Trigger1 -> Thu Mar 04 14:15:27 IST 2021 Unscheduling the job of SecondTrigger Trigger1 -> Thu Mar 04 14:15:30 IST 2021 Trigger1 -> Thu Mar 04 14:15:33 IST 2021 Trigger1 -> Thu Mar 04 14:15:36 IST 2021 Trigger1 -> Thu Mar 04 14:15:39 IST 2021 Trigger1 -> Thu Mar 04 14:15:42 IST 2021
No comments:
Post a Comment