How to deal with timeout exception in Java?

2020-06-29 02:39发布

Here is my code:

 private void synCampaign() {
    List<Campaign> campaigns;
    try {
        campaigns = AdwordsCampaign.getAllCampaign();
        for(Campaign c : campaigns) 
            CampaignDao.save(c);
    } catch (ApiException e) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        synCampaign();
        e.printStackTrace();
    } catch (RemoteException e) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        synCampaign();
        e.printStackTrace();
    }

}

AdwordsCampaign.getAllCampaign() tries to get some remote resource. This may throw a RemoteException because the internet connection times out. When the exception is caught, I just want the thread to sleep for a while, then try to get the remote resource again.

Is there a problem with my code? Or is there a better way?

标签: java timeout
3条回答
太酷不给撩
2楼-- · 2020-06-29 02:48

Nothing really wrong, but the (potentially infinite) retry loop with recursion (and the stack growing) makes me a little nervous. I'd write instead:

private void synCampaignWithRetries(int ntries, int msecsRetry) {
    while(ntries-- >=0 ) {
       try {
         synCampaign();
         return; // no exception? success
       } 
      catch (ApiException e ) {
           // log exception?
      }
      catch (RemoteException e ) {
           // log exception?
      }
      try {
           Thread.sleep(msecsRetry);
      } catch (InterruptedException e1) {
           // log exception?
      }
   }
   // no success , even with ntries - log?
}

private void synCampaign() throws ApiException ,RemoteException {
    List<Campaign> campaigns = AdwordsCampaign.getAllCampaign();
    for(Campaign c : campaigns) 
            CampaignDao.save(c);
}
查看更多
霸刀☆藐视天下
3楼-- · 2020-06-29 03:06

This looks OK except the repetition of code in catch block(be sure of number of retries you want). You may want to create a private method to handle your exception as below:

    private void synCampaign() {
        List<Campaign> campaigns;
        try {
            campaigns = AdwordsCampaign.getAllCampaign();
            for(Campaign c : campaigns) 
                CampaignDao.save(c);
        } catch (ApiException e) {
            e.printStackTrace();
            waitAndSync();
        } catch (RemoteException e) {
            e.printStackTrace();
            waitAndSync();
        }

    }

    private void waitAndSync(){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        synCampaign();
    }
查看更多
乱世女痞
4楼-- · 2020-06-29 03:14

You indeed cannot catch it as a SocketTimeoutException. What is possible is to catch the RemoteException, retrieve it's cause and check if that's an instanceof SocketTimeoutException.

    try{
             // Your code that throws SocketTimeoutException

        }catch (RemoteException e) {
          if(e.getCause().getClass().equals(SocketTimeoutException.class)){
             System.out.println("It is SocketTimeoutException");
             // Do handling for socket exception
            }else{
              throw e;
            }
        }catch (Exception e) {
           // Handling other exception. If necessary
        }
查看更多
登录 后发表回答