Adding more examples to the Quartz project (#2101)

This commit is contained in:
Alex Vargas 2017-06-18 00:36:51 -07:00 committed by Zeger Hendrikse
parent eba5b938f5
commit fada609f16
4 changed files with 66 additions and 2 deletions

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -21,6 +21,8 @@ public class QuartzExample {
JobDetail job = JobBuilder.newJob(SimpleJob.class) JobDetail job = JobBuilder.newJob(SimpleJob.class)
.withIdentity("myJob", "group1") .withIdentity("myJob", "group1")
.usingJobData("jobSays", "Hello World!")
.usingJobData("myFloatValue", 3.141f)
.build(); .build();
Trigger trigger = TriggerBuilder.newTrigger() Trigger trigger = TriggerBuilder.newTrigger()
@ -30,8 +32,36 @@ public class QuartzExample {
.withIntervalInSeconds(40) .withIntervalInSeconds(40)
.repeatForever()) .repeatForever())
.build(); .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(job, trigger);
sched.scheduleJob(jobA, triggerA);
sched.scheduleJob(jobB, triggerB);
sched.start(); sched.start();
} catch (SchedulerException e) { } catch (SchedulerException e) {

View File

@ -1,13 +1,21 @@
package com.baeldung.quartz; package com.baeldung.quartz;
import org.quartz.Job; import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext; import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; import org.quartz.JobExecutionException;
public class SimpleJob implements Job { public class SimpleJob implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException { public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("This is a quartz job!"); 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);
} }
} }