简体   繁体   English

如何处理Java中的超时异常?

[英]How to deal with timeout exception in Java?

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. AdwordsCampaign.getAllCampaign()尝试获取一些远程资源。 This may throw a RemoteException because the internet connection times out. 这可能会抛出RemoteException因为Internet连接超时。 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? 或者,还有更好的方法?

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

This looks OK except the repetition of code in catch block( be sure of number of retries you want ). 除了重复catch块中的代码之外,这看起来还不错( 确保你想要的重试次数 )。 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();
    }

You indeed cannot catch it as a SocketTimeoutException. 你确实无法将其作为SocketTimeoutException捕获。 What is possible is to catch the RemoteException, retrieve it's cause and check if that's an instanceof SocketTimeoutException. 可能的是捕获RemoteException,检索它的原因并检查它是否是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
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM