| | |
| | | this.executorService = Executors.newScheduledThreadPool(1); |
| | | } |
| | | |
| | | public void start() throws Exception { |
| | | public void start() { |
| | | try { |
| | | connect(); |
| | | if (session == null) { |
| | |
| | | |
| | | } catch (Exception e) { |
| | | log.error("mexc ws 连接过程中发生异常:" + e.getMessage(), e); // 记录连接过程中发生的异常 |
| | | throw e; |
| | | } finally { |
| | | executorService.shutdown(); |
| | | } |
| | |
| | | @OnClose |
| | | public void onClose() throws Exception { |
| | | log.info("mexc ws 连接已关闭,尝试重新连接..."); |
| | | throw new Exception(); |
| | | handleConnectionClosedOrError(); |
| | | } |
| | | |
| | | @OnError |
| | | public void onError(Throwable throwable) throws Exception { |
| | | log.error("mexc ws 发生错误: " + throwable.getMessage(), throwable); |
| | | throw new Exception(); |
| | | handleConnectionClosedOrError(); |
| | | } |
| | | |
| | | // private void handleConnectionClosedOrError() { |
| | | // synchronized (lock) { |
| | | // if (!reconnecting) { |
| | | // reconnecting = true; // 设置 reconnecting 为 true 表示开始重连 |
| | | // executorService.execute(this::attemptReconnect); // 使用 execute 方法立即执行重连 |
| | | // } |
| | | // } |
| | | // } |
| | | // |
| | | // private void attemptReconnect() { |
| | | // boolean doReconnect = true; |
| | | // try { |
| | | // log.info("mexc ws 开始重连"); |
| | | // connect(); // 假设 connect() 方法用于实际的连接逻辑 |
| | | // log.info("mexc ws 重连成功"); |
| | | // } catch (Exception e) { |
| | | // log.error("mexc ws 重连失败", e); |
| | | // // 连接失败时,可以根据具体情况决定是否继续重连 |
| | | // // 在这里假设总是继续尝试重连 |
| | | // } finally { |
| | | // synchronized (lock) { |
| | | // if (doReconnect) { |
| | | // scheduleReconnect(); // 如果需要继续重连,则重新调度重连任务 |
| | | // } else { |
| | | // reconnecting = false; // 重连结束后设置 reconnecting 为 false |
| | | // } |
| | | // } |
| | | // } |
| | | // } |
| | | // |
| | | // private void scheduleReconnect() { |
| | | // if (!executorService.isShutdown()) { |
| | | // executorService.schedule(this::attemptReconnect, 5, TimeUnit.SECONDS); |
| | | // } |
| | | // } |
| | | private void handleConnectionClosedOrError() { |
| | | synchronized (lock) { |
| | | if (!reconnecting) { |
| | | reconnecting = true; // 设置 reconnecting 为 true 表示开始重连 |
| | | executorService.execute(this::attemptReconnect); // 使用 execute 方法立即执行重连 |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void attemptReconnect() { |
| | | boolean doReconnect = true; |
| | | try { |
| | | log.info("mexc ws 开始重连"); |
| | | connect(); // 假设 connect() 方法用于实际的连接逻辑 |
| | | log.info("mexc ws 重连成功"); |
| | | } catch (Exception e) { |
| | | log.error("mexc ws 重连失败", e); |
| | | // 连接失败时,可以根据具体情况决定是否继续重连 |
| | | // 在这里假设总是继续尝试重连 |
| | | } finally { |
| | | synchronized (lock) { |
| | | if (doReconnect) { |
| | | scheduleReconnect(); // 如果需要继续重连,则重新调度重连任务 |
| | | } else { |
| | | reconnecting = false; // 重连结束后设置 reconnecting 为 false |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void scheduleReconnect() { |
| | | if (!executorService.isShutdown()) { |
| | | executorService.schedule(this::attemptReconnect, 5, TimeUnit.SECONDS); |
| | | } |
| | | } |
| | | |
| | | private void sendPing() { |
| | | try { |