Java Reference
In-Depth Information
Be sure to invoke the new method from the agent's constructor. In addition, you
will need to import the
jmxbook.ch9
package. It is like all the other connectivity
methods of the agent—it creates the MBean, registers it on the MBean server,
and invokes its
start()
method. Use the
HTML
adapter (via a web browser) if you
need to change the port value of the adapter.
9.5.2
Testing the TCP adapter
With everything else completed, it is time to write a simple test program for the
TCP
adapter (see listing 9.11). The test program is defined by the class
TCPTester
,
and it performs all four tasks available to the adapter.
Listing 9.11
TCPTester.java
package jmxbook.ch9;
import java.net.*;
import javax.management.*;
import java.io.*;
public class TCPTester
{
public TCPTester( String port ) throws Exception
{
Socket s = new Socket( "localhost", Integer.parseInt( port ) );
PrintWriter print = new PrintWriter( s.getOutputStream() );
//create a Hello World MBean
print.println( TCPAdapter.CREATE_MBEAN );
print.flush();
print.println( "jmxbook.ch2.HelloWorld" );
print.flush();
print.println( "JMXBookAgent:name=TCPCreatedHW" );
print.flush();
print.println( TCPAdapter.ARGS );
print.flush();
print.println( "This is my greeting" );
print.flush();
print.println( "java.lang.String" );
print.flush();
BufferedReader in = new BufferedReader(
new InputStreamReader(
s.getInputStream() ) );
System.out.println( in.readLine() );
Thread.sleep(10000);
//reset the greeting