SpringBootDelayQueue实现延时队列
亲测有效、你们也可直接复制下⾯代码先测试⼀遍是否可⾏,然后在根据⾃⼰业务进⾏修改
⼀、Delayed
package Queue;
import TimeUnit;
/**
* @author LunarYouI
* @create 2021-09-24 10:31
*/
public interface Delayed extends Comparable<Delayed>{
/**
* Returns the remaining delay associated with this object, in the
* given time unit.
*
* @param unit the time unit
* @return the remaining delay; zero or negative values indicate
* that the delay has already elapd
*/
long getDelay(TimeUnit unit);
}
⼆、DelayTask
*/
import Date;
import Delayed;
折花大全
import TimeUnit;
public class DelayTask implements Delayed {
final private TaskBa data;
final private long expire;
/**奋斗演讲稿
* 构造延时任务
* @param data 业务数据
* @param expire 任务延时时间(ms)
*/
public DelayTask(TaskBa data,long expire){
super();
this.data = data;
}
public TaskBa getData(){
return data;
}
public long getExpire(){
return expire;
}
@Override
public boolean equals(Object obj){
if(obj instanceof DelayTask){
return Identifier().equals(((DelayTask) obj).getData().getIdentifier());
}
return fal;
}
@Override
public String toString(){
return"{"+"data:"+ String()+","+"expire:"+new Date(expire)+"}";
}
@Override
public long getDelay(TimeUnit unit){
pire - System.currentTimeMillis(), unit);
}
@Override
public int compareTo(Delayed o){
long delta =getDelay(TimeUnit.NANOSECONDS)- o.getDelay(TimeUnit.NANOSECONDS);
return(int) delta;
极路由>员工关系
}
}
三、TaskBa
*/
import JSON;
public class TaskBa {
private String identifier;
public TaskBa(String identifier){
this.identifier = identifier;
}
public String getIdentifier(){
return identifier;
}
public void tIdentifier(String identifier){
this.identifier = identifier;
}
@Override
public String toString(){
JSONString(this);
}
}
四、DelayQueueManager
package Queue;
import Logger;
import LoggerFactory;
import CommandLineRunner;
import Component;
import SimpleDateFormat;
import Date;
import DelayQueue;
import Executors;
@Component
public class DelayQueueManager implements CommandLineRunner {
private final Logger logger = Logger(DelayQueueManager.class); private DelayQueue<DelayTask> delayQueue =new DelayQueue<>();
/**
* 加⼊到延时队列中
* @param task
*/
public void put(DelayTask task){
logger.info("加⼊延时任务:{}", task);
delayQueue.put(task);
}
/**
* 取消延时任务
* @param task
* @return
*/
public boolean remove(DelayTask task){
logger.info("取消延时任务:{}", task);宝贝回家志愿者协会
ve(task);
}
/**
* 取消延时任务
* @param taskid
* @return
*/
public boolean remove(String taskid){
return remove(new DelayTask(new TaskBa(taskid),0));
}
@Override
public void args)throws Exception {
logger.info("初始化延时队列");
}
/**
* 延时任务执⾏线程
*/
private void excuteThread(){
while(true){
try{
DelayTask task = delayQueue.take();
抛物线的标准方程>吃什么生精processTask(task);
物业管理方案范本
}catch(InterruptedException e){
break;
}
}
}
/**
* 内部执⾏延时任务
* @param task
*/
public void processTask(DelayTask task){
logger.info("执⾏延时任务:{}", task);
SimpleDateFormat dateFormat_min=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");//设置当前时间的格式,为年-⽉-⽇时-分-秒 System.out.println(dateFormat_min.format(new Date())+"=================="+"执⾏延时队列");
//根据task中的data⾃定义数据来处理相关逻辑,例 if (Data() instanceof XXX) {}
}
}
五、测试效果
我⾃⼰随便找了个controller来进⾏测试
我也忘记抄的谁的博客了,这⾥就随便找了个链接,都⼤差不差的