Eclipse Paho 自动重新连接配置

Eclipse Paho Automatic Reconnect configure

如 java 客户的 automaticReconnect 功能文档中所提供:

Once the client has been disconnected, the client will attempt to connect with an increasing time interval. Starting at 1 second, doubling on each failed attempt up to 2 minutes.

但这对客户行为提出了一些问题:

  1. 2 分钟后,客户端会一直尝试连接吗? 因为我找不到任何其他参数来控制此迭代。

  2. 有什么方法可以配置这个时间间隔吗?

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

/**
 * 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);
        }
    });
}

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