Java Reference
In-Depth Information
adapter.setPort( 9092 );
mbs.registerMBean( adapter, adapterName );
adapter.start();
}
catch( Exception e )
{
e.printStackTrace();
}
}
public static void main( String args[] )
{
System.out.println( "HelloAgent is running" );
HelloAgent agent = new HelloAgent();
}
}//class
E
Registers and
starts HTML
adapter MBean
Note the
import
statements in the listing. All classes in the
JMX
framework are in
the package
javax.management
. An additional package,
com.sun.jdmk.comm
, is
provided by the
RI
provider, Sun Microsystems. This package is considered an
unsupported contribution, meaning that the code is useful for
JMX
development
but is not part of the
JMX
specification. In particular, this package contains the
classes you need to instantiate the
HTML
adapter.
Creating the MBean server and HTML adapter
The
HelloAgent
class implements a
main
method that allows it to be started as a
standalone process. All it does is call the
HelloAgent
constructor, so let's start our
code examination there. The first step performed by the agent constructor is the
creation of the
MBeanServer
object.
Remember from chapter 1 that the MBean server is a Java object used to con-
tain and manipulate
JMX
MBeans. The MBean server is a standard
JMX
class
and is the heart of
JMX
agents. Agents acquire an instance of the
MBeanServer
class by using the
javax.management.MBeanServerFactory
class. This
MBeanServ-
erFactory
class is a
JMX
class that implements the factory pattern to provide
instances of the
MBeanServer
class. When you need an instance of an
MBean-
Server
, use the factory object to create or acquire a new instance. The factory can
manage many instances of the
MBeanServer
class, returning a specific instance or
creating a new one (as in the case of the
HelloAgent
class).
Notice that the parameter
HelloAgent
was passed to the factory's
create-
MBeanServer()
method. This parameter is a
String
value indicating the name for
this agent's domain. A domain name is a unique identifier used to indicate a
B
C