diff --git a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/utils/QueueUtils.java b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/utils/QueueUtils.java index 45b54966..f0fcb951 100644 --- a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/utils/QueueUtils.java +++ b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/utils/QueueUtils.java @@ -142,6 +142,32 @@ public class QueueUtils { return boundedBlockingQueue.trySetCapacity(capacity); } + /** + * 优先队列获取一个队列数据 没有数据返回 null(不支持延迟队列) + * + * @param queueName 队列名 + */ + public static T getPriorityQueueObject(String queueName) { + RPriorityBlockingQueue queue = CLIENT.getPriorityBlockingQueue(queueName); + return queue.poll(); + } + + /** + * 优先队列删除队列数据(不支持延迟队列) + */ + public static boolean removePriorityQueueObject(String queueName, T data) { + RPriorityBlockingQueue queue = CLIENT.getPriorityBlockingQueue(queueName); + return queue.remove(data); + } + + /** + * 优先队列销毁队列 所有阻塞监听 报错(不支持延迟队列) + */ + public static boolean destroyPriorityQueue(String queueName) { + RPriorityBlockingQueue queue = CLIENT.getPriorityBlockingQueue(queueName); + return queue.delete(); + } + /** * 尝试设置 有界队列 容量 用于限制数量 * @@ -169,6 +195,32 @@ public class QueueUtils { return boundedBlockingQueue.offer(data); } + /** + * 有界队列获取一个队列数据 没有数据返回 null(不支持延迟队列) + * + * @param queueName 队列名 + */ + public static T getBoundedQueueObject(String queueName) { + RBoundedBlockingQueue queue = CLIENT.getBoundedBlockingQueue(queueName); + return queue.poll(); + } + + /** + * 有界队列删除队列数据(不支持延迟队列) + */ + public static boolean removeBoundedQueueObject(String queueName, T data) { + RBoundedBlockingQueue queue = CLIENT.getBoundedBlockingQueue(queueName); + return queue.remove(data); + } + + /** + * 有界队列销毁队列 所有阻塞监听 报错(不支持延迟队列) + */ + public static boolean destroyBoundedQueue(String queueName) { + RBoundedBlockingQueue queue = CLIENT.getBoundedBlockingQueue(queueName); + return queue.delete(); + } + /** * 订阅阻塞队列(可订阅所有实现类 例如: 延迟 优先 有界 等) */ diff --git a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/queue/BoundedQueueController.java b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/queue/BoundedQueueController.java index b105ae91..f2fae0bf 100644 --- a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/queue/BoundedQueueController.java +++ b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/queue/BoundedQueueController.java @@ -1,9 +1,11 @@ package org.dromara.demo.controller.queue; +import cn.dev33.satoken.annotation.SaIgnore; import org.dromara.common.core.domain.R; import org.dromara.common.redis.utils.QueueUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.redisson.api.RBoundedBlockingQueue; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -19,6 +21,7 @@ import org.springframework.web.bind.annotation.RestController; * @author Lion Li * @version 3.6.0 */ +@SaIgnore @Slf4j @RequiredArgsConstructor @RestController @@ -35,7 +38,7 @@ public class BoundedQueueController { @GetMapping("/add") public R add(String queueName, int capacity) { // 用完了一定要销毁 否则会一直存在 - boolean b = QueueUtils.destroyQueue(queueName); + boolean b = QueueUtils.destroyBoundedQueue(queueName); log.info("通道: {} , 删除: {}", queueName, b); // 初始化设置一次即可 if (QueueUtils.trySetBoundedQueueCapacity(queueName, capacity)) { @@ -64,7 +67,7 @@ public class BoundedQueueController { @GetMapping("/remove") public R remove(String queueName) { String data = "data-" + 5; - if (QueueUtils.removeQueueObject(queueName, data)) { + if (QueueUtils.removeBoundedQueueObject(queueName, data)) { log.info("通道: {} , 删除数据: {}", queueName, data); } else { return R.fail("操作失败"); @@ -81,7 +84,7 @@ public class BoundedQueueController { public R get(String queueName) { String data; do { - data = QueueUtils.getQueueObject(queueName); + data = QueueUtils.getBoundedQueueObject(queueName); log.info("通道: {} , 获取数据: {}", queueName, data); } while (data != null); return R.ok("操作成功"); diff --git a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/queue/PriorityQueueController.java b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/queue/PriorityQueueController.java index 1989d9ee..02c73965 100644 --- a/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/queue/PriorityQueueController.java +++ b/ruoyi-modules/ruoyi-demo/src/main/java/org/dromara/demo/controller/queue/PriorityQueueController.java @@ -34,7 +34,7 @@ public class PriorityQueueController { @GetMapping("/add") public R add(String queueName) { // 用完了一定要销毁 否则会一直存在 - boolean b = QueueUtils.destroyQueue(queueName); + boolean b = QueueUtils.destroyPriorityQueue(queueName); log.info("通道: {} , 删除: {}", queueName, b); for (int i = 0; i < 10; i++) { @@ -63,7 +63,7 @@ public class PriorityQueueController { PriorityDemo data = new PriorityDemo(); data.setName(name); data.setOrderNum(orderNum); - if (QueueUtils.removeQueueObject(queueName, data)) { + if (QueueUtils.removePriorityQueueObject(queueName, data)) { log.info("通道: {} , 删除数据: {}", queueName, data); } else { return R.fail("操作失败"); @@ -80,7 +80,7 @@ public class PriorityQueueController { public R get(String queueName) { PriorityDemo data; do { - data = QueueUtils.getQueueObject(queueName); + data = QueueUtils.getPriorityQueueObject(queueName); log.info("通道: {} , 获取数据: {}", queueName, data); } while (data != null); return R.ok("操作成功");