SpringBoot定时任务

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;
    }
}

下期更新在线动态配置管理定时任务

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小白的小,小白的白

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

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

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

打赏作者

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

抵扣说明:

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

余额充值