springboot整合定时任务

简介: 定时任务

一.创建定时任务类,使用cron表达式
package com.wxit.staservice.scheduled;

import com.wxit.staservice.service.StatisticsDailyService;
import com.wxit.staservice.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**

  • @author wj
  • @date 2021.12.25 15:14

*/
@Component
public class ScheduledTask {

@Autowired
private StatisticsDailyService statisticsDailyService;

/**
 * 测试
 * 每天七点到二十三点每五秒执行一次
 */
@Scheduled(cron = "0/5 * * * * ?")
public void task1() {
    System.out.println("*********++++++++++++*****执行了");
}

/**
 * 每天凌晨1点执行定时
 */
@Scheduled(cron = "0 0 1 * * ?")
public void task2() {
    //获取上一天的日期
    String day = DateUtil.formatDate(DateUtil.addDays(new Date(), -1));
    statisticsDailyService.registerCount(day);
}

}
二.在启动类上添加注解

package com.wxit.staservice;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

/**

  • @author wj
  • @date 2021.12.24 14:40

*/
@SpringBootApplication
@ComponentScan(basePackages = {"com.wxit"})
@EnableFeignClients
@EnableDiscoveryClient
@MapperScan("com.wxit.staservice.mapper")
@EnableScheduling //开启定时任务
public class StaApplication {

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

}

三.在线生成corn表达式
http://cron.qqe2.com/
具体生成规则见官网教程

相关文章
|
4月前
|
存储 Java Spring
【八】springboot整合定时任务
【八】springboot整合定时任务
47 0
|
4月前
|
Java 调度
SpringBoot中实现定时任务(Quartz)(二)
SpringBoot中实现定时任务(Quartz)
49 0
SpringBoot中实现定时任务(Quartz)(二)
|
4月前
|
Java 调度 Spring
SpringBoot中实现定时任务(Quartz)(一)
SpringBoot中实现定时任务(Quartz)
65 0
SpringBoot中实现定时任务(Quartz)(一)
|
10月前
|
Java 关系型数据库 MySQL
SpringBoot实现定时任务
SpringBoot实现定时任务
|
Java 调度 数据库
|
Java 容器
SpringBoot 整合定时任务
SpringBoot 整合定时任务
|
Java
springboot 定时任务遇到的坑
springboot 定时任务遇到的坑
196 0
|
Java BI 调度
SpringBoot——整合定时任务
SpringBoot——整合定时任务
247 0
SpringBoot——整合定时任务
|
Java 容器 Spring
SpringBoot定时任务源码分析
写作目的 最近看了一篇博客 “Spring Boot实现定时任务的动态增删启停” ,其实实现这个需求的前提是你要搞明白 定时任务 的实现原理,这样你才有可能实现定时任务的动态增删启停,所以下面从源码的角度跟 SpringBoot定时任务原理。
241 0
SpringBoot定时任务源码分析
5、SpringBoot2.0实现定时任务(五)
引入相关定时任务相关的依赖
83 0
5、SpringBoot2.0实现定时任务(五)