当前位置:网站首页>定时任务删除指定时间的的数据
定时任务删除指定时间的的数据
2022-06-30 15:48:00 【字符跳跃】
方法一:quartz框架解决
快速搭建项目以后
1.添加pom依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
2.将对象添加到spring容器中
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;
@Component
public class MyAdaptableJobFactory extends AdaptableJobFactory {
//AutowireCapableBeanFactory 可以将一个对象添加到SpringIOC容器中,并且完成该对象注入
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
/**
* 该方法需要将实例化的任务对象手动的添加到springIOC容器中并且完成对象的注入
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object obj = super.createJobInstance(bundle);
//将obj对象添加Spring IOC容器中,并完成注入
this.autowireCapableBeanFactory.autowireBean(obj);
return obj;
}
}
3.编写配置类
import com.zxhc.manager.component.MyAdaptableJobFactory;
import com.zxhc.manager.task.DeleteQuartzTask;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configuration
public class DeleteQuartzConfig {
/**
* 1.创建Job对象
*/
@Bean
public JobDetailFactoryBean jobDetailFactoryBean() {
JobDetailFactoryBean factory = new JobDetailFactoryBean();
//关联我们自己的Job类
factory.setJobClass(DeleteQuartzTask.class);
return factory;
}
/**
* 2.创建Trigger对象
* 简单的Trigger
*/
/**
* Cron Trigger
*/
@Bean
public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) {
CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
factory.setJobDetail(jobDetailFactoryBean.getObject());
factory.setCronExpression("0 0 0 * * ? * ");
return factory;
}
/**
* 3.创建Scheduler对象
*/
@Bean
public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean, MyAdaptableJobFactory myAdaptableJobFactory) {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
//关联trigger
factory.setTriggers(cronTriggerFactoryBean.getObject());
factory.setJobFactory(myAdaptableJobFactory);
return factory;
}
}
4.编写任务类
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Calendar;
import java.util.Date;
public class DeleteQuartzTask implements Job {
@Autowired
private DevicePositionService devicePositionService;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DATE,-7);
Date dateBeforeWeek = calendar.getTime();
//这里写逻辑,创建时间小于Date的删除
}
}
方法二:springboot自带的@EnableScheduling 配合@Scheduled实现定时任务
1.pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
2.启动类上添加@EnableScheduling关键注解
@SpringBootApplication
@EnableScheduling
public class AppApplication {
public static void main(String[] args) {
SpringApplication.run(AppApplication .class, args);
}
}
3.业务组件代码
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zxhc.manager.service.DevicePositionService;
import com.zxhc.pojo.DevicePosition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Calendar;
import java.util.Date;
@Component
public class DeleteQuartzTask{
@Autowired
private DevicePositionService devicePositionService;
@Scheduled(cron=" 0 0 0 * * *")
public void execute(){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DATE,-7);
Date dateBeforeWeek = calendar.getTime();
小于时间删除的逻辑
}
}
边栏推荐
- 2020蓝桥杯国赛B组-搬砖-(贪心排序+01背包)
- jspreadsheet/CE JExcel数据字段比给的字段(columns)多会导致空白列的问题解决方案
- 编译丨迅为iTOP4412开发板Makefile编译
- 山西化工园区智能化管控平台建设时间表
- 牛客网:最小花费爬楼梯
- 【微信小程序】常用组件基本使用(view/scroll-view/swiper、text/rich-text、button/image)
- The 25th anniversary of Hong Kong's return to China the Hong Kong Palace Museum officially opened as a new cultural landmark
- POJ Project Summer
- Niuke: how many different binary search trees are there
- 深度学习——(2)几种常见的损失函数
猜你喜欢

中航无人机科创板上市:市值385亿 拳头产品是翼龙无人机

数据挖掘知识点整理(期末复习版)

Half year inventory of new consumption in 2022: the industry is cold, but these nine tracks still attract gold

HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴
![[Verilog basics] summary of some concepts about clock signals (clock setup/hold, clock tree, clock skew, clock latency, clock transition..)](/img/54/fd7541dae4aad7e4216bcaabbb146b.png)
[Verilog basics] summary of some concepts about clock signals (clock setup/hold, clock tree, clock skew, clock latency, clock transition..)

Data security compliance has brought new problems to the risk control team

JS ES5也可以创建常量?
![[bjdctf2020]the mystery of ip|[ciscn2019 southeast China division]web11|ssti injection](/img/c2/d6760826b81589781574aebff61f9a.png)
[bjdctf2020]the mystery of ip|[ciscn2019 southeast China division]web11|ssti injection

RT thread heap size setting

搬运两个负载均衡的笔记,日后省的找
随机推荐
[Verilog basics] summary of some concepts about clock signals (clock setup/hold, clock tree, clock skew, clock latency, clock transition..)
Drug management system plus database, overnight, plus report
Raft introduction
Hologres shared cluster helps Taobao subscribe to the extreme refined operation
观测云与 TDengine 达成深度合作,优化企业上云体验
安全帽佩戴检测算法研究
Carry two load balancing notes and find them in the future
idea必用插件
Additional: (not written yet, don't look at ~ ~ ~) corsfilter filter;
differential analysis between different groups nichenet for silicosis成功运行!
牛客网:乘积为正数的最长连续子数组
go-micro教程 — 第一章 快速入门
Deep learning - (2) several common loss functions
[BJDCTF2020]The mystery of ip|[CISCN2019 华东南赛区]Web11|SSTI注入
HMS core audio editing service 3D audio technology helps create an immersive auditory feast
[machine learning] K-means clustering analysis
addmodule_allmerge_ams_im
ArcMap operation series: 80 plane to latitude and longitude 84
[Demo] 循环写文件
Lambda表达式_Stream流_File类