Quartz timing task integration spring basic use

Quartz timing task

1. Dependencies that need to be imported

Dependencies required by quartz timing tasks
Because we are using the ssm framework and spring has integrated quartz, we also need to import a dependency package associated with spring and quartz, so that we can directly use the content of quartz in spring.
Insert picture description here
Spring integrated quartz package

2. Write the job class to be executed

package com.ceh.quartz;

public class MyJob {
    
    

    public void job(){
    
    
        System.out.println("Hello Quartz!");
    }

}

3. Need to use the class

Need to use the class

4. Configure the environment according to the class in the figure above

<!--配置quartz-->
    <!--1.把自定义的job类放入spring工厂中-->
    <bean id="myJob" class="com.ceh.quartz.MyJob"></bean>
    <!--2.配置你要执行的工作-->
    <bean id="jobDetailFactoryBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!--指定任务的类-->
        <property name="targetObject" ref="myJob" />
        <!--指定任务中的方法-->
        <property name="targetMethod" value="job" />
    </bean>
    <!--配置时间间隔-->
    <bean id="cronTriggerFactoryBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    	<!--你要配置那个任务的时间间隔-->
        <property name="jobDetail" ref="jobDetailFactoryBean" />
        <!--时间间隔的表达式-->
        <property name="cronExpression" value="0/3 * * * * ?" />
    </bean>
    <!--执行定时,课程表-->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <!--将指定的任务注入-->
        <property name="triggers">
            <array>
                <ref bean="cronTriggerFactoryBean"></ref>
            </array>
        </property>
    </bean>

Note: When
configuring the parameters in MethodInvokingJobDetailFactoryBean, one of the two parameters in the figure below must exist, otherwise an error will be reported
Insert picture description here

The error:
Insert picture description here

After the above configuration is completed, the project can be started:
Insert picture description here

5. cron expression

*  *  *  *  *  ?   *
秒 分 时 日  月 星期 年
	上边演示中的表达式即为每年每月每日每时每分,从0秒开始,每三秒执行一次

Special symbols allowed

Seconds 0-59,-* /
Minutes 0-59,-* /
Hours 0-23,-* /
Date 1-31,-*? / LWC
Month 1-12 or JAN-DEC,-* /
Week 1-7 Or SUN-SAT,-*? / LC #
年 (optional) Leave blank, 1970-2099,-* /

*,代表和的意思
*-代表之间
**通配符,代表所有
*x/y 从x开始,每y执行一次
*?只能在日和星期存在,一个表达式中,日和星期只能存在一个,不想要那个,就使用?
*L代表最后的意思(Last,放在日上,就是每个月最后一天)
*W和日期临近的工作日,如果选择的日是周日,那么他就会选择周一代表日
*月份和星期可以使用英文缩写
*星期是1-7也就是7是周六

Excerpt
0 0 10,14,16 * *? Every day at 10 am, 2 pm, and 4 pm
0 0/30 9-17 * *? Every half hour
0 0 12? * WED means every day.
Every Wednesday at 12 noon "0 0 12 * * ?" Trigger
"0 15 10? * *" at 12 noon every day Trigger
"0 15 10 * * ?" at 10:15 every day Trigger
"0 15 10 at 10:15 every day * *? *" Trigger
"0 15 10 * *? 2005" at 10:15 every day and trigger
"0 * 14 * * ?" at 10:15 every day in 2005 every day from 2 pm to 2:59 pm 1 minute trigger
"0 0/5 14 * * ?" Trigger every 5 minutes
from 2 pm to 2:55 pm every day, trigger "0 0/5 14,18 * * ?" every day from 2 pm to 2:55 pm Trigger
"0 0-5 14 * * ?" every 5 minutes during the period and between 6 pm and 6:55 pm. Trigger
"0 10,44 14? ​​3 WED every 1 minute between 2 pm and 2:05 pm every day
0 15 10? * MON-FRI” is triggered at 2:10 and 2:44 pm on Wednesday in March every year.
“0 15 10 15 * ?” is triggered at 10:15 from Monday to Friday at 10 am on the 15th of each month :15 trigger
"0 15 10 L * ?" trigger at 10:15 am on the last day of each month
"0 15 10? * 6L" trigger at 10:15 am on the last Friday of each month
"0 15 10? * 6L 2002-2005" triggered at 10:15 am on the last Friday of each month from 2002 to 2005
"0 15 10? * 6#3" triggered at 10:15 am on the third Friday of each month

Expression generator address

Guess you like

Origin blog.csdn.net/weixin_43431123/article/details/111938372