fix 插件端删除 spring-boot-starter-websocket 依赖

This commit is contained in:
bwcx_jzy 2022-12-26 14:24:47 +08:00
parent 6e270f0027
commit 931e0fe2a8
No known key found for this signature in database
GPG Key ID: 5E48E9372088B9E5
12 changed files with 339 additions and 153 deletions

View File

@ -17,6 +17,7 @@
(感谢 [@LeonChen21](https://gitee.com/leonchen21) [Gitee issues I67C3C](https://gitee.com/dromara/Jpom/issues/I67C3C)
7. 【server】优化 websocket 控制台操作日志记录
8. 【server】修复 超级管理的 websocket 操作日志记录工作空间不正确
9. 【agent】优化 插件端删除 spring-boot-starter-websocket 依赖
### ❌ 不兼容功能

View File

@ -60,12 +60,17 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -24,6 +24,7 @@ package io.jpom.socket;
import cn.hutool.core.io.FileUtil;
import io.jpom.util.BaseFileTailWatcher;
import io.jpom.util.SocketSessionUtil;
import lombok.extern.slf4j.Slf4j;
import javax.websocket.Session;
@ -151,6 +152,14 @@ public class AgentFileTailWatcher<T> extends BaseFileTailWatcher<T> {
}
@Override
protected void send(T session, String msg) {
try {
SocketSessionUtil.send((Session) session, msg);
} catch (Exception e) {
log.error("发送消息异常", e);
}
}
/**
* 关闭

View File

@ -22,9 +22,21 @@
*/
package io.jpom.socket;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.util.Assert;
import org.springframework.web.context.support.WebApplicationObjectSupport;
import javax.websocket.DeploymentException;
import javax.websocket.server.ServerContainer;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.server.ServerEndpointConfig;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* 插件端socket 配置
@ -33,10 +45,91 @@ import org.springframework.web.socket.server.standard.ServerEndpointExporter;
* @since 2019/4/19
*/
@Configuration
public class AgentWebSocketConfig {
public class AgentWebSocketConfig extends WebApplicationObjectSupport implements InitializingBean, SmartInitializingSingleton {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
@Override
public void afterPropertiesSet() throws Exception {
//Assert.state(this.serverContainer != null, "javax.websocket.server.ServerContainer not available");
}
// @Override
// protected void initServletContext(ServletContext servletContext) {
// if (this.serverContainer == null) {
// this.serverContainer = (ServerContainer) servletContext.getAttribute("javax.websocket.server.ServerContainer");
// }
// }
private ServerContainer getServerContainer() {
return (ServerContainer) Objects.requireNonNull(getServletContext()).getAttribute("javax.websocket.server.ServerContainer");
}
@Override
protected boolean isContextRequired() {
return true;
}
@Override
public void afterSingletonsInstantiated() {
registerEndpoints();
}
protected void registerEndpoints() {
Set<Class<?>> endpointClasses = new LinkedHashSet<>();
ApplicationContext context = getApplicationContext();
if (context != null) {
String[] endpointBeanNames = context.getBeanNamesForAnnotation(ServerEndpoint.class);
for (String beanName : endpointBeanNames) {
endpointClasses.add(context.getType(beanName));
}
}
for (Class<?> endpointClass : endpointClasses) {
registerEndpoint(endpointClass);
}
if (context != null) {
Map<String, ServerEndpointConfig> endpointConfigMap = context.getBeansOfType(ServerEndpointConfig.class);
for (ServerEndpointConfig endpointConfig : endpointConfigMap.values()) {
registerEndpoint(endpointConfig);
}
}
}
private void registerEndpoint(Class<?> endpointClass) {
ServerContainer serverContainer = getServerContainer();
Assert.state(serverContainer != null,
"No ServerContainer set. Most likely the server's own WebSocket ServletContainerInitializer " +
"has not run yet. Was the Spring ApplicationContext refreshed through a " +
"org.springframework.web.context.ContextLoaderListener, " +
"i.e. after the ServletContext has been fully initialized?");
try {
if (logger.isDebugEnabled()) {
logger.debug("Registering @ServerEndpoint class: " + endpointClass);
}
serverContainer.addEndpoint(endpointClass);
} catch (DeploymentException ex) {
throw new IllegalStateException("Failed to register @ServerEndpoint class: " + endpointClass, ex);
}
}
private void registerEndpoint(ServerEndpointConfig endpointConfig) {
ServerContainer serverContainer = this.getServerContainer();
Assert.state(serverContainer != null, "No ServerContainer set");
try {
if (logger.isDebugEnabled()) {
logger.debug("Registering ServerEndpointConfig: " + endpointConfig);
}
serverContainer.addEndpoint(endpointConfig);
} catch (DeploymentException ex) {
throw new IllegalStateException("Failed to register ServerEndpointConfig: " + endpointConfig, ex);
}
}
// @Bean
// public ServerEndpointExporter serverEndpointExporter() {
// return new ServerEndpointExporter();
// }
}

View File

@ -0,0 +1,89 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Code Technology Studio
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package io.jpom.util;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import javax.websocket.Session;
import java.io.IOException;
/**
* socket 会话对象
*
* @author jiangzeyin
* @since 2018/9/29
*/
@Slf4j
public class SocketSessionUtil {
/**
*
*/
private static final KeyLock<String> LOCK = new KeyLock<>();
/**
* 错误尝试次数
*/
private static final int ERROR_TRY_COUNT = 10;
/**
* 发送消息
*
* @param session 会话对象
* @param msg 消息
* @throws IOException 异常
*/
public static void send(final Session session, String msg) throws IOException {
if (StrUtil.isEmpty(msg)) {
return;
}
if (!session.isOpen()) {
throw new RuntimeException("session close ");
}
try {
LOCK.lock(session.getId());
IOException exception = null;
int tryCount = 0;
do {
tryCount++;
if (exception != null) {
// 上一次有异常休眠 500
ThreadUtil.sleep(500);
}
try {
session.getBasicRemote().sendText(msg);
exception = null;
break;
} catch (IOException e) {
log.error("发送消息失败:" + tryCount, e);
exception = e;
}
} while (tryCount <= ERROR_TRY_COUNT);
if (exception != null) {
throw exception;
}
} finally {
LOCK.unlock(session.getId());
}
}
}

View File

@ -82,10 +82,7 @@
<groupId>cn.hutool</groupId>
<artifactId>hutool-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!-- aop-->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -1,3 +1,25 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Code Technology Studio
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package io.jpom.common;
import org.springframework.context.ApplicationContext;

View File

@ -26,14 +26,10 @@ import cn.hutool.core.date.DateUnit;
import cn.hutool.core.io.file.Tailer;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import io.jpom.system.JpomRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.socket.WebSocketSession;
import javax.websocket.Session;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.HashSet;
@ -73,18 +69,19 @@ public abstract class BaseFileTailWatcher<T> {
this.charset = charset;
}
protected void send(T session, String msg) {
try {
if (session instanceof Session) {
SocketSessionUtil.send((Session) session, msg);
} else if (session instanceof WebSocketSession) {
SocketSessionUtil.send((WebSocketSession) session, msg);
} else {
throw new JpomRuntimeException("没有对应类型");
}
} catch (IOException ignored) {
}
}
protected abstract void send(T session, String msg);
//{
// try {
// if (session instanceof Session) {
// SocketSessionUtil.send((Session) session, msg);
// } else if (session instanceof WebSocketSession) {
// SocketSessionUtil.send((WebSocketSession) session, msg);
// } else {
// throw new JpomRuntimeException("没有对应类型");
// }
// } catch (IOException ignored) {
// }
//}
/**
* 有新的日志

View File

@ -1,125 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Code Technology Studio
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package io.jpom.util;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import javax.websocket.Session;
import java.io.IOException;
/**
* socket 会话对象
*
* @author jiangzeyin
* @since 2018/9/29
*/
@Slf4j
public class SocketSessionUtil {
/**
*
*/
private static final KeyLock<String> LOCK = new KeyLock<>();
/**
* 错误尝试次数
*/
private static final int ERROR_TRY_COUNT = 10;
/**
* 发送消息
*
* @param session 会话对象
* @param msg 消息
* @throws IOException 异常
*/
public static void send(final Session session, String msg) throws IOException {
if (StrUtil.isEmpty(msg)) {
return;
}
if (!session.isOpen()) {
throw new RuntimeException("session close ");
}
try {
LOCK.lock(session.getId());
IOException exception = null;
int tryCount = 0;
do {
tryCount++;
if (exception != null) {
// 上一次有异常休眠 500
ThreadUtil.sleep(500);
}
try {
session.getBasicRemote().sendText(msg);
exception = null;
break;
} catch (IOException e) {
log.error("发送消息失败:" + tryCount, e);
exception = e;
}
} while (tryCount <= ERROR_TRY_COUNT);
if (exception != null) {
throw exception;
}
} finally {
LOCK.unlock(session.getId());
}
}
public static void send(WebSocketSession session, String msg) throws IOException {
if (StrUtil.isEmpty(msg)) {
return;
}
if (!session.isOpen()) {
throw new RuntimeException("session close ");
}
try {
LOCK.lock(session.getId());
IOException exception = null;
int tryCount = 0;
do {
tryCount++;
if (exception != null) {
// 上一次有异常休眠 500
ThreadUtil.sleep(500);
}
try {
session.sendMessage(new TextMessage(msg));
exception = null;
break;
} catch (IOException e) {
log.error("发送消息失败:" + tryCount, e);
exception = e;
}
} while (tryCount <= ERROR_TRY_COUNT);
if (exception != null) {
throw exception;
}
} finally {
LOCK.unlock(session.getId());
}
}
}

View File

@ -126,6 +126,11 @@
<artifactId>agent-transport-http</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>
<build>
<resources>

View File

@ -24,6 +24,7 @@ package io.jpom.socket;
import cn.hutool.core.io.FileUtil;
import io.jpom.util.BaseFileTailWatcher;
import io.jpom.util.SocketSessionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.WebSocketSession;
@ -140,6 +141,15 @@ public class ServiceFileTailWatcher<T> extends BaseFileTailWatcher<T> {
}
}
@Override
protected void send(T session, String msg) {
try {
SocketSessionUtil.send((WebSocketSession) session, msg);
} catch (Exception e) {
log.error("发送消息异常", e);
}
}
/**
* 关闭
*/

View File

@ -0,0 +1,83 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Code Technology Studio
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package io.jpom.util;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import java.io.IOException;
/**
* socket 会话对象
*
* @author jiangzeyin
* @since 2018/9/29
*/
@Slf4j
public class SocketSessionUtil {
/**
*
*/
private static final KeyLock<String> LOCK = new KeyLock<>();
/**
* 错误尝试次数
*/
private static final int ERROR_TRY_COUNT = 10;
public static void send(WebSocketSession session, String msg) throws IOException {
if (StrUtil.isEmpty(msg)) {
return;
}
if (!session.isOpen()) {
throw new RuntimeException("session close ");
}
try {
LOCK.lock(session.getId());
IOException exception = null;
int tryCount = 0;
do {
tryCount++;
if (exception != null) {
// 上一次有异常休眠 500
ThreadUtil.sleep(500);
}
try {
session.sendMessage(new TextMessage(msg));
exception = null;
break;
} catch (IOException e) {
log.error("发送消息失败:" + tryCount, e);
exception = e;
}
} while (tryCount <= ERROR_TRY_COUNT);
if (exception != null) {
throw exception;
}
} finally {
LOCK.unlock(session.getId());
}
}
}