• 千峰商城-springboot项目搭建-85-订单超时取消-定时任务框架quartz的整合使用


    1.创建新的springboot项目

    2.导入依赖

    删除多余文件

     

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-quartzartifactId>
    4. dependency>

    3.创建定时任务

    定时任务:每隔指定的时间就执行一次任务。
    案例:每隔三秒打印一次helloworld。

     创建一个service包:

    创建PrintHelloWorldJob类:

    可以生成Scheduled中的定时语法的定时器网址:https://cron.qqe2.com/
    
    PrintHelloWorldJob :
    
    1. @Component
    2. public class PrintHelloWorldJob {
    3. //https://cron.qqe2.com
    4. //生成器,可以生成Scheduled中的定时语法
    5. @Scheduled(cron = "0/3 * * * * ?")
    6. public void printHelloWorld(){
    7. System.out.println("----------HelloWorld.");
    8. }
    9. }
    Scheduled中的定时语法:
    
    字段******
    含义小时星期
    取值0-590-590-231-31

    1-12或月份对应的

    前三个英文字母(大小写均可)

    0-7(0、7表示周日)或星期对应的

    前三个英文字母(大小写均可)

    允许的特殊字符, - * /, - * /, - * /, - * / ? L, - * /, - * / ? L

     特殊字符含义:

    特殊字符-*/L
    含义枚举区间任意值步长日/星期冲突匹配符最后
    举例

    "1,3,5 * * * * *"

    任意时间的1、3、5秒钟执行

    "0 0-5 14 * * ?"

    每天14:00-14:05触发

    "0 0 12 * * ?"

    每天12:00触发

    "0/5 * * * * *"

    每5秒触发一次

    "0 * * 26 * ?"

    每月的26日的每分钟执行

    "0 0 * L * ?"

    每月最后一日的每一小时执行

    application.properties:(由于商城项目已占用8080端口,所以修改新项目的端口为9999)
    server.port=9999
    

    在启动类上添加注解@EnableScheduling,启动定时任务:

    1. @SpringBootApplication
    2. @EnableScheduling
    3. public class QuartzDemoApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(QuartzDemoApplication.class, args);
    6. }
    7. }

    启动api,进行测试:

  • 相关阅读:
    sass和 scss的区别?
    Azure Data Factory(十)Data Flow 组件详解
    带着问题去分析:Spring Bean 生命周期
    MySQL多表查询
    Navicat for MySQL 11软件下载及安装教程
    java8之CompletableFuture
    MYSQL基础管理-auto_increment测试应用
    ESP8266-Arduino编程实例-GP2Y1010AU0F灰尘传感器驱动
    SpringBoot项目将Nacos作为配置中心与注册中心,微服务启动失败
    Golang Linux 安装与环境变量配置
  • 原文地址:https://blog.csdn.net/LYly_B/article/details/126089376