use

Create a SpringBoot project to introduce dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-quartz</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
Copy the code

Unlike Scheduled, you don’t need to add annotations to start classes

Define the content of the task

public class MyJob2 extends QuartzJobBean {
    private String name;

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("hello : "+ name + ""+newDate()); }}Copy the code

Corn expressions can be used to define the time interval. The format of the CORN expression is as follows

[second] [minute] [hour] [day] [month] [week] [year]

Note that the days of the month and the week of the month may conflict, so one of them must be?

Wildcard meaning:

  • ?Indicates that no value is specified. That is, the value of a field is not cared about. It is important to note that the days and weeks of the month may conflict, so one or the other must be configured?
  • *Represents all values, e.g., set on the field of seconds*Is triggered every second
  • .Used to separate multiple values, such as “MON,WED,FRI” on the week field to trigger Monday, Wednesday, and Friday
  • -Indicates the interval. For example, if you set seconds to 10-12, the alarm is triggered in 10,11, and 12 seconds
  • /For incremental triggering, for example, set “5/15” above seconds to trigger every 15 seconds starting from 5 seconds (5,20,35,50)
  • # #Ordinal (the day of the month), for example, set “6##3” to the third Saturday of the month in the week field (perfect for Mother’s Day and Father’s Day)
  • The setting of the week field is case insensitive when using English letters, that is, MON is the same as MON
  • LShow the last meaning. In the day field, it represents the last day of the month (depending on the current month, if it is February, it will automatically determine whether it is a wet year). In the week field, it represents Saturday, which is equivalent to “7” or “SAT” (note that Sunday is the first day). If the “L” is preceded by a number, it indicates the last of the data. For example, if “6L” is set in the week field, it means “last Friday of the month”.
  • WIndicates the latest working day (From Monday to Friday) to the specified date. For example, if 15W is set to the day field, this parameter is triggered on the working day closest to the 15th of each month. If the 15th falls on a Saturday, the next Friday (14th) is triggered. If the 15th falls on a weekend, the next Monday (16th) is triggered. If the 15th falls on a weekday (Monday to Friday), the next Monday (16th) is triggered. If the format is 1W, it is triggered on the latest working day after the first day of each month. If the 1st falls on a Saturday, it will be triggered on Monday the 3rd. (Note, only specific numbers can be set before “W”, not range “-“)
  • L 和 WCan be used in combination. If “LW” is set on the day field, it triggers on the last working day of the month.
@Configuration
public class QuartzConfig {
    @Bean
    JobDetailFactoryBean jobDetailFactoryBean(a) {
        JobDetailFactoryBean bean = new JobDetailFactoryBean();
        bean.setJobClass(MyJob2.class);
        JobDataMap map = new JobDataMap();
        map.put("name"."mike");
        bean.setJobDataMap(map);
        return bean;
    }

    @Bean
    CronTriggerFactoryBean cronTrigger(a) {
        CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
        bean.setCronExpression("0/3 * * * *?");
        // If there are more than one, the parameters can be easily separated
        bean.setJobDetail(jobDetailFactoryBean().getObject());
        return bean;
    }

    @Bean
    SchedulerFactoryBean schedulerFactoryBean(a) {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        bean.setTriggers(cronTrigger().getObject());
        returnbean; }}Copy the code

Start the test