Mqtt Eclipse Paho自动重新连接配置

Mqtt Eclipse Paho自动重新连接配置,mqtt,paho,Mqtt,Paho,如java客户端的自动连接功能文档所述: 一旦客户端断开连接,客户端将尝试 以增加的时间间隔连接。从1秒开始, 每次失败尝试加倍,最多2分钟 但这也引发了一些关于客户行为的问题: 在2分钟限制后,客户端是否会继续尝试无限期连接? 因为我找不到任何其他参数来控制这个迭代 是否有任何方法可以配置此间隔 查看源代码(我删除了下面几行): /** *尝试将客户端重新连接到服务器。 *如果成功,它将确保没有进一步的问题 *已计划重新连接。但是,如果连接失败,延迟将加倍 *最多128秒,并将在延迟后重新安排

如java客户端的
自动连接
功能文档所述:

一旦客户端断开连接,客户端将尝试 以增加的时间间隔连接。从1秒开始, 每次失败尝试加倍,最多2分钟

但这也引发了一些关于客户行为的问题:

  • 在2分钟限制后,客户端是否会继续尝试无限期连接? 因为我找不到任何其他参数来控制这个迭代

  • 是否有任何方法可以配置此间隔

  • 查看源代码(我删除了下面几行):

    /**
    *尝试将客户端重新连接到服务器。
    *如果成功,它将确保没有进一步的问题
    *已计划重新连接。但是,如果连接失败,延迟将加倍
    *最多128秒,并将在延迟后重新安排重新连接。
    * 
    *任何抛出的异常都会被记录,但不会按照假定的方式进行操作
    *由于服务器处于脱机状态,因此正在抛出它们,请重新连接
    *尝试仍将继续。
    */
    私有void尝试重新连接(){
    最终字符串methodName=“attemptReconnect”;
    //@跟踪500=正在尝试重新连接客户端:{0}
    log.fine(CLASS_NAME,methodName,“500”,新对象[]{this.clientId});
    connect(this.connOpts、this.userContext、新的IMqttActionListener(){
    成功时公共无效(IMqttToken asyncActionToken){
    //@跟踪501=自动重新连接成功:{0}
    fine(CLASS_NAME,methodName,“501”,新对象[]{asyncActionToken.getClient().getClientId()});
    通信设置状态(假);
    停止重新连接循环();
    }
    public void onFailure(IMqttToken asyncActionToken,可丢弃异常){
    //@跟踪502=自动重新连接失败,重新调度:{0}
    fine(CLASS_NAME,methodName,“502”,新对象[]{asyncActionToken.getClient().getClientId()});
    if(重新连接延迟<128000){
    重新连接延迟=重新连接延迟*2;
    }
    重新安排连接周期(重新连接延迟);
    }
    });
    }
    

    因此,重新连接延迟将为:
    1、2、4、8、16、32、64、128、128、128、…

    ,因此它将尝试无限期重新连接,并且是否有任何配置来停止此操作。假设10次重试后停止连接。确定:修改上面的java文件并编译您的自定义版本的Paho。或者禁用自动重新连接并在代码中处理它。如果我已经回答了你的问题,别忘了把它标记为已接受的答案。
    /**
     * Attempts to reconnect the client to the server.
     * If successful it will make sure that there are no further
     * reconnects scheduled. However if the connect fails, the delay will double
     * up to 128 seconds and will re-schedule the reconnect for after the delay.
     * 
     * Any thrown exceptions are logged but not acted upon as it is assumed that 
     * they are being thrown due to the server being offline and so reconnect
     * attempts will continue.
     */
    private void attemptReconnect(){
        final String methodName = "attemptReconnect";   
        //@Trace 500=Attempting to reconnect client: {0}
        log.fine(CLASS_NAME, methodName, "500", new Object[]{this.clientId});
    
        connect(this.connOpts, this.userContext,new IMqttActionListener() {
    
            public void onSuccess(IMqttToken asyncActionToken) {
                //@Trace 501=Automatic Reconnect Successful: {0}
                log.fine(CLASS_NAME, methodName, "501", new Object[]{asyncActionToken.getClient().getClientId()});
                comms.setRestingState(false);
                stopReconnectCycle();
            }
    
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                //@Trace 502=Automatic Reconnect failed, rescheduling: {0}
                log.fine(CLASS_NAME, methodName, "502", new Object[]{asyncActionToken.getClient().getClientId()});
                if(reconnectDelay < 128000){
                    reconnectDelay = reconnectDelay * 2;
                }
                rescheduleReconnectCycle(reconnectDelay);
            }
        });
    }