|
@@ -20,8 +20,8 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import java.util.Base64;
|
|
import java.util.Base64;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
-import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
|
-import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
|
|
+import java.util.concurrent.Executors;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -29,22 +29,28 @@ import java.util.concurrent.TimeUnit;
|
|
|
*
|
|
*
|
|
|
* @author skyline
|
|
* @author skyline
|
|
|
*/
|
|
*/
|
|
|
-//@Component
|
|
|
|
|
|
|
+//@Component 备注:弃用MNS改用AMQP
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class MnsHandler {
|
|
public class MnsHandler {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 线程池
|
|
|
|
|
|
|
+ * 使用 Java 21 虚拟线程执行器(推荐)
|
|
|
|
|
+ * 虚拟线程非常轻量,适合I/O密集型的消息处理
|
|
|
|
|
+ */
|
|
|
|
|
+ private final static ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 传统线程池(备用)
|
|
|
*/
|
|
*/
|
|
|
// private final static ExecutorService executorService = new ThreadPoolExecutor(
|
|
// private final static ExecutorService executorService = new ThreadPoolExecutor(
|
|
|
// 2, 4, 60, TimeUnit.SECONDS,
|
|
// 2, 4, 60, TimeUnit.SECONDS,
|
|
|
// new LinkedBlockingQueue<>(10000));
|
|
// new LinkedBlockingQueue<>(10000));
|
|
|
|
|
|
|
|
// 增加饱和策略(建议用CallerRunsPolicy)
|
|
// 增加饱和策略(建议用CallerRunsPolicy)
|
|
|
- private final static ThreadPoolExecutor executorService = new ThreadPoolExecutor(
|
|
|
|
|
- 4, 8, 60, TimeUnit.SECONDS,
|
|
|
|
|
- new LinkedBlockingQueue<>(10000),
|
|
|
|
|
- new ThreadPoolExecutor.CallerRunsPolicy()); // 关键修改点
|
|
|
|
|
|
|
+// private final static ThreadPoolExecutor executorService = new ThreadPoolExecutor(
|
|
|
|
|
+// 4, 8, 60, TimeUnit.SECONDS,
|
|
|
|
|
+// new LinkedBlockingQueue<>(10000),
|
|
|
|
|
+// new ThreadPoolExecutor.CallerRunsPolicy()); // 关键修改点
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -77,8 +83,9 @@ public class MnsHandler {
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
} finally {
|
|
} finally {
|
|
|
- //打印线程池状态
|
|
|
|
|
- log.info("Pool Size:{} ,Active Thread Count:{},Task Queue Size:{},Completed Task Count: {}", executorService.getPoolSize(), executorService.getActiveCount(), executorService.getQueue().size(), executorService.getCompletedTaskCount());
|
|
|
|
|
|
|
+ // 虚拟线程执行器不支持传统线程池的监控方法
|
|
|
|
|
+ // 虚拟线程是轻量级的,无需关注池大小和队列
|
|
|
|
|
+ log.debug("虚拟线程执行器正在处理消息...");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|