SpringBoot整合定时任务

1.首先需要在启动类加上注解

@EnableScheduling
public class StaApplication {
    public static void main(String[] args) {
        SpringApplication.run(StaApplication.class, args);
    }
}

2.创建定时任务类

在这个类里面使用表达式来设置什么时候去执行

1.cron表达式,设置执行规则(也称为七子表达式或者七域表达式)

2.在线生成cron表达式

在线Cron表达式生成器

3.编写代码

package com.xingchen.statistic.schedule;

import com.xingchen.statistic.service.StatisticsDailyService;

import com.xingchen.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ScheduleTask {
    @Autowired
    private StatisticsDailyService statisticsDailyService;

    //在每天的凌晨一点,把前一天的数据进行添加
    @Scheduled(cron = "0 0 1 * * ?")
    public void task1() {
        //DateUtil工具类生成前一天时间
        String day = DateUtil.formatDate(DateUtil.addDays(new Date(), -1));
        statisticsDailyService.countRegister(day);
    }


    //每隔5秒执行一次
//    @Scheduled(cron = "0/5 * * * * ?")
//    public void task1() {
//        long l = System.currentTimeMillis();
//        System.out.println("*********++++++++++++*****执行了"+l);
//    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林寻星辰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值
>