Example #1
0
  /**
   * Sends the planner metrics to the metrics server
   *
   * @param metrics the metrics to log
   * @param url the url to send the metrics to
   */
  private void sendMetricsSynchronously(PlannerMetrics metrics, String url) throws IOException {

    SendMetrics sm = new SendMetrics(metrics, url);

    SendMetricsResult result = sm.call();

    if (result.getCode() == 202) {
      mLogger.log("Metrics succesfully sent to the server", LogManager.DEBUG_MESSAGE_LEVEL);
    } else {
      mLogger.log("Unable to send metrics to the server " + result, LogManager.DEBUG_MESSAGE_LEVEL);
    }
  }
Example #2
0
  /**
   * Sends the planner metrics to the metrics server
   *
   * @param metrics the metrics to log
   * @param url the url to send the metrics to
   */
  private SendMetricsResult send(PlannerMetrics metrics, String url) throws IOException {
    SendMetricsResult result = new SendMetricsResult();

    URL u = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) u.openConnection();
    connection.setDoOutput(true);
    // connection.setDoInput( true );
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Content-Type", "application/json");

    try {
      OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());

      try {
        // String payload = URLEncoder.encode( metrics.toJson(), "UTF-8") ;
        String payload = metrics.toJson();
        out.write(payload);
      } finally {
        out.close();
      }

      result.setCode(connection.getResponseCode());
      result.setResponseMessage(connection.getResponseMessage());

      /*
      BufferedReader in = new BufferedReader(
                              new InputStreamReader(
                               connection.getInputStream()));
      String result;
      while (( result = in.readLine()) != null) {
          System.out.println( result );
      }*/
    } finally {
      connection.disconnect();
    }

    return result;
  }
Example #3
0
  /**
   * Sends the planner metrics to the metrics server asynchrnously with a timeout of 5 seconds
   *
   * @param metrics the metrics to log
   * @param url the url to send the metrics to
   */
  private void sendMetricsAsynchronously(PlannerMetrics metrics, String url) {

    ExecutorService executor = Executors.newSingleThreadExecutor();
    //      Future<SendMetricsResult> future =   (Future<SendMetricsResult>) executor.submit(
    //                                                           new FutureTask<SendMetricsResult>(
    // new SendMetrics( metrics, url ) ));

    Future<SendMetricsResult> future =
        (Future<SendMetricsResult>) executor.submit(new SendMetrics(metrics, url));

    SendMetricsResult result = null;
    try {
      result = future.get(METRICS_SEND_TIMEOUT, TimeUnit.SECONDS);

    } catch (InterruptedException ex) {
      mLogger.log("Interrupted while sending metrics " + url, ex, LogManager.DEBUG_MESSAGE_LEVEL);
    } catch (ExecutionException ex) {
      mLogger.log(
          "Exception caught while sending metrics to server " + url,
          ex,
          LogManager.DEBUG_MESSAGE_LEVEL);
    } catch (TimeoutException e) {
      mLogger.log(
          "Sending of metrics to server timed out " + url, e, LogManager.DEBUG_MESSAGE_LEVEL);
    } finally {
      executor.shutdownNow();
    }

    if (result != null) {
      if (result.getCode() == 202) {
        mLogger.log("Metrics succesfully sent to the server", LogManager.DEBUG_MESSAGE_LEVEL);
      } else {
        mLogger.log(
            "Unable to send metrics to the server " + result, LogManager.DEBUG_MESSAGE_LEVEL);
      }
    }
  }