Introduction
In this project, you will learn how to use the Quartz framework to schedule and execute a simple job that reminds the user to wake up, brush their teeth, and wash their face.
🎯 Tasks
In this project, you will learn:
- How to implement a
Jobinterface in theRemindJobclass to define the task that needs to be executed - How to implement a
RemindSchedulerclass to schedule and execute theRemindJobusing the Quartz Scheduler - How to write a test function to verify the scheduling functionality of the
RemindSchedulerclass
🏆 Achievements
After completing this project, you will be able to:
- Use the Quartz framework to schedule and execute jobs
- Create and configure
JobDetailandTriggerinstances for a job - Start and shut down the Quartz Scheduler
- Write test functions to verify the functionality of your application
Implement the RemindJob class
In this step, you will learn how to implement the RemindJob class that implements the Job interface from the Quartz framework.
Open the
RemindJob.javafile located in the/home/labex/project/QuartzRemind/src/main/java/org/labexdirectory.Implement the
execute()method of theorg.quartz.Jobinterface. This method will be called by the Quartz Scheduler when the job is executed.
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Wake up! Brush your teeth and wash your face!");
System.out.println("Current system time - " + new Date());
}
The execute() method should print the reminder message "Wake up! Brush your teeth and wash your face!" and the current system time.
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.javafile located in the/home/labex/project/QuartzRemind/src/main/java/org/labexdirectory.Implement the
getJobDetail()method that creates and returns aorg.quartz.JobDetailinstance for theRemindJob.
public static JobDetail getJobDetail() {
return JobBuilder.newJob(RemindJob.class)
.withIdentity("reminderJob", "reminderGroup")
.build();
}
- Implement the
getTrigger()method that creates and returns aorg.quartz.Triggerinstance for theRemindJob. 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 theRemindJobusing theorg.quartz.Schedulerinstance.
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.
Write the test function
In this step, you will learn how to write a test function to verify the scheduling functionality of the RemindScheduler class.
Open the
TestQuartz.javafile located in the/home/labex/project/QuartzRemind/src/test/java/org/labexdirectory.Implement the
test()method that creates an instance ofRemindSchedulerand invokes itsdoScheduler()method.
@Test
public void test() {
try {
new RemindScheduler().doScheduler();
} catch (InterruptedException | SchedulerException e) {
e.printStackTrace();
}
}
This test method will execute the scheduling functionality and verify that the job is executed as expected.
Run the test
To run the test and verify the project, follow these steps:
Open a terminal and navigate to the
/home/labex/project/QuartzReminddirectory.Run the following command to execute the test:
mvn test
You should see the following output:
Scheduling started
Wake up! Brush your teeth and wash your face!
Current system time - Fri Apr 26 06:54:03 CST 2024
Wake up! Brush your teeth and wash your face!
Current system time - Fri Apr 26 06:54:05 CST 2024
Wake up! Brush your teeth and wash your face!
Current system time - Fri Apr 26 06:54:07 CST 2024
Scheduling completed
This output indicates that the RemindJob was executed three times, as scheduled by the RemindScheduler class.
Congratulations! You have successfully completed the project by implementing the RemindJob and RemindScheduler classes, and writing a test function to verify the scheduling functionality.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



