2017-06-07 89 views
1

如Java客戶端的automaticReconnect功能的文檔提供:Eclipse的泛美衛生組織自動重新配置

一旦客戶端已經斷開,客戶端將嘗試 有增加的時間間隔連接。從1秒開始, 每失敗一次,最多2分鐘。

但它提出了在客戶端行爲的一些問題:

  1. 2分鐘後限制,威爾客戶端不斷嘗試連接下去? 由於我無法找到任何其他參數來控制此迭代。

  2. 有什麼辦法可以配置這個間隔嗎?

回答

1

看那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, ...

+0

所以它會嘗試無限期地重新連接,並有任何配置可以阻止這一點。說10次再試停止連接。 –

+0

當然:修改上面的java文件並編譯你的自定義版本的Paho。或者禁用自動重新連接並在您的代碼中處理它。如果我回答了您的問題,請不要忘記將其標記爲已接受的答案。 –