Java Reference
In-Depth Information
With the completion of the
main()
method, you have finished the agent code. As
mentioned earlier, you still need to provide a way to create an
RMI
client when
you want to interact with the agent using the
RMI
connector. The next section
presents a utility class called
RMIClientFactory
that provides the solution.
3.3 Writing the RMIClientFactory class
The
RMIClientFactory
class is used as a convenient way to acquire an
RMI
client
to connect to your
JMX
agent. Currently, the factory class returns a client with all
the default values, which will connect to the agent. You will learn how to change
the defaults for the
RMI
connector in chapter 9. Listing 3.5 shows the class.
Listing 3.5
RMIClientFactory.java
package jmxbook.ch3;
import javax.management.*;
import com.sun.jdmk.comm.*;
public class RMIClientFactory
{
public static RmiConnectorClient getClient()
{
RmiConnectorClient client = new RmiConnectorClient();
RmiConnectorAddress address = new RmiConnectorAddress();
address.setPort( 2099 );
System.out.println("\t\tTYPE\t= " +
address.getConnectorType ());
System.out.println("\t\tPORT\t= " + address.getPort());
System.out.println("\t\tHOST\t= " + address.getHost());
System.out.println("\t\tSERVER\t= " + address.getName());
try
{
client.connect( address );
}
catch( Exception e )
{
ExceptionUtil.printException( e );
}
return client;
}
}