EPAgent RESTful API: Java Example

Document created by Chris Kline Employee on Jan 17, 2015Last modified by PoetryFan on Apr 8, 2015
Version 3Show Document
  • View in full screen mode

This article describes a fully functional java program which utilizes the EPAgent RESTful interface to submit metrics to CA APM Enterprise Manager.  It will post a single metric to the EPAgent.  (provided by PoetryFan David Martin).

 

Note the sample URL on line 4 below:  http://172.16.88.131:8080/apm/metricFeed

 

This code is made available under an Apache License; you are free to make changes to and use of the script in any way you see fit.  It relies on the Apache HttpClient libraries, in maven:

 

groupId: org.apache.httpcomponents
artifactId: httpclient
version: 4.3.6
#Sample URL: http://172.16.88.131:8080/apm/metricFeed

package com.wily.dbm.json.simple_post;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;


import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;


public class SimpleJSONPost {


     private String apiURL;
     private Random random;


     public SimpleJSONPost(String apiURL) {      
          this.apiURL = apiURL;
          this.random = new Random(System.currentTimeMillis());
     }


     public void makeHTTPPOSTRequest() {
          try {
               HttpClient c = HttpClientBuilder.create().build();      
               HttpPost p = new HttpPost(this.apiURL);      


               p.setEntity(new StringEntity(
                         "{ \"metrics\" : [{ \"type\" : \"IntAverage\", \"name\" : \"Frontends|Apps|Foo:Average Response Time (ms)\", \"value\"=\"" + random.nextInt(1000) + "\"}] }",
                         ContentType.create("application/json")));
               HttpResponse r = c.execute(p);


               BufferedReader rd = new BufferedReader(new InputStreamReader(r.getEntity().getContent()));
               String line = "";
               while ((line = rd.readLine()) != null) {
                    System.out.println(line);
               }
          }
          catch(IOException e) {
               System.out.println(e);
          }                      
     }  
}


 

The resulting metric created by this code would look like:

RESTfulPost.png

2 people found this helpful

Attachments

    Outcomes