2013-07-17 20 views
0

什麼是嘗試多個時間相同的RPC調用失敗的RPC調用的最佳方式?什麼是最好的嘗試嘗試多次相同的RPC調用

只是一個例子:在這種情況下,如果RPC因網絡連接而失敗,它將捕獲到onFailure(Throwable caught)。 現在在這裏它應該再次調用同一個RPC來檢查網絡連接。最大嘗試次數應該是3次,然後向用戶顯示消息,如「網絡未建立」 我該如何實現?

有些夫婦喜歡在onFailure調用相同的RPC調用的想法,但這裏要求成爲different.but我想same request have a three request,這是不好的做法,我不知道它什麼好的解決辦法。

在此先感謝。

回答

1

在您的AsynCallBack實施中使用計數器。我還建議在再次請求服務器之前使用計時器。

該代碼應工作:

final GreetingServiceAsync greetingService = GWT.create(GreetingService.class); 
    final String textToServer = "foo"; 
    greetingService.greetServer(textToServer, new AsyncCallback<String>() { 
    int tries = 0; 
    public void onSuccess(String result) { 
     // Do something 
    } 
    public void onFailure(Throwable caught) { 
     if (tries ++ < 3) { 
     // Optional Enclose the new call in a timer to wait sometime before requesting the server again 
     new Timer() { 
      public void run() { 
      greetingService.greetServer(textToServer, this); 
      } 
     }.schedule(4000); 
     } 
    } 
    }); 
+0

謝謝,它工作+1,我有其他好的方式來實現它從谷歌集團。所以我也分享這個答案。 – iMBMT

0

@Jens給出這個答案從谷歌網上論壇。

通過使用自定義RpcRequestBuilder,您可以透明地處理給定GWT-RPC接口的所有請求。這個自定義的RpcRequestBuilder會進行3次請求嘗試,如果所有3次都失敗,則調用onFailure()方法。

MyRemoteServiceAsync service = GWT.create(MyRemoteService.class); ((ServiceDefTarget)service).setRpcRequestBuilder(new RetryThreeTimesRequestBuilder());

如果多個應用程序組件可能對該信息感興趣,那麼自定義的RequestBuilder也可以觸發eventBus上的「NetworkFailureEvent」。例如,您可以用黑屏覆蓋整個應用程序,並定期嘗試向您的服務器發送Ping請求,直到網絡恢復聯機。也可以在線HTML 5屬性,你可以檢查,但它不是100%可靠(https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.onLine