update 重构 QueueUtils 抽取通用方法 统一使用 适配优先队列新用法
This commit is contained in:
parent
1920ba94b7
commit
bd338dd934
2
pom.xml
2
pom.xml
@ -31,7 +31,7 @@
|
||||
<hutool.version>5.8.5</hutool.version>
|
||||
<okhttp.version>4.10.0</okhttp.version>
|
||||
<spring-boot-admin.version>2.7.4</spring-boot-admin.version>
|
||||
<redisson.version>3.17.5</redisson.version>
|
||||
<redisson.version>3.17.6</redisson.version>
|
||||
<lock4j.version>2.2.2</lock4j.version>
|
||||
<dynamic-ds.version>3.5.1</dynamic-ds.version>
|
||||
<tlog.version>1.4.3</tlog.version>
|
||||
|
@ -42,7 +42,7 @@ public class QueueUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个普通队列数据 没有数据返回 null
|
||||
* 通用获取一个队列数据 没有数据返回 null(不支持延迟队列)
|
||||
*
|
||||
* @param queueName 队列名
|
||||
*/
|
||||
@ -52,13 +52,21 @@ public class QueueUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除普通队列数据
|
||||
* 通用删除队列数据(不支持延迟队列)
|
||||
*/
|
||||
public static <T> boolean removeQueueObject(String queueName, T data) {
|
||||
RBlockingQueue<T> queue = CLIENT.getBlockingQueue(queueName);
|
||||
return queue.remove(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用销毁队列 所有阻塞监听 报错(不支持延迟队列)
|
||||
*/
|
||||
public static <T> boolean destroyQueue(String queueName) {
|
||||
RBlockingQueue<T> queue = CLIENT.getBlockingQueue(queueName);
|
||||
return queue.delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加延迟队列数据 默认毫秒
|
||||
*
|
||||
@ -113,17 +121,6 @@ public class QueueUtils {
|
||||
delayedQueue.destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试设置 优先队列比较器 用于排序优先级
|
||||
*
|
||||
* @param queueName 队列名
|
||||
* @param comparator 比较器
|
||||
*/
|
||||
public static <T> boolean trySetPriorityQueueComparator(String queueName, Comparator<T> comparator) {
|
||||
RPriorityBlockingQueue<T> priorityBlockingQueue = CLIENT.getPriorityBlockingQueue(queueName);
|
||||
return priorityBlockingQueue.trySetComparator(comparator);
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试设置 优先队列比较器 用于排序优先级
|
||||
*
|
||||
@ -134,7 +131,7 @@ public class QueueUtils {
|
||||
public static <T> boolean trySetPriorityQueueComparator(String queueName, Comparator<T> comparator, boolean destroy) {
|
||||
RPriorityBlockingQueue<T> priorityBlockingQueue = CLIENT.getPriorityBlockingQueue(queueName);
|
||||
if (priorityBlockingQueue.isExists() && destroy) {
|
||||
destroyPriorityQueueObject(queueName);
|
||||
destroyQueue(queueName);
|
||||
}
|
||||
return priorityBlockingQueue.trySetComparator(comparator);
|
||||
}
|
||||
@ -150,32 +147,6 @@ public class QueueUtils {
|
||||
return priorityBlockingQueue.offer(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个优先队列数据 没有数据返回 null
|
||||
*
|
||||
* @param queueName 队列名
|
||||
*/
|
||||
public static <T> T getPriorityQueueObject(String queueName) {
|
||||
RPriorityBlockingQueue<T> priorityBlockingQueue = CLIENT.getPriorityBlockingQueue(queueName);
|
||||
return priorityBlockingQueue.poll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除优先队列数据
|
||||
*/
|
||||
public static <T> boolean removePriorityQueueObject(String queueName, T data) {
|
||||
RPriorityBlockingQueue<T> priorityBlockingQueue = CLIENT.getPriorityBlockingQueue(queueName);
|
||||
return priorityBlockingQueue.remove(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁优先队列
|
||||
*/
|
||||
public static boolean destroyPriorityQueueObject(String queueName) {
|
||||
RPriorityBlockingQueue<?> priorityBlockingQueue = CLIENT.getPriorityBlockingQueue(queueName);
|
||||
return priorityBlockingQueue.delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试设置 有界队列 容量 用于限制数量
|
||||
*
|
||||
@ -197,7 +168,7 @@ public class QueueUtils {
|
||||
public static <T> boolean trySetBoundedQueueCapacity(String queueName, int capacity, boolean destroy) {
|
||||
RBoundedBlockingQueue<T> boundedBlockingQueue = CLIENT.getBoundedBlockingQueue(queueName);
|
||||
if (boundedBlockingQueue.isExists() && destroy) {
|
||||
destroyBoundedQueueObject(queueName);
|
||||
destroyQueue(queueName);
|
||||
}
|
||||
return boundedBlockingQueue.trySetCapacity(capacity);
|
||||
}
|
||||
@ -214,32 +185,6 @@ public class QueueUtils {
|
||||
return boundedBlockingQueue.offer(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个有界队列数据 没有数据返回 null
|
||||
*
|
||||
* @param queueName 队列名
|
||||
*/
|
||||
public static <T> T getBoundedQueueObject(String queueName) {
|
||||
RBoundedBlockingQueue<T> boundedBlockingQueue = CLIENT.getBoundedBlockingQueue(queueName);
|
||||
return boundedBlockingQueue.poll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除有界队列数据
|
||||
*/
|
||||
public static <T> boolean removeBoundedQueueObject(String queueName, T data) {
|
||||
RBoundedBlockingQueue<T> boundedBlockingQueue = CLIENT.getBoundedBlockingQueue(queueName);
|
||||
return boundedBlockingQueue.remove(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁有界队列
|
||||
*/
|
||||
public static boolean destroyBoundedQueueObject(String queueName) {
|
||||
RBoundedBlockingQueue<?> boundedBlockingQueue = CLIENT.getBoundedBlockingQueue(queueName);
|
||||
return boundedBlockingQueue.delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅阻塞队列(可订阅所有实现类 例如: 延迟 优先 有界 等)
|
||||
*/
|
||||
|
@ -35,7 +35,7 @@ public class BoundedQueueController {
|
||||
@GetMapping("/add")
|
||||
public R<Void> add(String queueName, int capacity) {
|
||||
// 用完了一定要销毁 否则会一直存在
|
||||
boolean b = QueueUtils.destroyBoundedQueueObject(queueName);
|
||||
boolean b = QueueUtils.destroyQueue(queueName);
|
||||
log.info("通道: {} , 删除: {}", queueName, b);
|
||||
// 初始化设置一次即可
|
||||
if (QueueUtils.trySetBoundedQueueCapacity(queueName, capacity)) {
|
||||
@ -64,7 +64,7 @@ public class BoundedQueueController {
|
||||
@GetMapping("/remove")
|
||||
public R<Void> remove(String queueName) {
|
||||
String data = "data-" + 5;
|
||||
if (QueueUtils.removeBoundedQueueObject(queueName, data)) {
|
||||
if (QueueUtils.removeQueueObject(queueName, data)) {
|
||||
log.info("通道: {} , 删除数据: {}", queueName, data);
|
||||
} else {
|
||||
return R.fail("操作失败");
|
||||
@ -81,7 +81,7 @@ public class BoundedQueueController {
|
||||
public R<Void> get(String queueName) {
|
||||
String data;
|
||||
do {
|
||||
data = QueueUtils.getBoundedQueueObject(queueName);
|
||||
data = QueueUtils.getQueueObject(queueName);
|
||||
log.info("通道: {} , 获取数据: {}", queueName, data);
|
||||
} while (data != null);
|
||||
return R.ok("操作成功");
|
||||
|
@ -2,6 +2,7 @@ package com.ruoyi.demo.controller.queue;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* 实体类 注意不允许使用内部类 否则会找不到类
|
||||
@ -11,7 +12,12 @@ import lombok.NoArgsConstructor;
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class PriorityDemo {
|
||||
public class PriorityDemo implements Comparable<PriorityDemo> {
|
||||
private String name;
|
||||
private Integer orderNum;
|
||||
|
||||
@Override
|
||||
public int compareTo(@NotNull PriorityDemo other) {
|
||||
return Integer.compare(getOrderNum(), other.getOrderNum());
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +0,0 @@
|
||||
package com.ruoyi.demo.controller.queue;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* 比较器 注意不允许使用 内部类或匿名类或lambda表达式 会找不到类
|
||||
*
|
||||
* @author Lion Li
|
||||
* @version 3.6.0
|
||||
*/
|
||||
public class PriorityDemoComparator implements Comparator<PriorityDemo> {
|
||||
@Override
|
||||
public int compare(PriorityDemo pd1, PriorityDemo pd2) {
|
||||
return Integer.compare(pd1.getOrderNum(), pd2.getOrderNum());
|
||||
}
|
||||
}
|
@ -34,16 +34,9 @@ public class PriorityQueueController {
|
||||
@GetMapping("/add")
|
||||
public R<Void> add(String queueName) {
|
||||
// 用完了一定要销毁 否则会一直存在
|
||||
boolean b = QueueUtils.destroyPriorityQueueObject(queueName);
|
||||
boolean b = QueueUtils.destroyQueue(queueName);
|
||||
log.info("通道: {} , 删除: {}", queueName, b);
|
||||
// 初始化设置一次即可 此处注意 不允许用内部类或匿名类
|
||||
boolean flag = QueueUtils.trySetPriorityQueueComparator(queueName, new PriorityDemoComparator());
|
||||
if (flag) {
|
||||
log.info("通道: {} , 设置比较器成功", queueName);
|
||||
} else {
|
||||
log.info("通道: {} , 设置比较器失败", queueName);
|
||||
return R.fail("操作失败");
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int randomNum = RandomUtil.randomInt(10);
|
||||
PriorityDemo data = new PriorityDemo();
|
||||
@ -70,7 +63,7 @@ public class PriorityQueueController {
|
||||
PriorityDemo data = new PriorityDemo();
|
||||
data.setName(name);
|
||||
data.setOrderNum(orderNum);
|
||||
if (QueueUtils.removePriorityQueueObject(queueName, data)) {
|
||||
if (QueueUtils.removeQueueObject(queueName, data)) {
|
||||
log.info("通道: {} , 删除数据: {}", queueName, data);
|
||||
} else {
|
||||
return R.fail("操作失败");
|
||||
@ -87,7 +80,7 @@ public class PriorityQueueController {
|
||||
public R<Void> get(String queueName) {
|
||||
PriorityDemo data;
|
||||
do {
|
||||
data = QueueUtils.getPriorityQueueObject(queueName);
|
||||
data = QueueUtils.getQueueObject(queueName);
|
||||
log.info("通道: {} , 获取数据: {}", queueName, data);
|
||||
} while (data != null);
|
||||
return R.ok("操作成功");
|
||||
|
Loading…
x
Reference in New Issue
Block a user