diff --git a/libraries/src/main/java/com/baeldung/quartz/JobA.java b/libraries/src/main/java/com/baeldung/quartz/JobA.java new file mode 100644 index 0000000000..76eb1118e6 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/quartz/JobA.java @@ -0,0 +1,13 @@ +package com.baeldung.quartz; + +import org.quartz.Job; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; + +public class JobA implements Job { + + public void execute(JobExecutionContext arg0) throws JobExecutionException { + System.out.println("This is the job A"); + } + +} \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/quartz/JobB.java b/libraries/src/main/java/com/baeldung/quartz/JobB.java new file mode 100644 index 0000000000..1a5e353c74 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/quartz/JobB.java @@ -0,0 +1,13 @@ +package com.baeldung.quartz; + +import org.quartz.Job; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; + +public class JobB implements Job { + + public void execute(JobExecutionContext arg0) throws JobExecutionException { + System.out.println("This is the job B"); + } + +} \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/quartz/QuartzExample.java b/libraries/src/main/java/com/baeldung/quartz/QuartzExample.java index 1e37fc028b..20853aa01b 100644 --- a/libraries/src/main/java/com/baeldung/quartz/QuartzExample.java +++ b/libraries/src/main/java/com/baeldung/quartz/QuartzExample.java @@ -21,6 +21,8 @@ public class QuartzExample { JobDetail job = JobBuilder.newJob(SimpleJob.class) .withIdentity("myJob", "group1") + .usingJobData("jobSays", "Hello World!") + .usingJobData("myFloatValue", 3.141f) .build(); Trigger trigger = TriggerBuilder.newTrigger() @@ -30,8 +32,36 @@ public class QuartzExample { .withIntervalInSeconds(40) .repeatForever()) .build(); + + JobDetail jobA = JobBuilder.newJob(JobA.class) + .withIdentity("jobA", "group2") + .build(); + + JobDetail jobB = JobBuilder.newJob(JobB.class) + .withIdentity("jobB", "group2") + .build(); + + Trigger triggerA = TriggerBuilder.newTrigger() + .withIdentity("triggerA", "group2") + .startNow() + .withPriority(15) + .withSchedule(SimpleScheduleBuilder.simpleSchedule() + .withIntervalInSeconds(40) + .repeatForever()) + .build(); + + Trigger triggerB = TriggerBuilder.newTrigger() + .withIdentity("triggerB", "group2") + .startNow() + .withPriority(10) + .withSchedule(SimpleScheduleBuilder.simpleSchedule() + .withIntervalInSeconds(20) + .repeatForever()) + .build(); sched.scheduleJob(job, trigger); + sched.scheduleJob(jobA, triggerA); + sched.scheduleJob(jobB, triggerB); sched.start(); } catch (SchedulerException e) { diff --git a/libraries/src/main/java/com/baeldung/quartz/SimpleJob.java b/libraries/src/main/java/com/baeldung/quartz/SimpleJob.java index 370d698d13..986c5e96e5 100644 --- a/libraries/src/main/java/com/baeldung/quartz/SimpleJob.java +++ b/libraries/src/main/java/com/baeldung/quartz/SimpleJob.java @@ -1,13 +1,21 @@ package com.baeldung.quartz; import org.quartz.Job; +import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class SimpleJob implements Job { - public void execute(JobExecutionContext arg0) throws JobExecutionException { - System.out.println("This is a quartz job!"); + public void execute(JobExecutionContext context) throws JobExecutionException { + JobDataMap dataMap = context.getJobDetail() + .getJobDataMap(); + + String jobSays = dataMap.getString("jobSays"); + float myFloatValue = dataMap.getFloat("myFloatValue"); + + System.out.println("Job says: " + jobSays + ", and val is: " + myFloatValue); + } } \ No newline at end of file