update:删除无效引用

This commit is contained in:
hrz
2025-06-05 09:03:17 +08:00
parent ecc9d154d8
commit 492e0b952e
3 changed files with 66 additions and 61 deletions
@@ -2,36 +2,29 @@ package xiaozhi.modules.sys.enums;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.annotation.JsonValue;
import xiaozhi.common.exception.RenException;
/** /**
* 服务端动作枚举 * 服务端动作枚举
*/ */
public enum ServerActionEnum public enum ServerActionEnum {
{
RESTART("restart"), RESTART("restart"),
UPDATE_CONFIG("update_config"); UPDATE_CONFIG("update_config");
private final String value; private final String value;
ServerActionEnum(String value) ServerActionEnum(String value) {
{
this.value = value; this.value = value;
} }
@JsonValue @JsonValue
public String getValue() public String getValue() {
{
return value; return value;
} }
@JsonCreator @JsonCreator
public static ServerActionEnum fromValue(String value) public static ServerActionEnum fromValue(String value) {
{ for (ServerActionEnum action : ServerActionEnum.values()) {
for (ServerActionEnum action : ServerActionEnum.values()) if (action.value.equalsIgnoreCase(value)) {
{
if (action.value.equalsIgnoreCase(value))
{
return action; return action;
} }
} }
@@ -1,16 +1,16 @@
package xiaozhi.modules.sys.enums; package xiaozhi.modules.sys.enums;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
/** /**
* 服务端调用响应枚举 * 服务端调用响应枚举
*/ */
public enum ServerActionResponseEnum public enum ServerActionResponseEnum {
{
SUCCESS("success"), FAIL("fail"); SUCCESS("success"), FAIL("fail");
private final String value; private final String value;
ServerActionResponseEnum(String value) { ServerActionResponseEnum(String value) {
@@ -18,8 +18,7 @@ public enum ServerActionResponseEnum
} }
@JsonValue @JsonValue
public String getValue() public String getValue() {
{
return value; return value;
} }
@@ -1,13 +1,5 @@
package xiaozhi.modules.sys.utils; package xiaozhi.modules.sys.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;
import org.springframework.web.socket.*;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
import xiaozhi.common.utils.DateUtils;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
@@ -15,30 +7,51 @@ import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.*; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import org.springframework.util.StopWatch;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import xiaozhi.common.utils.DateUtils;
/** /**
* WebSocketClientResource:支持 try-with-resources 模式 * WebSocketClientResource:支持 try-with-resources 模式
*/ */
@Slf4j @Slf4j
public class WebSocketClientManager implements Closeable public class WebSocketClientManager implements Closeable {
{
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
// 全局回调线程池 // 全局回调线程池
private static final ExecutorService CALLBACK_EXECUTOR = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() { private static final ExecutorService CALLBACK_EXECUTOR = Executors
private final AtomicInteger cnt = new AtomicInteger(); .newFixedThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
private final AtomicInteger cnt = new AtomicInteger();
public Thread newThread(Runnable r) public Thread newThread(Runnable r) {
{ Thread t = new Thread(r, "ws-callback-" + cnt.getAndIncrement());
Thread t = new Thread(r, "ws-callback-" + cnt.getAndIncrement()); t.setDaemon(true);
t.setDaemon(true); return t;
return t; }
} });
});
private volatile WebSocketSession session; private volatile WebSocketSession session;
private final BlockingQueue<String> textMessageQueue; private final BlockingQueue<String> textMessageQueue;
@@ -71,13 +84,14 @@ public class WebSocketClientManager implements Closeable
this.errorFuture = new CompletableFuture<>(); this.errorFuture = new CompletableFuture<>();
} }
public static WebSocketClientManager build(Builder b) throws InterruptedException, ExecutionException, TimeoutException, IOException { public static WebSocketClientManager build(Builder b)
throws InterruptedException, ExecutionException, TimeoutException, IOException {
WebSocketClientManager ws = new WebSocketClientManager(b); WebSocketClientManager ws = new WebSocketClientManager(b);
StandardWebSocketClient client = new StandardWebSocketClient(); StandardWebSocketClient client = new StandardWebSocketClient();
CompletableFuture<WebSocketSession> future = client.execute(ws.new InternalHandler(b.uri), b.headers, URI.create(b.uri)); CompletableFuture<WebSocketSession> future = client.execute(ws.new InternalHandler(b.uri), b.headers,
URI.create(b.uri));
WebSocketSession sess = future.get(b.connectTimeout, b.connectUnit); WebSocketSession sess = future.get(b.connectTimeout, b.connectUnit);
if (sess == null || !sess.isOpen()) if (sess == null || !sess.isOpen()) {
{
throw new IOException("握手失败或会话未打开"); throw new IOException("握手失败或会话未打开");
} }
ws.session = sess; ws.session = sess;
@@ -100,13 +114,10 @@ public class WebSocketClientManager implements Closeable
session.sendMessage(new TextMessage(json)); session.sendMessage(new TextMessage(json));
} }
private <T> List<T> listenerCustom( private <T> List<T> listenerCustom(
BlockingQueue<T> queue, BlockingQueue<T> queue,
Predicate<T> predicate) Predicate<T> predicate)
throws InterruptedException, TimeoutException, ExecutionException throws InterruptedException, TimeoutException, ExecutionException {
{
List<T> collected = new ArrayList<>(); List<T> collected = new ArrayList<>();
long deadline = System.currentTimeMillis() + maxSessionDurationUnit.toMillis(maxSessionDuration); long deadline = System.currentTimeMillis() + maxSessionDurationUnit.toMillis(maxSessionDuration);
@@ -136,17 +147,16 @@ public class WebSocketClientManager implements Closeable
/** /**
* 同步接收多条消息,直到 predicate 为 true 或超时抛异常; * 同步接收多条消息,直到 predicate 为 true 或超时抛异常;
*
* @return 返回监听期间的所有消息列表 * @return 返回监听期间的所有消息列表
*/ */
public List<String> listener(Predicate<String> predicate) public List<String> listener(Predicate<String> predicate)
throws InterruptedException, TimeoutException, ExecutionException throws InterruptedException, TimeoutException, ExecutionException {
{
return listenerCustom(textMessageQueue, predicate); return listenerCustom(textMessageQueue, predicate);
} }
public List<byte[]> listenerBinary(Predicate<byte[]> predicate) public List<byte[]> listenerBinary(Predicate<byte[]> predicate)
throws InterruptedException, TimeoutException, ExecutionException throws InterruptedException, TimeoutException, ExecutionException {
{
return listenerCustom(binaryMessageQueue, predicate); return listenerCustom(binaryMessageQueue, predicate);
} }
@@ -183,8 +193,8 @@ public class WebSocketClientManager implements Closeable
if (session != null && session.isOpen()) { if (session != null && session.isOpen()) {
session.close(CloseStatus.NORMAL); session.close(CloseStatus.NORMAL);
} }
} catch (IOException ignored) {
} }
catch (IOException ignored) {}
textMessageQueue.clear(); textMessageQueue.clear();
binaryMessageQueue.clear(); binaryMessageQueue.clear();
errorFuture.completeExceptionally(new IOException("WebSocket 已关闭")); errorFuture.completeExceptionally(new IOException("WebSocket 已关闭"));
@@ -207,7 +217,8 @@ public class WebSocketClientManager implements Closeable
// 保存会话 // 保存会话
WebSocketClientManager.this.session = session; WebSocketClientManager.this.session = session;
this.stopWatch.start(); this.stopWatch.start();
log.info("ws连接成功, 目标URI: {}, 连接时间: {}", targetUri, DateUtils.getDateTimeNow(DateUtils.DATE_TIME_MILLIS_PATTERN)); log.info("ws连接成功, 目标URI: {}, 连接时间: {}", targetUri,
DateUtils.getDateTimeNow(DateUtils.DATE_TIME_MILLIS_PATTERN));
} }
/** /**
@@ -264,18 +275,19 @@ public class WebSocketClientManager implements Closeable
stopWatch.stop(); stopWatch.stop();
} }
log.info("ws连接关闭, 目标URI: {}, 关闭时间: {}, 连接总时长: {}s", log.info("ws连接关闭, 目标URI: {}, 关闭时间: {}, 连接总时长: {}s",
targetUri, DateUtils.getDateTimeNow(DateUtils.DATE_TIME_MILLIS_PATTERN), DateUtils.millsToSecond(stopWatch.getTotalTimeMillis())); targetUri, DateUtils.getDateTimeNow(DateUtils.DATE_TIME_MILLIS_PATTERN),
DateUtils.millsToSecond(stopWatch.getTotalTimeMillis()));
} }
} }
public static class Builder { public static class Builder {
private String uri; // 目标 WS URI private String uri; // 目标 WS URI
private long connectTimeout = 3; // 请求连接等待时间 private long connectTimeout = 3; // 请求连接等待时间
private TimeUnit connectUnit = TimeUnit.SECONDS; // 请求连接等待时间单位 private TimeUnit connectUnit = TimeUnit.SECONDS; // 请求连接等待时间单位
private long maxSessionDuration = 5; // 最大连线时间,默认5秒 private long maxSessionDuration = 5; // 最大连线时间,默认5秒
private TimeUnit maxSessionDurationUnit = TimeUnit.SECONDS; // 最大连线时间单位 private TimeUnit maxSessionDurationUnit = TimeUnit.SECONDS; // 最大连线时间单位
private int queueCapacity = 100; // 消息队列容量 private int queueCapacity = 100; // 消息队列容量
private WebSocketHttpHeaders headers; // 请求头 private WebSocketHttpHeaders headers; // 请求头
/** /**
* 目标 WS URI * 目标 WS URI
@@ -307,7 +319,8 @@ public class WebSocketClientManager implements Closeable
return this; return this;
} }
public WebSocketClientManager build() throws InterruptedException, ExecutionException, TimeoutException, IOException { public WebSocketClientManager build()
throws InterruptedException, ExecutionException, TimeoutException, IOException {
return WebSocketClientManager.build(this); return WebSocketClientManager.build(this);
} }