| | |
| | | |
| | | |
| | | |
| | | public void start() throws Exception { |
| | | public void start() { |
| | | try { |
| | | connect(); // 尝试连接 |
| | | if (session == null) { |
| | |
| | | log.error("线程被中断: " + e.getMessage(), e); // 记录线程中断的错误 |
| | | } catch (Exception e) { |
| | | log.error("bitget ws 连接过程中发生异常: " + e.getMessage(), e); // 记录连接过程中发生的异常 |
| | | throw e; |
| | | } finally { |
| | | executorService.shutdownNow(); // 尝试立即关闭调度服务 |
| | | } |
| | |
| | | } |
| | | |
| | | @OnClose |
| | | public void onClose() throws Exception { |
| | | public void onClose() { |
| | | log.info("bitget ws 连接已关闭,尝试重新连接..."); // 记录连接关闭的信息 |
| | | throw new Exception(); |
| | | handleConnectionClosedOrError(); |
| | | } |
| | | |
| | | @OnError |
| | | public void onError(Throwable throwable) throws Exception { |
| | | public void onError(Throwable throwable) { |
| | | log.error("bitget ws 发生错误: " + throwable.getMessage(), throwable); // 记录错误信息 |
| | | throw new Exception(); |
| | | handleConnectionClosedOrError(); |
| | | } |
| | | |
| | | // private void handleConnectionClosedOrError() { |
| | | // executorService.execute(() -> { |
| | | // try { |
| | | // attemptReconnect(); |
| | | // } catch (Exception e) { |
| | | // throw new RuntimeException(e); |
| | | // } |
| | | // }); |
| | | // } |
| | | private void handleConnectionClosedOrError() { |
| | | executorService.execute(() -> { |
| | | try { |
| | | attemptReconnect(); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | // private void attemptReconnect() throws Exception { |
| | | // if (reconnectAttempts < MAX_RECONNECT_ATTEMPTS) { // 检查重连次数是否超过限制 |
| | | // try { |
| | | // log.info("bitget ws 开始重连"); // 记录开始重连的信息 |
| | | // connect(); // 尝试重连 |
| | | // log.info("bitget ws 重连成功"); // 成功重连的日志 |
| | | // reconnectAttempts = 0; // 重连成功,重置重连次数 |
| | | // } catch (Exception e) { |
| | | // reconnectAttempts++; // 增加重连尝试次数 |
| | | // log.warn("bitget ws 重连失败,尝试次数: " + reconnectAttempts, e); // 记录重连失败的警告信息 |
| | | // // 采用指数退避策略,增加重连间隔 |
| | | // long waitTime = Math.min(60, (long) Math.pow(2, reconnectAttempts)) * 1000; // 最大等待时间为 60 秒 |
| | | // scheduleReconnect(waitTime); // 调度下次重连 |
| | | // } |
| | | // } else { |
| | | // log.error("超过最大重连次数,停止重连"); // 超过最大重连次数 |
| | | // throw new Exception(); |
| | | // } |
| | | // } |
| | | // |
| | | // private void scheduleReconnect(long waitTime) { // 接受等待时间参数 |
| | | // if (!executorService.isShutdown()) { |
| | | // executorService.schedule(this::attemptReconnect, waitTime, TimeUnit.MILLISECONDS); // 调度重连 |
| | | // } |
| | | // } |
| | | private void attemptReconnect() { |
| | | if (reconnectAttempts < MAX_RECONNECT_ATTEMPTS) { // 检查重连次数是否超过限制 |
| | | try { |
| | | log.info("bitget ws 开始重连"); // 记录开始重连的信息 |
| | | connect(); // 尝试重连 |
| | | log.info("bitget ws 重连成功"); // 成功重连的日志 |
| | | reconnectAttempts = 0; // 重连成功,重置重连次数 |
| | | } catch (Exception e) { |
| | | reconnectAttempts++; // 增加重连尝试次数 |
| | | log.warn("bitget ws 重连失败,尝试次数: " + reconnectAttempts, e); // 记录重连失败的警告信息 |
| | | // 采用指数退避策略,增加重连间隔 |
| | | long waitTime = Math.min(60, (long) Math.pow(2, reconnectAttempts)) * 1000; // 最大等待时间为 60 秒 |
| | | scheduleReconnect(waitTime); // 调度下次重连 |
| | | } |
| | | } else { |
| | | log.error("超过最大重连次数,停止重连"); // 超过最大重连次数 |
| | | } |
| | | } |
| | | |
| | | private void scheduleReconnect(long waitTime) { // 接受等待时间参数 |
| | | if (!executorService.isShutdown()) { |
| | | executorService.schedule(this::attemptReconnect, waitTime, TimeUnit.MILLISECONDS); // 调度重连 |
| | | } |
| | | } |
| | | |
| | | private void sendPing() { |
| | | try { |