Spring Task 自定义定时任务类

1、导入maven坐标 spring-context

这个只要项目时这个依赖是基于SpringBoot的,如果是SpringBoot项目可以不用引入。

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

package com.zsh;

@SpringBootApplication
@EnableTransactionManagement //开启注解方式的事务管理
@Slf4j
@EnableCaching//开启缓存注解功能
@EnableScheduling
public class SkyApplication {
    public static void main(String[] args) {
        SpringApplication.run(SkyApplication.class, args);
        log.info("server started");
    }
}

3、自定义定时任务类OrderTask

package com.zsh.task;

/**
 * 自定义定时任务,实现订单状态定时处理
 */
@Component
@Slf4j
public class OrderTask {

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 处理支付超时订单
     */
    @Scheduled(cron = "0 * * * * ?")
    public void processTimeoutOrder(){
        log.info("处理支付超时订单:{}", new Date());
    }

    /**
     * 处理“派送中”状态的订单
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void processDeliveryOrder(){
        log.info("处理派送中订单:{}", new Date());
    }

}

相关推荐

  1. SpringTask定时任务

    2024-01-29 05:28:01       38 阅读
  2. SpringTask定时任务框架

    2024-01-29 05:28:01       10 阅读
  3. Spring Task 定义定时任务

    2024-01-29 05:28:01       28 阅读
  4. springboot定义cron定时任务执行

    2024-01-29 05:28:01       38 阅读
  5. SpringBoot定义starter开发:定时任务报表开发

    2024-01-29 05:28:01       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-29 05:28:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-29 05:28:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-29 05:28:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-29 05:28:01       20 阅读

热门阅读

  1. true friendship

    2024-01-29 05:28:01       31 阅读
  2. vue3使用特殊字符@、~代替路径src

    2024-01-29 05:28:01       40 阅读
  3. 【字节跳动】资深后端开发工程师-平台应用服务

    2024-01-29 05:28:01       35 阅读
  4. vue-cli 无法安装问题解决

    2024-01-29 05:28:01       35 阅读
  5. 典型相关分析

    2024-01-29 05:28:01       32 阅读
  6. leetcode670最大交换

    2024-01-29 05:28:01       33 阅读
  7. 驾照考试-科目二

    2024-01-29 05:28:01       34 阅读