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