Implement the RemindScheduler Class
In this step, you will learn how to implement the RemindScheduler
class that schedules and executes the RemindJob
.
-
Open the RemindScheduler.java
file located in the /home/labex/project/QuartzRemind/src/main/java/org/labex
directory.
-
Implement the getJobDetail()
method that creates and returns a org.quartz.JobDetail
instance for the RemindJob
.
public static JobDetail getJobDetail() {
return JobBuilder.newJob(RemindJob.class)
.withIdentity("reminderJob", "reminderGroup")
.build();
}
- Implement the
getTrigger()
method that creates and returns a org.quartz.Trigger
instance for the RemindJob
. The trigger will repeat the job every 2 seconds, 3 times.
public static Trigger getTrigger() {
return TriggerBuilder.newTrigger()
.withIdentity("reminderTrigger", "reminderGroup")
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(2)
.withRepeatCount(2))
.build();
}
- Implement the
doScheduler()
method that schedules and executes the RemindJob
using the org.quartz.Scheduler
instance.
public void doScheduler() throws SchedulerException, InterruptedException {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// Start the scheduler
scheduler.start();
System.out.println("Scheduling started");
// Schedule the job with the defined JobDetail and Trigger
scheduler.scheduleJob(getJobDetail(), getTrigger());
// Sleep for 10 seconds
Thread.sleep(10000);
// Shutdown the scheduler
scheduler.shutdown();
System.out.println("Scheduling completed");
}
This method starts the scheduler, schedules the job with the JobDetail
and Trigger
instances, waits for 10 seconds, and then shuts down the scheduler.