2016-07-06 7 views
4

プロキシを使用してサーバーから情報を取得する次の方法があります。時には劣悪なプロキシに私はあなたが私の方法で見ることができるように私はすでにサーバが何かを返した場合、私は、サーバーの応答の内容を取得するためにそれを行う必要がありcatch (IOException ioe)を使用していますSocketExceptionSSLExceptionSSLHandshakeExceptionまたはConnectExceptionいくつかの例外について再試行メソッド

を取得します上記の例外が発生した場合、どうすればメソッドを再試行できますか?

public String getMeta() throws IOException 
{ 

    HttpsURLConnection con = null; 
    InputStream is = null; 
    StringWriter writer = new StringWriter(); 
    try 
    { 
     String url = "https://api.myapp.com/meta"; 
     URL urlObj = new URL(url); 

     if (useProxy && fullProxy) 
     { 
      myapp.Proxy proxyCustom = getRandomProxy(); 
      Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyCustom.getProxyIp(), proxyCustom.getProxyPort())); 
      con = (HttpsURLConnection) urlObj.openConnection(proxy); 
     } 
     else 
     { 
      con = (HttpsURLConnection) urlObj.openConnection(); 
     } 

     con.setRequestMethod("GET"); 
     con.setRequestProperty("User-Agent", USER_AGENT); 
     con.setRequestProperty("Content-Type", "application/json; charset=utf-8"); 
     con.setRequestProperty("host", urlObj.getHost()); 
     con.setRequestProperty("Connection", "Keep-Alive"); 

     int responseCode = 0; 

     responseCode = con.getResponseCode(); 

     is = con.getInputStream(); 
     writer = new StringWriter(); 
     IOUtils.copy(is, writer, "UTF-8"); 
    } 
    catch (IOException ioe) 
    { 
     if (con instanceof HttpsURLConnection) 
     { 
      HttpsURLConnection httpConn = (HttpsURLConnection) con; 
      int statusCode = httpConn.getResponseCode(); 
      if (statusCode != 200) 
      { 
       is = httpConn.getErrorStream(); 
       writer = new StringWriter(); 
       IOUtils.copy(is, writer, "UTF-8"); 
      } 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

    return writer.toString(); 
} 
+0

ループまたは再帰を使用します。あなたがそのようなものや無限の再帰/ループを避けたいのであれば、余分なロジックを必要とせずにtryブロックから新しいメソッドにすべてを入れてください。その後、別の時間を呼び出すことができます。 – HopefullyHelpful

+1

'メソッドを再試行するにはどうすればいいですか'一度だけ再試行しますか、例外がスローされなくなるまで継続的に再試行しますか? – copeg

+0

@copeg再試行する必要のある回数を設定する簡単な方法があることを願っていますが、複雑すぎるため、一度試してみるだけです。 – Arya

答えて

0

私はどうなる方法は、新しい再試行メソッドを作成し、それが唯一の異常の場合には呼ばれているので 私はあなただけ再試行を呼び出す必要がありますと仮定していますあなたのgetMeta()メソッドの最後で、それを呼び出しています数回ひいてはgetMetaはなく、その後、回

例外はあなたのgetMeta()あなたが今やっているように、キャッチはそのようなものを行いますにスローされた場合
public String getMeta() throws IOException, RetryException 
{ 

    HttpsURLConnection con = null; 
    InputStream is = null; 
    StringWriter writer = new StringWriter(); 
    try 
    { 
     String url = "https://api.myapp.com/meta"; 
     URL urlObj = new URL(url); 

     if (useProxy && fullProxy) 
     { 
      myapp.Proxy proxyCustom = getRandomProxy(); 
      Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyCustom.getProxyIp(), proxyCustom.getProxyPort())); 
      con = (HttpsURLConnection) urlObj.openConnection(proxy); 
     } 
     else 
     { 
      con = (HttpsURLConnection) urlObj.openConnection(); 
     } 

     con.setRequestMethod("GET"); 
     con.setRequestProperty("User-Agent", USER_AGENT); 
     con.setRequestProperty("Content-Type", "application/json; charset=utf-8"); 
     con.setRequestProperty("host", urlObj.getHost()); 
     con.setRequestProperty("Connection", "Keep-Alive"); 

     int responseCode = 0; 

     responseCode = con.getResponseCode(); 

     is = con.getInputStream(); 
     writer = new StringWriter(); 
     IOUtils.copy(is, writer, "UTF-8"); 

     return writer.toString(); 
    } 
    catch (IOException ioe) 
    { 
     if (con instanceof HttpsURLConnection) 
     { 
      HttpsURLConnection httpConn = (HttpsURLConnection) con; 
      int statusCode = httpConn.getResponseCode(); 
      if (statusCode != 200) 
      { 
       is = httpConn.getErrorStream(); 
       writer = new StringWriter(); 
       IOUtils.copy(is, writer, "UTF-8"); 

       return writer.toString(); 
      } 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

    return retryGetMeta(); 
} 


private String retryGetMeta() 
{ 
    try 
    { 
     return getMeta(); 
    } 
    finally 
    { 
     //Do your stuff 
    } 
} 

public RetryException extends Exception 
{ 
    private String message = "Retries exceeded"; 

     public String getMessage() 
     { 
     return message; 
     } 

} 

一定数の後RetryExceptionのようないくつかの例外をスローする必要があり、 retryGetMetaが呼び出されます

+0

答えをありがとう、これはIOExceptionの権利の場合にリコールされないだろうか? SocketException、SSLException、SSLHandshakeExceptionまたはConnectExceptionがスローされたときに呼び出されます。 – Arya

+0

はい、IOExceptionのwriter.toString()を戻す場合は、再試行は呼び出されません。 IOExceptionの場合にライターを返すように修正されたコード、再試行を呼び出すその他の例外 – SomeDude

0

上記の例外が発生した場合、メソッドを再試行するにはどうすればよいですか?

以下に示す方法の1つは、getMetaメソッドが実際にIOExceptionをスローすることです。キャッチされたExceptionによってcallerメソッドが再帰的に自身を呼び出すようにすることができます。私はそれは、時代の方法n数を呼び出し、引数としての回数に合格し、対処することができるようにするに

を再試行する方法回設定する簡単な方法があると思います

それに応じて停止基準論理を更新する。 getMetaで

public String caller(int total) throws IOException{ 
    return callerImpl(1, total); 
} 

public String callerImpl(int current, int total) throws IOException{ 
    try{ 
     return getMeta(); 
    }catch(IOException e){ 
     current++; 
     if (current > total){ 
      throw e;//or return null or empty string, depending upon upstream requirements 
     } 
     return callerImpl(current, total); 
    } 

    return null; 
} 

:たとえば

try{ 
    .... 
}catch(IOException io){ 
    //log or handle io 
    throw io; 
} 

注上記のあなたには、いくつかの方法で処理したいことがスローされた例外のロギング/ロジックを扱っていません。

関連する問題