ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java4 -- SpringBoot定时任务调度

2022-02-11 09:59:51  阅读:223  来源: 互联网

标签:Scheduled 调用 -- springframework java4 import org 任务调度 public


一、定时删除数据库记录

首先,这个是根据业务要求的,比如是业务要求的是删除7天之前的所有记录,只保留近7天的数据,那么首先就先在数据库中修改SQL语句。

由于使用的是springBoot + Mybatis 所以需要修改的就是mappers.xml文件。

   <delete id="delete*"
            parameterType="com.windaka.suizhi.switchcloudtest.model.*">
        delete
        from
            表
        where  DATE(time) &lt; DATE(DATE_SUB(NOW(),INTERVAL 7 DAY))
        // 代表时间是今天到7天前的时间范围
    </delete>

之后,再写一个schedule配置文件

package com.*.schedul;

import com.*.dao.*;
import com.*.dao.*;
import com.*.model.*;
import com.*.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
/**
 * Created with IntelliJ IDEA.
 * 定时任务
 * @Author: 
 * @Date: 2021/12/30/9:42
 * @Version 1.0
 * @return
 * @Description:
 */
@Component
@EnableCaching
@EnableScheduling
@Slf4j
public class ScheduleConfig {
    @Autowired
    private *Mapper *Mapper;
  
    /**
     * 一分钟清理一次 ccar(车辆抓拍) 中多余的信息 防止数据堆积
     * @author 
     * @date 10:09 2021/12/30
     * @param
     * @return
     * @other
     **/
    //每天中午12点触发一次
    @Scheduled(cron = "0 0 12 * * ?")
    public void cleanCar() {
        try {
            *Car *car = new *Car();
            *Mapper.delete*(*car);
            System.out.println("车辆抓拍记录删除成功!");

        }catch (Exception e){
            log.error("删除7天前的车辆抓拍记录失败!"+e.getMessage());
        }
    }
}

注意:该代码只是一个案例,把一些具体的对象以*号表示,不能直接运行。

二、定时调用某个接口/方法

是SpringBoot项目中的定时器 (SpringBoot中的任务调度),如果是springboot项目,针对于某一个方法的调用,使用起来比较方便。

首先,在启动类上添加注解@EnableScheduling

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@EnableScheduling
public class SwitchCloudOnemachineOnegearApplication {


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

}

其次在需要使用任务调度的类上面加注解 @Component,使该类被Spring管理

之后在所要定时调度的方法上加上注解@Scheduled(cron=“0 0 0 * * ?”)。以下介绍@scheduled的属性

  • cron属性:时间表达式
  • fixedRate属性:上一个调用开始后再次调用的延时(再次调用时不需要等上一个调用执行完成)
  • fixedDelay属性:上一个调用完成后再次调用的延时(再次调用时需要等上一个调用执行完成)
  • initialDelay属性:第一次调用时的延迟时间

在这里插入图片描述

案例

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
@Component
public class ScheduleTest {
 
	//每1秒执行一次该方法
	@Scheduled(fixedRate = 1000)
	public void test1() {
		System.out.println("定时调度1------");
	}
	
	//每天12点执行一次该方法
	@Scheduled("0 0 12 * * ?")
	public void test2() {
		System.out.println("定时调度2------");
	}
	
}

有时候,如果任务调度需要配合异步功能,那么类上需要加上@EnableAsync注解,该注解是加在类上的

参考来源:https://blog.csdn.net/Eternal_Blue/article/details/94442770

标签:Scheduled,调用,--,springframework,java4,import,org,任务调度,public
来源: https://blog.csdn.net/snwown/article/details/122844148

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有