From ad8157258ed17d032c951788b0efeb22f7ee9e22 Mon Sep 17 00:00:00 2001 From: Odboy Date: Thu, 30 Oct 2025 22:59:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(websocket):=20=E6=B7=BB=E5=8A=A0=20Underto?= =?UTF-8?q?w=20WebSocket=20=E9=85=8D=E7=BD=AE=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 UndertowWebSocketConfig 类解决启动警告 - 配置 WebSocket 缓冲池大小及线程本地缓存 - 重命名 WsConfig 为 WebSocketEndpointConfig - 移除旧的 WsFactory 配置类 - 优化 WebSocket 部署信息自定义逻辑 --- .../config/UndertowWebSocketConfig.java | 41 ++++++++++++ ...nfig.java => WebSocketEndpointConfig.java} | 9 ++- .../config/UndertowWebSocketConfig.java | 41 ++++++++++++ ...nfig.java => WebSocketEndpointConfig.java} | 65 ++++++++++--------- .../framework/websocket/config/WsFactory.java | 43 ------------ 5 files changed, 124 insertions(+), 75 deletions(-) create mode 100644 cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/UndertowWebSocketConfig.java rename cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/{WsConfig.java => WebSocketEndpointConfig.java} (85%) create mode 100644 cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/UndertowWebSocketConfig.java rename cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/{WsConfig.java => WebSocketEndpointConfig.java} (85%) delete mode 100644 cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WsFactory.java diff --git a/cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/UndertowWebSocketConfig.java b/cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/UndertowWebSocketConfig.java new file mode 100644 index 00000000..f49cadfd --- /dev/null +++ b/cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/UndertowWebSocketConfig.java @@ -0,0 +1,41 @@ +package cn.odboy.framework.websocket.config; + +import io.undertow.server.DefaultByteBufferPool; +import io.undertow.websockets.jsr.WebSocketDeploymentInfo; +import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; +import org.springframework.stereotype.Component; + +/** + * 解决启动io.undertow.websockets.jsr UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used的警告 + * + * @author odboy + * @date 2025-10-30 + */ +@Component +public class UndertowWebSocketConfig implements WebServerFactoryCustomizer { + private int calculateThreadLocalCacheSize(int processorCount) { + // 每个I/O线程缓存2-4个缓冲区 + return Math.max(2, processorCount / 2); + } + + private int calculateMaxPoolSize(int processorCount) { + // 基础值 + 根据CPU核心数调整 + int baseSize = 100; + return baseSize + (processorCount * 20); + } + + @Override + public void customize(UndertowServletWebServerFactory factory) { + int processorCount = Runtime.getRuntime().availableProcessors(); + int bufferSize = 1024; + int maxPoolSize = calculateMaxPoolSize(processorCount); + int threadLocalCacheSize = calculateThreadLocalCacheSize(processorCount); + + factory.addDeploymentInfoCustomizers(deploymentInfo -> { + WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo(); + webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, bufferSize, maxPoolSize, threadLocalCacheSize)); + deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo); + }); + } +} \ No newline at end of file diff --git a/cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WsConfig.java b/cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WebSocketEndpointConfig.java similarity index 85% rename from cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WsConfig.java rename to cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WebSocketEndpointConfig.java index 53a2e9e4..f0bb27f7 100644 --- a/cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WsConfig.java +++ b/cutejava-springboot3/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WebSocketEndpointConfig.java @@ -19,9 +19,14 @@ package cn.odboy.framework.websocket.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; - +/** + * 自动注册使用了 @ServerEndpoint 注解声明的 WebSocket 端点对象 + * + * @author odboy + * @date 2025-10-30 + */ @Configuration -public class WsConfig { +public class WebSocketEndpointConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { diff --git a/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/UndertowWebSocketConfig.java b/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/UndertowWebSocketConfig.java new file mode 100644 index 00000000..f49cadfd --- /dev/null +++ b/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/UndertowWebSocketConfig.java @@ -0,0 +1,41 @@ +package cn.odboy.framework.websocket.config; + +import io.undertow.server.DefaultByteBufferPool; +import io.undertow.websockets.jsr.WebSocketDeploymentInfo; +import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; +import org.springframework.stereotype.Component; + +/** + * 解决启动io.undertow.websockets.jsr UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used的警告 + * + * @author odboy + * @date 2025-10-30 + */ +@Component +public class UndertowWebSocketConfig implements WebServerFactoryCustomizer { + private int calculateThreadLocalCacheSize(int processorCount) { + // 每个I/O线程缓存2-4个缓冲区 + return Math.max(2, processorCount / 2); + } + + private int calculateMaxPoolSize(int processorCount) { + // 基础值 + 根据CPU核心数调整 + int baseSize = 100; + return baseSize + (processorCount * 20); + } + + @Override + public void customize(UndertowServletWebServerFactory factory) { + int processorCount = Runtime.getRuntime().availableProcessors(); + int bufferSize = 1024; + int maxPoolSize = calculateMaxPoolSize(processorCount); + int threadLocalCacheSize = calculateThreadLocalCacheSize(processorCount); + + factory.addDeploymentInfoCustomizers(deploymentInfo -> { + WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo(); + webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, bufferSize, maxPoolSize, threadLocalCacheSize)); + deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo); + }); + } +} \ No newline at end of file diff --git a/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WsConfig.java b/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WebSocketEndpointConfig.java similarity index 85% rename from cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WsConfig.java rename to cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WebSocketEndpointConfig.java index 0214a338..f0bb27f7 100644 --- a/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WsConfig.java +++ b/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WebSocketEndpointConfig.java @@ -1,30 +1,35 @@ -/* - * Copyright 2021-2025 Odboy - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package cn.odboy.framework.websocket.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.socket.server.standard.ServerEndpointExporter; - -@Configuration -public class WsConfig { - - @Bean - public ServerEndpointExporter serverEndpointExporter() { - return new ServerEndpointExporter(); - } -} +/* + * Copyright 2021-2025 Odboy + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.odboy.framework.websocket.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.server.standard.ServerEndpointExporter; +/** + * 自动注册使用了 @ServerEndpoint 注解声明的 WebSocket 端点对象 + * + * @author odboy + * @date 2025-10-30 + */ +@Configuration +public class WebSocketEndpointConfig { + + @Bean + public ServerEndpointExporter serverEndpointExporter() { + return new ServerEndpointExporter(); + } +} diff --git a/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WsFactory.java b/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WsFactory.java deleted file mode 100644 index beef4096..00000000 --- a/cutejava/cutejava-framework/src/main/java/cn/odboy/framework/websocket/config/WsFactory.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2021-2025 Odboy - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package cn.odboy.framework.websocket.config; - -import io.undertow.server.DefaultByteBufferPool; -import io.undertow.websockets.jsr.WebSocketDeploymentInfo; -import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; -import org.springframework.boot.web.server.WebServerFactoryCustomizer; -import org.springframework.stereotype.Component; - -/** - * 解决启动io.undertow.websockets.jsr UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used的警告 - * - * @date 2024-06-07 - */ -@Component -public class WsFactory implements WebServerFactoryCustomizer { - - @Override - public void customize(UndertowServletWebServerFactory factory) { - factory.addDeploymentInfoCustomizers(deploymentInfo -> { - WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo(); - webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, 1024)); - deploymentInfo.addServletContextAttribute( - "io.undertow.websockets.jsr.WebSocketDeploymentInfo", - webSocketDeploymentInfo); - }); - } -}