SoFunction
Updated on 2025-04-21

Spring Boot integrates Quartz and uses Cron expressions to implement timing tasks

Preface

In Spring Boot projects, we often need to perform certain tasks regularly, such as regularly cleaning databases, synchronizing data, sending notifications, etc.QuartzIt is a powerful task scheduling framework that can be usedCron expressionsAchieve flexible task scheduling. This article will introduce how toSpring BootIntegrationQuartzAnd useCron expressionsPerform task scheduling.

1. Add Quartz dependencies

existSpring BootIn the project, the first thing you need to do is to introduce Quartz-related dependencies.

If usingMaven,existAdd the following dependencies to:

<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

If usingGradle, then add:

implementation ':spring-boot-starter-quartz'

2. Create Quartz tasks

In Quartz, each timing task needs to be inheritedInterface and implementexecutemethod.

import ;
import ;
import ;
import ;
public class MyQuartzJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        ("Quartz timing task is being executed:" + ());
    }
}

3. Configure Quartz task scheduling

Spring Boot allows to passSchedulerFactoryBeanConfigure Quartz tasks and use@BeanMethod defines the scheduling rules of tasks.

import .*;
import ;
import ;
@Configuration
public class QuartzConfig {
    // Define JobDetail    @Bean
    public JobDetail myJobDetail() {
        return ()
                .withIdentity("myQuartzJob")  // Task name                .storeDurably()
                .build();
    }
    // Define triggers, use Cron expressions    @Bean
    public Trigger myJobTrigger() {
        return ()
                .forJob(myJobDetail())
                .withIdentity("myQuartzTrigger") // Trigger name                .withSchedule(("0 0/5 0-7 ? * * *")) // Cron expression                .build();
    }
}

In the above configuration, our timing tasks:

  • Perform every 5 minutes
  • Perform from 00:00 to 07:59 every day
  • useCron expression0 0/5 0-7 ? * * *

If needed08:00 also executed once, you can modify the Cron expression to:

0 0/5 0-8 ? * * *

4. Start Spring Boot to observe the execution of the scheduled task

start upSpring BootProject, Observe console output:

Quartz timing task is being executed: 2024-03-19T00:00:00
Quartz timing task is being executed: 2024-03-19T00:05:00
Quartz timing task is being executed: 2024-03-19T00:10:00
...
Quartz timing task is being executed: 2024-03-19T07:55:00

If the Cron expression is modified to0 0/5 0-8 ? * * *, it will be executed once more:

Quartz Timed tasks are being executed:2024-03-19T08:00:00

5. Detailed explanation of Quartz Cron expression

Quartz's Cron expression has 7 fields, and the meaning of each field is as follows:

Fields Allowed values illustrate
Seconds 0-59 Number of seconds the task is triggered
Minutes 0-59 Number of minutes triggered by a task
Hours 0-23 The number of hours the task is triggered
Date (Day of month) 1-31 The date when the task is triggered (cannot be specified at the same time as Day of week)
Month 1-12 or JAN-DEC The month of task triggered
Day of week 1-7 or SUN-SAT The day of week when the task is triggered
Year (optional) Empty or 1970-2099 Year of task trigger

Example Cron expression

  • 0 0 12 * * ?→ Every day at 12:00 noon
  • 0 0/30 * * * ?→ Perform every 30 minutes
  • 0 15 10 * * ?→ Perform at 10:15 every day
  • 0 0 8-20/2 * * ?→ Every day between 08:00 and 20:00, every 2 hours

6. Conclusion

This article describes how toSpring BootIntegrationQuartzPerform scheduled tasks and passCron expressionsControls the task execution time.Quartz provides more powerful task scheduling capabilities,Compare@ScheduledThe annotation is more flexible and suitable for complex timing task requirements.

Are you using Quartz in your project? Welcome to share your experience in the comment section! 🚀

This is the end of this article about Spring Boot integrating Quartz and using Cron expressions to implement timing tasks. For more related Spring Boot integration Quartz content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!