SpringBoot定时任务
在日常开发中我们会遇到让某个功能在某个固定的时间去执行,这个时候就可以用到定时任务类了
注解:
@EnableScheduling | 开启定时任务 | 写在启动类的注解 |
---|---|---|
@Component | 注册组件 | 写在你定时任务类上 |
@Scheduled | 做定时任务 | 写在定时任务方法上 |
在这里写贴一下代码
在SpringBoot项目 核心启动类 上添加注解@EnableScheduling 开启定时任务
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.net.InetAddress;
/***
* @author gxh
* @date 2021-8-21
*/
@EnableSwagger2 //开启swagger文档
@EnableAsync //开启异步
@EnableScheduling //开启定时任务
@SpringBootApplication
public class SpringBootAndMpApplication {
public static void main(String[] args) throws Exception{
Long startTime = System.currentTimeMillis();
ConfigurableApplicationContext application = SpringApplication.run(SpringBootAndMpApplication.class, args);
Long endTime = System.currentTimeMillis();
Environment env = application.getEnvironment();
String ip = InetAddress.getLocalHost().getHostAddress();
String port = env.getProperty("server.port");
String path = env.getProperty("server.servlet.context-path");
System.out.println("=======================================>");
System.out.println("启动耗时=======>" + (endTime -startTime));
System.out.println("Local:http://localhost:" + port + "/swagger-ui.html");
System.out.println("serve start-up success");
System.out.println("=======================================>");
}
}
创建一个常量类,将用到的cron表达式定义成常量
/****
* 定时任务表达式常量接口
* 0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
* 0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
* 0 0 12 ? * WED 表示每个星期三中午12点
* "0 0 12 * * ?" 每天中午12点触发
* "0 15 10 ? * *" 每天上午10:15触发
* "0 15 10 * * ?" 每天上午10:15触发
* "0 15 10 * * ? *" 每天上午10:15触发
* "0 15 10 * * ? 2005" 2005年的每天上午10:15触发
* "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
* "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
* "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
* "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
* "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
* "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
* "0 15 10 15 * ?" 每月15日上午10:15触发
* "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
* "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
* "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
*
* "0 0/3 * * * ?"" ""
*
* "0 15 10 28-31 * ?"; 每月最后一日的上午10:15触发
*
*
*
*/
public class TimedTaskCronConstant {
// /**
// * 启动时执行一次,之后每隔2秒执行一次
// */
// @Scheduled(fixedRate = 1000*2)
// public void print() {
// System.out.println("print method 2");
// }
//每天凌晨执行一次 0 0 0 * * ?
//每秒执行一次 * * * * * ?
//每天中午12点触发 0 0 12 * * ?
//每天 18 点执行 0 0 18 * * ?
/***
* 任务表达式
*/
//每天凌晨执行
public static final String FAIR_CRON = "0 0 0 * * ?";
//五秒执行
public static final String FIVE_SECOND_CRON = "0/5 * * * * ?";
//每月最后一日的上午10:15触发
public static final String MONTH_REPORT_CRON = "0 15 10 28-31 * ?";
//每月的1日的凌晨2点调整任务
// public static final String MONTH_EVERY_CRON="0 0 2 1 * ? *";
//每月最后一日的02:30触发
public static final String MONTH_EVERY_CRON="0 30 02 28-31 * ?";
//每隔15分钟
public static final String EVERY_OTHER_FIFTY = "0 */15 * * * ?";
//每隔20分钟
public static final String EVERY_OTHER_TWENTY = "0 */20 * * * ?";
}
创建定时任务类,用来做主要的业务
package com.gxh.modules.util;
import com.gxh.modules.constant.TimedTaskCronConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Author gxh
* @Data 2021/8/26
*/
@Component //注册组件
@Slf4j //日志
public class TimedTaskCoreBean {
/**日期格式化*/
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
//模拟的业务类
@Autowrid
private ISystemContextAPI systemApi;
/***
* 定时任务类 可以在这个类中做主要的业务逻辑
*
* cron cron表达式
* @return
*/
@Scheduled(cron = TimedTaskCronConstant.EVERY_OTHER_FIFTY)
public String test(){
return "str";
}
/**
* 定时清除系统日志
* 每月 1 号
*/
@Scheduled(cron = TimedTaskCronConstant.MONTH_EVERY_CRON)
public void clearSystemLog(){
log.info("开始执行清除系统日志.......");
//模拟的方法
systemApi.clearSystemLog();
log.info("执行清除系统日志完毕"+getNowTime());
}
/**
* 获取当前时间
*/
public static String getNowTime(){
String thisTime = simpleDateFormat.format(new Date());
return thisTime;
}
}