当前位置:网站首页>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
}
}
边栏推荐
- Drug management system plus database, overnight, plus report
- 观测云与 TDengine 达成深度合作,优化企业上云体验
- IO流_递归
- Hologres shared cluster helps Taobao subscribe to the extreme refined operation
- 数据安全合规之后,给风控团队带来了新的问题
- redis淘汰策略
- The new tea drinks are "dead and alive", but the suppliers are "full of pots and bowls"?
- BC1.2 PD协议
- Sub chain cross technology source level exploration: an overview of xcvm
- register_ Chrdev and CDEV_ init cdev_ Add usage differences
猜你喜欢

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

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

Mathematical modeling for war preparation 36 time series model 2

【机器学习】K-means聚类分析

坚果云-在新电脑上同步移动硬盘的文件

Hologres共享集群助力淘宝订阅极致精细化运营

restartProcessIfVisible的流程

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

Etcd教程 — 第九章 Etcd之实现分布式锁

RT thread heap size Setting
随机推荐
Tencent two sides: @bean and @component are used on the same class. What happens?
JS ES5也可以创建常量?
编译丨迅为STM32P157开发板编译U-Boot源码
Multi terminal collaboration of Huawei accounts to create a better internet life
安全帽佩戴检测算法研究
go-micro教程 — 第一章 快速入门
STL tutorial 7-set, pair pair pair group and functor
9: Chapter 3: e-commerce engineering analysis: 4: [general module]; (to be written...)
Delete duplicates in an ordered array ii[double pointers -- unified in multiple cases]
Drug management system plus database, overnight, plus report
Jspreadsheet/ce JExcel: more data fields than the given fields (columns) will lead to blank columns. Solution
RTP sending PS stream zero copy scheme
TCP Socket与TCP 连接
补充材料 supplementary
restartProcessIfVisible的流程
坚果云-在新电脑上同步移动硬盘的文件
Etcd教程 — 第九章 Etcd之实现分布式锁
几个跨端开发神器
More dragon lizard self-developed features! Production available Anolis OS 8.6 officially released
Mathematical modeling for war preparation 35 time series prediction model