First Program with Quartz Framework

JavaJavaBeginner
Practice Now

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 Job interface in the RemindJob class to define the task that needs to be executed
  • How to implement a RemindScheduler class to schedule and execute the RemindJob using the Quartz Scheduler
  • How to write a test function to verify the scheduling functionality of the RemindScheduler class

🏆 Achievements

After completing this project, you will be able to:

  • Use the Quartz framework to schedule and execute jobs
  • Create and configure JobDetail and Trigger instances 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.

  1. Open the RemindJob.java file located in the /home/labex/project/QuartzRemind/src/main/java/org/labex directory.

  2. Implement the execute() method of the org.quartz.Job interface. 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.

âœĻ Check Solution and Practice

Implement the RemindScheduler Class

In this step, you will learn how to implement the RemindScheduler class that schedules and executes the RemindJob.

  1. Open the RemindScheduler.java file located in the /home/labex/project/QuartzRemind/src/main/java/org/labex directory.

  2. 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();
}
  1. 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();
}
  1. 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.

âœĻ Check Solution and Practice

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.

  1. Open the TestQuartz.java file located in the /home/labex/project/QuartzRemind/src/test/java/org/labex directory.

  2. Implement the test() method that creates an instance of RemindScheduler and invokes its doScheduler() 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.

âœĻ Check Solution and Practice

Run the Test

To run the test and verify the project, follow these steps:

  1. Open a terminal and navigate to the /home/labex/project/QuartzRemind directory.

  2. 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.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Java Tutorials you may like