如何在以下方法中使用重试?

时间:2018-06-28 14:19:23

标签: java gradle groovy

下面是该方法,如果没有返回状态码= 200,我尝试添加重试。

您能否建议我如何在以下方法中使用它?

def listcodeGroups(String host, int port, String user, String pass) {
  CredentialsProvider credsProvider = new BasicCredentialsProvider()
  credsProvider.setCredentials(new AuthScope("${host}", port),
                               new UsernamePasswordCredentials(user, pass))

  CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build()
  try {
    HttpGet httpget = new HttpGet("https://${host}/api/groups/")

    System.out.println("Executing request " + httpget.getRequestLine())
    CloseableHttpResponse response = httpclient.execute(httpget)
    int status = response.getStatusLine().getStatusCode()
    if ( status != 200)
      throw new RuntimeException("Something went wrong: HTTP status: " + status)
    try {
      System.out.println("----------------------------------------")
      System.out.println(response.getStatusLine())
      String json = EntityUtils.toString(response.getEntity())
      JSONObject root = new JSONObject(json)
      System.out.println(root.toString())
    } catch(IOException e) {
      System.out.println(e.getMessage())}
    finally {
      response.close()
    }
  }catch(HTTPException e) {
    System.err.println("Fatal protocol violation: " + e.getMessage())
    e.printStackTrace() }
  catch(IOException e) {
    System.err.println("Fatal transport error: " + e.getMessage())
    e.printStackTrace()}
  finally {
    httpclient.close()
  }
}

0 个答案:

没有答案