当前位置:网站首页>A scheduled task deletes data at a specified time
A scheduled task deletes data at a specified time
2022-06-30 17:01:00 【Character jump】
Method 1 :quartz Framework solution
After quickly building the project
1. add to pom rely on
<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. Add objects to spring In the container
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 You can add an object to SpringIOC In the container , And complete the object Injection
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
/**
* This method needs to manually add the instantiated task object to springIOC Container and complete the injection of objects
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object obj = super.createJobInstance(bundle);
// take obj Object to add Spring IOC In the container , And complete the injection
this.autowireCapableBeanFactory.autowireBean(obj);
return obj;
}
}
3. Write configuration classes
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. establish Job object
*/
@Bean
public JobDetailFactoryBean jobDetailFactoryBean() {
JobDetailFactoryBean factory = new JobDetailFactoryBean();
// Connected to our own Job class
factory.setJobClass(DeleteQuartzTask.class);
return factory;
}
/**
* 2. establish Trigger object
* ordinary 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. establish Scheduler object
*/
@Bean
public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean, MyAdaptableJobFactory myAdaptableJobFactory) {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
// relation trigger
factory.setTriggers(cronTriggerFactoryBean.getObject());
factory.setJobFactory(myAdaptableJobFactory);
return factory;
}
}
4. Write a task class
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();
// Here's the logic , The creation time is less than Date The deletion of
}
}
Method 2 :springboot Self contained @EnableScheduling coordination @Scheduled Achieve timed tasks
1.pom rely on
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
2. Add on startup class @EnableScheduling Key notes
@SpringBootApplication
@EnableScheduling
public class AppApplication {
public static void main(String[] args) {
SpringApplication.run(AppApplication .class, args);
}
}
3. Business component code
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();
Logic deleted before time
}
}
边栏推荐
- CGR 21 (D,E,F)
- php7.3 centos7.9安装sqlserver扩展
- RT-Thread 堆区大小设置
- Etcd教程 — 第八章 Etcd之Compact、Watch和Lease API
- geo 读取单细胞csv表达矩阵 单细胞 改列名 seurat
- 编译丨迅为iTOP4412开发板Makefile编译
- In order to make remote work unaffected, I wrote an internal chat room | community essay
- [Verilog basics] summary of some concepts about clock signals (clock setup/hold, clock tree, clock skew, clock latency, clock transition..)
- 9: Chapter 3: e-commerce engineering analysis: 4: [general module]; (to be written...)
- Hologres shared cluster helps Taobao subscribe to the extreme refined operation
猜你喜欢

Installing jupyter notebook under Anaconda

On July 2, I invited you to TD Hero online conference
![删除有序数组中的重复项 II[双指针--多情况统一]](/img/e2/cadfdbe476a86cb2d72c1ae0160a4a.png)
删除有序数组中的重复项 II[双指针--多情况统一]

halcon知识:区域专题【07】

商单视频播放超2000万!农院改造为何屡被催更?

TCP socket and TCP connection

Deep learning - (2) several common loss functions

香港回归25周年 香港故宫博物馆正式开放成文化新地标

Eight basic sorting (detailed explanation)
![[Verilog quick start of Niuke online question series] ~ bit splitting and operation](/img/17/4b8f5607c4cba1596435233a6cf30a.png)
[Verilog quick start of Niuke online question series] ~ bit splitting and operation
随机推荐
9: Chapter 3: e-commerce engineering analysis: 4: [general module]; (to be written...)
商单视频播放超2000万!农院改造为何屡被催更?
Etcd教程 — 第九章 Etcd之实现分布式锁
JS Es5 can also create constants?
观测云与 TDengine 达成深度合作,优化企业上云体验
7 月 2 日邀你来TD Hero 线上发布会
【Verilog基础】关于Clock信号的一些概念总结(clock setup/hold、clock tree、clock skew、clock latency、clock transition..)
OpenCV中LineTypes各枚举值(LINE_4 、LINE_8 、LINE_AA )的含义
nichenet实战silicosis
[machine learning] K-means clustering analysis
【微信小程序】常用组件基本使用(view/scroll-view/swiper、text/rich-text、button/image)
[bjdctf2020]the mystery of ip|[ciscn2019 southeast China division]web11|ssti injection
Bidding announcement: Taizhou Unicom Oracle all in one machine and database maintenance service project in 2022
【牛客网刷题系列 之 Verilog快速入门】~ 位拆分与运算
MC Instruction Decoder
In order to make remote work unaffected, I wrote an internal chat room | community essay
深度学习——(2)几种常见的损失函数
idea必用插件
RTP sending PS stream zero copy scheme
Data mining knowledge points sorting (final review version)