基于Spring Task框架的定时任务处理

本文介绍了Spring框架的SpringTask用于定时任务调度,重点讲解了cron表达式的构成和使用,以及如何在SpringBoot项目中实现定时任务的入门案例,包括创建定时任务类、配置cron表达式和功能测试。
摘要由CSDN通过智能技术生成

1.1 介绍

Spring Task 是Spring框架提供的任务调度工具,可以按照约定的时间自动执行某个代码逻辑。

  • 定位:定时任务框架

  • 作用:定时自动执行某段Java代码

  • 应用场景:

1). 信用卡每月还款提醒

2). 银行贷款每月还款提醒

3). 火车票售票系统处理未支付订单

强调:只要是需要定时处理的场景都可以使用Spring Task

1.2 cron表达式

cron表达式其实就是一个字符串,通过cron表达式可以定义任务触发的时间

构成规则:分为6或7个域,由空格分隔开,每个域代表一个含义

每个域的含义分别为:秒、分钟、小时、日、月、周、年(可选)

举例:

2022年10月12日上午9点整 对应的cron表达式为:0 0 9 12 10 ? 2022

说明:一般的值不同时设置,其中一个设置,另一个用?表示。

比如:描述2月份的最后一天,最后一天具体是几号呢?可能是28号,也有可能是29号,所以就不能写具体数字。

为了描述这些信息,提供一些特殊的字符。这些具体的细节,我们就不用自己去手写,因为这个cron表达式,它其实有在线生成器。

cron表达式在线生成器:在线Cron表达式生成器

可以直接在这个网站上面,只要根据自己的要求去生成corn表达式即可。所以一般就不用自己去编写这个表达式。

通配符:

* 表示所有值;

? 表示未说明的值,即不关心它为何值;

- 表示一个指定的范围;

, 表示附加一个可能值;

/ 符号前表示开始时间,符号后表示每次递增的值;

cron表达式案例:

*/5 * * * * ? 每隔5秒执行一次

0 */1 * * * ? 每隔1分钟执行一次

0 0 5-15 * * ? 每天5-15点整点触发

0 0/3 * * * ? 每三分钟触发一次

0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发

0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发

0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发

0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时

0 0 10,14,16 * * ? 每天上午10点,下午2点,4点

1.3 入门案例

1.3.1 Spring Task使用步骤

1). 导入maven坐标 spring-context

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

2). 启动类添加注解 @EnableScheduling 开启任务调度

3). 自定义定时任务类

1.3.2 代码开发(例子*)

编写定时任务类:

进入xxx-server模块中

自定义定时任务类MyTask

package com.xxx.task;
​
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
​
import java.util.Date;
​
/**
 * 自定义定时任务类
 */
@Component
@Slf4j
public class MyTask {
​
    /**
     * 定时任务 每隔5秒触发一次
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void executeTask(){
        log.info("定时任务开始执行:{}",new Date());
    }
}

开启任务调度:

启动类添加注解 @EnableScheduling

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
​
@SpringBootApplication
@EnableTransactionManagement //开启注解方式的事务管理
@Slf4j
@EnableCaching
@EnableScheduling //开启任务调度
public class SkyApplication {
    public static void main(String[] args) {
        SpringApplication.run(SkyApplication.class, args);
        log.info("server started");
    }
}

1.3.3 功能测试

启动服务,查看日志

每隔5秒执行一次。

二、实例代码(订单业务自动处理)

需求:待付款,超时自动取消,需求代码开发

开发步骤:

  • 定义触发时间

  • 明确查询条件

  • 获取查询数据

  • 将满足条件的数据更新

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.List;
​
/**
 * 定时任务类,定时处理订单状态
 */
@Component
@Slf4j
public class OrderTask {
​
    @Autowired
    private OrderMapper orderMapper;
​
    /**
     * 处理超时订单的方法
     */
    @Scheduled(cron = "0 * * * * ? ") //每分钟触发一次
    public void processTimeoutOrder(){
        log.info("定时处理超时订单:{}", LocalDateTime.now());
         
        //获得当前时间15分钟前的时间
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
​
        // select * from orders where status = ? and order_time < (当前时间 - 15分钟)
        List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeLT(Orders.PENDING_PAYMENT, time);
​
        if(ordersList != null && ordersList.size() > 0){
            for (Orders orders : ordersList) {
                orders.setStatus(Orders.CANCELLED);
                orders.setCancelReason("订单超时,自动取消");
                orders.setCancelTime(LocalDateTime.now());
                orderMapper.update(orders);
            }
        }
    }
​
    /**
     * 处理一直处于派送中状态的订单
     */
    @Scheduled(cron = "0 0 1 * * ?") //每天凌晨1点触发一次
    public void processDeliveryOrder(){
        log.info("定时处理处于派送中的订单:{}",LocalDateTime.now());
​
        LocalDateTime time = LocalDateTime.now().plusMinutes(-60);
​
        List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeLT(Orders.DELIVERY_IN_PROGRESS, time);
​
        if(ordersList != null && ordersList.size() > 0){
            for (Orders orders : ordersList) {
                orders.setStatus(Orders.COMPLETED);
                orderMapper.update(orders);
            }
        }
    }
}

mapper查询订单数据

    
/**
     * 根据订单状态和下单时间查询订单
     * @param status
     * @param orderTime
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime orderTime);

订单数据库对象

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
​
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
​
/**
 * 订单
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Orders implements Serializable {
​
    /**
     * 订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
     */
    public static final Integer PENDING_PAYMENT = 1;
    public static final Integer TO_BE_CONFIRMED = 2;
    public static final Integer CONFIRMED = 3;
    public static final Integer DELIVERY_IN_PROGRESS = 4;
    public static final Integer COMPLETED = 5;
    public static final Integer CANCELLED = 6;
​
    /**
     * 支付状态 0未支付 1已支付 2退款
     */
    public static final Integer UN_PAID = 0;
    public static final Integer PAID = 1;
    public static final Integer REFUND = 2;
​
    private static final long serialVersionUID = 1L;
​
    private Long id;
​
    //订单号
    private String number;
​
    //订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消 7退款
    private Integer status;
​
    //下单用户id
    private Long userId;
​
    //地址id
    private Long addressBookId;
​
    //下单时间
    private LocalDateTime orderTime;
​
    //结账时间
    private LocalDateTime checkoutTime;
​
    //支付方式 1微信,2支付宝
    private Integer payMethod;
​
    //支付状态 0未支付 1已支付 2退款
    private Integer payStatus;
​
    //实收金额
    private BigDecimal amount;
​
    //备注
    private String remark;
​
    //用户名
    private String userName;
​
    //手机号
    private String phone;
​
    //地址
    private String address;
​
    //收货人
    private String consignee;
​
    //订单取消原因
    private String cancelReason;
​
    //订单拒绝原因
    private String rejectionReason;
​
    //订单取消时间
    private LocalDateTime cancelTime;
​
    //预计送达时间
    private LocalDateTime estimatedDeliveryTime;
​
    //配送状态  1立即送出  0选择具体时间
    private Integer deliveryStatus;
​
    //送达时间
    private LocalDateTime deliveryTime;
​
    //打包费
    private int packAmount;
​
    //餐具数量
    private int tablewareNumber;
​
    //餐具数量状态  1按餐量提供  0选择具体数量
    private Integer tablewareStatus;
}
  • 25
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
SpringTaskSpring框架提供的一个用于实现定时任务的模块。它可以帮助开发者在应用中创建、调度和管理定时任务。 要使用SpringTask,首先需要在Spring配置文件中配置一个任务调度器。可以使用`@EnableScheduling`注解来启用SpringTask,并且在需要执行定时任务的方法上使用`@Scheduled`注解来指定任务的触发条件。 例如,下面的代码展示了一个简单的定时任务配置: ```java import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @EnableScheduling public class MyTask { @Scheduled(fixedRate = 5000) // 每隔5秒执行一次任务 public void myTask() { // 执行定时任务的逻辑代码 System.out.println("定时任务执行中..."); } } ``` 在上述代码中,使用`@Component`注解将`MyTask`类注册为Spring的组件,并使用`@EnableScheduling`注解启用SpringTask。然后,在`myTask()`方法上使用`@Scheduled`注解指定了定时任务的触发条件,这里是每隔5秒执行一次。 通过以上配置,当应用启动后,定时任务就会按照指定的触发条件自动执行。 除了`fixedRate`之外,`@Scheduled`注解还支持其他的触发条件配置,如`fixedDelay`、`cron`等,开发者可以根据具体需求选择合适的触发条件。 希望以上内容对你有帮助,如果还有其他问题,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

唯手熟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值