samza-yarn/src/main/java/org/apache/samza/webapp/ApplicationMasterRestClient.java [40:109]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class ApplicationMasterRestClient implements Closeable {
  private final CloseableHttpClient httpClient;
  private final HttpHost appMasterHost;
  private final ObjectMapper jsonMapper = SamzaObjectMapper.getObjectMapper();

  public ApplicationMasterRestClient(CloseableHttpClient client, String amHostName, int amRpcPort) {
    httpClient = client;
    appMasterHost = new HttpHost(amHostName, amRpcPort);
  }

  /**
   * @return  the metrics as a map of groupName to metricName to metricValue.
   * @throws IOException if there was an error fetching the metrics from the servlet.
   */
  public Map<String, Map<String, Object>> getMetrics() throws IOException {
    String jsonString = getEntityAsJson("/metrics", "metrics");
    return jsonMapper.readValue(jsonString, new TypeReference<Map<String, Map<String, Object>>>() { });
  }

  /**
   * @return  the task context as a map of key to value
   * @throws IOException if there was an error fetching the task context from the servlet.
   */
  public Map<String, Object> getTaskContext() throws IOException {
    String jsonString = getEntityAsJson("/task-context", "task context");
    return jsonMapper.readValue(jsonString, new TypeReference<Map<String, Object>>() { });
  }

  /**
   * @return  the AM state as a map of key to value
   * @throws IOException if there was an error fetching the AM state from the servlet.
   */
  public Map<String, Object> getAmState() throws IOException {
    String jsonString = getEntityAsJson("/am", "AM state");
    return jsonMapper.readValue(jsonString, new TypeReference<Map<String, Object>>() { });
  }

  /**
   * @return  the config as a map of key to value
   * @throws IOException if there was an error fetching the config from the servlet.
   */
  public Map<String, Object> getConfig() throws IOException {
    String jsonString = getEntityAsJson("/config", "config");
    return jsonMapper.readValue(jsonString, new TypeReference<Map<String, Object>>() { });
  }

  @Override
  public void close() throws IOException {
    httpClient.close();
  }

  private String getEntityAsJson(String path, String entityName) throws IOException {
    HttpGet getRequest = new HttpGet(path);
    HttpResponse httpResponse = httpClient.execute(appMasterHost, getRequest);

    StatusLine status = httpResponse.getStatusLine();
    if (status.getStatusCode() != HttpStatus.SC_OK) {
      throw new SamzaException(String.format(
          "Error retrieving %s from host %s. Response: %s",
          entityName,
          appMasterHost.toURI(),
          status.getReasonPhrase()));
    }

    return EntityUtils.toString(httpResponse.getEntity());
  }

  @Override
  public String toString() {
    return "AppMasterClient for uri: " + appMasterHost.toURI().toString();
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



samza-yarn3/src/main/java/org/apache/samza/webapp/ApplicationMasterRestClient.java [40:109]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class ApplicationMasterRestClient implements Closeable {
  private final CloseableHttpClient httpClient;
  private final HttpHost appMasterHost;
  private final ObjectMapper jsonMapper = SamzaObjectMapper.getObjectMapper();

  public ApplicationMasterRestClient(CloseableHttpClient client, String amHostName, int amRpcPort) {
    httpClient = client;
    appMasterHost = new HttpHost(amHostName, amRpcPort);
  }

  /**
   * @return  the metrics as a map of groupName to metricName to metricValue.
   * @throws IOException if there was an error fetching the metrics from the servlet.
   */
  public Map<String, Map<String, Object>> getMetrics() throws IOException {
    String jsonString = getEntityAsJson("/metrics", "metrics");
    return jsonMapper.readValue(jsonString, new TypeReference<Map<String, Map<String, Object>>>() { });
  }

  /**
   * @return  the task context as a map of key to value
   * @throws IOException if there was an error fetching the task context from the servlet.
   */
  public Map<String, Object> getTaskContext() throws IOException {
    String jsonString = getEntityAsJson("/task-context", "task context");
    return jsonMapper.readValue(jsonString, new TypeReference<Map<String, Object>>() { });
  }

  /**
   * @return  the AM state as a map of key to value
   * @throws IOException if there was an error fetching the AM state from the servlet.
   */
  public Map<String, Object> getAmState() throws IOException {
    String jsonString = getEntityAsJson("/am", "AM state");
    return jsonMapper.readValue(jsonString, new TypeReference<Map<String, Object>>() { });
  }

  /**
   * @return  the config as a map of key to value
   * @throws IOException if there was an error fetching the config from the servlet.
   */
  public Map<String, Object> getConfig() throws IOException {
    String jsonString = getEntityAsJson("/config", "config");
    return jsonMapper.readValue(jsonString, new TypeReference<Map<String, Object>>() { });
  }

  @Override
  public void close() throws IOException {
    httpClient.close();
  }

  private String getEntityAsJson(String path, String entityName) throws IOException {
    HttpGet getRequest = new HttpGet(path);
    HttpResponse httpResponse = httpClient.execute(appMasterHost, getRequest);

    StatusLine status = httpResponse.getStatusLine();
    if (status.getStatusCode() != HttpStatus.SC_OK) {
      throw new SamzaException(String.format(
          "Error retrieving %s from host %s. Response: %s",
          entityName,
          appMasterHost.toURI(),
          status.getReasonPhrase()));
    }

    return EntityUtils.toString(httpResponse.getEntity());
  }

  @Override
  public String toString() {
    return "AppMasterClient for uri: " + appMasterHost.toURI().toString();
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



