Java Reference
In-Depth Information
Listing 3.2
startHTMLAdapter() method that adds the HTML adapter to the agent
protected void startHTMLAdapter()
{
HtmlAdaptorServer adapter = new HtmlAdaptorServer();
ObjectName adapterName = null;
try
{
adapter.setPort( 9092 );
//create the HTML adapter
adapterName = new ObjectName(
"JMXBookAgent:name=html,port=9092" );
server.registerMBean( adapter, adapterName );
adapter.start();
}
catch(Exception e)
{
ExceptionUtil.printException( e );
System.out.println("Error Starting HTML Adapter for Agent");
}
}
Remember from the previous chapter that the
HTML
adapter is an MBean and
therefore must be registered with the agent like any other MBean. It needs an
ObjectName
instance, which you provide with the domain of the agent and a few
descriptive properties. Once it is registered on the MBean server, you call its
start()
method to initialize it. If any errors occur, you print them out to the
agent output.
Notice in the
catch
block that the method uses a class called
ExceptionUtil
;
it's a utility class that you'll write at the end of this chapter. It contains a single
static method,
printException()
, which prints
MBeanException
s and the excep-
tions wrapped within.
Adding the RMI connector
You have seen the
HTML
adapter created and registered twice in this topic, but
you have yet to see any code for the
RMI
connector. (Remember, we aren't cover-
ing the connector extensively in this chapter—we're only showing how to use it
during a simple test. Later chapters use the connector to add and manipulate
MBeans in the agent. For a detailed discussion of the connector, you can jump
ahead to chapter 9.)
Listing 3.3 shows the
startRMIConnector()
method that is invoked by the agent
constructor. Its purpose is to create and start the
RMI
connector for this agent.