Previous Topic: Agent VSE APIsNext Topic: Java Agent Extensions


Agent API Examples

Agent API Example 1

The following code generates a transaction from client code and retrieves the transaction tree that it generated:

private static void testAddTransaction() throws Exception
 {
 String request = "http://localhost:8080/examples/servlets/servlet/HelloWorldExample";
 TransactionsClient tc = AgentClient.getInstance().getTransactionClient();
 
 tc.installSocketTracker(false);
 long start = System.currentTimeMillis();
 
 String response = testMakeRequest(request);
 
 long end = System.currentTimeMillis();
 tc.uninstallSocketTracker(false);
 
 String frameId = tc.addClientTransaction("test", new Object[] { request }, response, end - start);
 TransactionFrame frame = tc.getTransactionTree(frameId);
 
 System.out.println(frame.isComplete() ? "Yes!" : "No!");
 }

The preceding code uses the following utility function, which has nothing to do with the agent but is listed for completeness:

/** Assuming Tomcat is running on localhost:8080 */
 private static String testMakeRequest(String url) throws IOException
 {
 StringBuffer response = new StringBuffer();
 HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
 
 con.setRequestMethod("GET");
 con.setDoOutput(true);
 con.setUseCaches(false);
 
 BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
 for (String line = br.readLine(); line != null; line = br.readLine()) response.append(line);
 br.close();
 
 return response.toString();
 }

Agent API Example 2

The following code registers a logging VSE callback:

AgentClient.getInstance().addListener(new IAgentEventListener()
 {
  public void onAgentOffline(final IAgentInfo info) {}
  public void onAgentOnline(final IAgentInfo info)
  {
   AgentClient.getInstance().getVSEClient().registerVSECallback(info, new IVSECallback()
   {
    public void onFrameRecord(VSEFrame frame) { System.out.println("Recorded: " + frame); }
    public VSEFrame onFramePlayback(VSEFrame frame) { System.out.println("Played back: " + frame); return frame; }
    public int hashCode() { return 0; }
    public boolean equals(Object o) { return o instanceof IVSECallback; }
  });
 
  try { AgentClient.getInstance().getVSEClient().startVSERecording(info); } catch (JMSInvocationException e) {}
 });