Clarify responsibilities and eliminate redundancy in BlockingQueueManager

pull/1612/head
mingri31164 11 months ago
parent 2efaa3a56d
commit 0ddbcdd04c

@ -51,13 +51,15 @@ com.example.queue.MyArrayBlockingQueue
### 3.1 队列创建与验证
```java
// 创建队列
BlockingQueue<T> q = BlockingQueueManager.createQueue(queueType, capacity);
// 创建队列 - 使用 BlockingQueueTypeEnum
BlockingQueue<T> q = BlockingQueueTypeEnum.createBlockingQueue(queueType, capacity);
// 或者通过队列名称创建
BlockingQueue<T> q2 = BlockingQueueTypeEnum.createBlockingQueue("ArrayBlockingQueue", capacity);
// 验证队列配置
// 验证队列配置 - 使用 BlockingQueueManager
boolean valid = BlockingQueueManager.validateQueueConfig(queueType, capacity);
// 动态调整容量(仅 ResizableCapacityLinkedBlockingQueue 支持)
// 动态调整容量(仅 ResizableCapacityLinkedBlockingQueue 支持)- 使用 BlockingQueueManager
boolean ok = BlockingQueueManager.changeQueueCapacity(executor.getQueue(), newCapacity);
```

@ -51,13 +51,15 @@ com.example.queue.MyArrayBlockingQueue
### 3.1 队列创建与验证
```java
// 创建队列
BlockingQueue<T> q = BlockingQueueManager.createQueue(queueType, capacity);
// 创建队列 - 使用 BlockingQueueTypeEnum
BlockingQueue<T> q = BlockingQueueTypeEnum.createBlockingQueue(queueType, capacity);
// 或者通过队列名称创建
BlockingQueue<T> q2 = BlockingQueueTypeEnum.createBlockingQueue("ArrayBlockingQueue", capacity);
// 验证队列配置
// 验证队列配置 - 使用 BlockingQueueManager
boolean valid = BlockingQueueManager.validateQueueConfig(queueType, capacity);
// 动态调整容量(仅 ResizableCapacityLinkedBlockingQueue 支持)
// 动态调整容量(仅 ResizableCapacityLinkedBlockingQueue 支持)- 使用 BlockingQueueManager
boolean ok = BlockingQueueManager.changeQueueCapacity(executor.getQueue(), newCapacity);
```

@ -23,49 +23,17 @@ import lombok.extern.slf4j.Slf4j;
import java.util.Collection;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
/**
* Blocking queue manager for queue operations.
* Supports SPI extension, queue creation, capacity management, and type recognition.
* Blocking queue runtime manager.
* Provides queue management operations: capacity adjustment, type recognition, and configuration validation.
*
* <p>Note: For queue creation, use {@link BlockingQueueTypeEnum#createBlockingQueue(int, Integer)}
* or {@link BlockingQueueTypeEnum#createBlockingQueue(String, Integer)} directly.</p>
*/
@Slf4j
public class BlockingQueueManager {
static {
ServiceLoaderRegistry.register(CustomBlockingQueue.class);
}
/**
* Create blocking queue by type and capacity
*
* @param queueType queue type
* @param capacity queue capacity
* @param <T> queue element type
* @return blocking queue instance
*/
public static <T> BlockingQueue<T> createQueue(Integer queueType, Integer capacity) {
if (queueType == null) {
queueType = BlockingQueueTypeEnum.LINKED_BLOCKING_QUEUE.getType();
}
return BlockingQueueTypeEnum.createBlockingQueue(queueType, capacity);
}
/**
* Create blocking queue by name and capacity
*
* @param queueName queue name
* @param capacity queue capacity
* @param <T> queue element type
* @return blocking queue instance
*/
public static <T> BlockingQueue<T> createQueue(String queueName, Integer capacity) {
if (queueName == null || queueName.isEmpty()) {
queueName = BlockingQueueTypeEnum.LINKED_BLOCKING_QUEUE.getName();
}
return BlockingQueueTypeEnum.createBlockingQueue(queueName, capacity);
}
/**
* Check if queue capacity can be dynamically changed
*

@ -143,28 +143,28 @@ public class BlockingQueueSpiTest {
}
/**
* Test Case 3: BlockingQueueManager unified creation entry
* Test Case 3: Queue creation via BlockingQueueTypeEnum
*/
@Test
public void testBlockingQueueManagerCreation() {
System.out.println("\n========== Test Case 3: BlockingQueueManager unified creation entry ==========");
public void testBlockingQueueCreation() {
System.out.println("\n========== Test Case 3: Queue creation via BlockingQueueTypeEnum ==========");
// Create built-in queue via BlockingQueueManager
BlockingQueue<Runnable> queue1 = BlockingQueueManager.createQueue(1, 512);
// Create built-in queue via BlockingQueueTypeEnum
BlockingQueue<Runnable> queue1 = BlockingQueueTypeEnum.createBlockingQueue(1, 512);
Assert.assertNotNull("Should successfully create queue", queue1);
Assert.assertTrue("Should create ArrayBlockingQueue", queue1 instanceof ArrayBlockingQueue);
// Create by type name
BlockingQueue<Runnable> queue2 = BlockingQueueManager.createQueue("ArrayBlockingQueue", 1024);
BlockingQueue<Runnable> queue2 = BlockingQueueTypeEnum.createBlockingQueue("ArrayBlockingQueue", 1024);
Assert.assertNotNull("Should successfully create queue by name", queue2);
Assert.assertTrue("Should create ArrayBlockingQueue", queue2 instanceof ArrayBlockingQueue);
// Test default queue (null type)
BlockingQueue<Runnable> defaultQueue = BlockingQueueManager.createQueue("", 1024);
Assert.assertNotNull("Null type should create default queue", defaultQueue);
// Test default queue with null type - falls back to LinkedBlockingQueue
BlockingQueue<Runnable> defaultQueue = BlockingQueueTypeEnum.createBlockingQueue("", 1024);
Assert.assertNotNull("Empty name should create default queue", defaultQueue);
System.out.println("Default queue type: " + defaultQueue.getClass().getSimpleName());
System.out.println("Passed: BlockingQueueManager unified entry works");
System.out.println("Passed: BlockingQueueTypeEnum queue creation works");
}
/**

@ -160,9 +160,9 @@ public class ServerThreadPoolDynamicRefresh implements ThreadPoolDynamicRefresh
if (BlockingQueueManager.canChangeCapacity(executor.getQueue())) {
boolean success = BlockingQueueManager.changeQueueCapacity(executor.getQueue(), parameter.getCapacity());
if (success) {
log.info("Queue capacity changed to: {}", parameter.getCapacity());
log.info("Queue capacity changed to: {} for thread pool: {}", parameter.getCapacity(), parameter.getTpId());
} else {
log.warn("Failed to change queue capacity to: {}", parameter.getCapacity());
log.warn("Failed to change queue capacity to: {} for thread pool: {}", parameter.getCapacity(), parameter.getTpId());
}
} else {
log.warn("Queue capacity cannot be changed for current queue type: {}. " +

Loading…
Cancel
Save