当前位置:网站首页>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
}
}
边栏推荐
- Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column
- 安全帽佩戴检测算法研究
- Simpleitk encountered an ITK only supports orthonormal direction cosines error while reading NII
- 9: Chapter 3: e-commerce engineering analysis: 4: [general module]; (to be written...)
- Niuke.com: minimum cost of climbing stairs
- The meaning of linetypes enumeration values (line_4, line_8, line_aa) in opencv
- 牛客网:最小花费爬楼梯
- go-micro教程 — 第一章 快速入门
- [machine learning] K-means clustering analysis
- STL教程7-set、pair对组和仿函数
猜你喜欢

数据库系统概论习题册

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

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

【牛客网刷题系列 之 Verilog快速入门】~ 位拆分与运算

华为帐号多端协同,打造美好互联生活

Mathematical modeling for war preparation 36 time series model 2

八大基本排序(详解)

The meaning of linetypes enumeration values (line_4, line_8, line_aa) in opencv

Data mining knowledge points sorting (final review version)

RT thread heap size Setting
随机推荐
Mathematical modeling for war preparation 36 time series model 2
药品管理系统加数据库,一夜做完,加报告
List announced - outstanding intellectual property service team in China in 2021
Etcd教程 — 第九章 Etcd之实现分布式锁
【微信小程序】常用组件基本使用(view/scroll-view/swiper、text/rich-text、button/image)
9:第三章:电商工程分析:4:【通用模块】;(待写……)
八大基本排序(详解)
OpenCV中LineTypes各枚举值(LINE_4 、LINE_8 、LINE_AA )的含义
Etcd tutorial - Chapter 8 compact, watch, and lease APIs for etcd
Php7.3 centos7.9 installing sqlserver extensions
Lambda表达式_Stream流_File类
坚果云-在新电脑上同步移动硬盘的文件
编译丨迅为iTOP4412开发板Makefile编译
Cmakelists Basics
Dart: string replace related methods
Delete duplicates in an ordered array ii[double pointers -- unified in multiple cases]
Headhunter 50, 000, I'll go to VC
Restartprocessifvisible process
Mathematical modeling for war preparation 34-bp neural network prediction 2
register_chrdev和cdev_init cdev_add用法区别