The following code is one of the smaller agents you can write. It retains all of the functionality that we will need to connect to it with a web browser in the next topic ("The HTML Protocol Adaptor").
import javax.management.ObjectInstance; import javax.management.MBeanServer; import javax.management.MBeanServerFactory; public class MinimalistAgent { public static void main(String[] args) { MBeanServer server = MBeanServerFactory.createMBeanServer(); try { ObjectInstance html = server.createMBean( "com.sun.jdmk.comm.HtmlAdaptorServer", null); server.invoke(html.getObjectName(), "start", null, null); } catch(Exception e) { e.printStackTrace(); return; } } } |
Only three classes are "imported" by this program and needed to compile it. However, the MBean server dynamically instantiates other classes such as the HtmlAdaptorServer which are needed at run-time. As a result, the Java Dynamic Management Kit runtime jar file (jdmkrt.jar) must be in the classpath of the Java virtual machine running the minimal agent.
The MinimalistAgent relies on the HTML adaptor, but we could have used any MBean that provides some way of accessing the MBean server. You could even use your own MBean that encodes some proprietary protocol, provided it makes all functionality of the MBean server available remotely.
It is important to realize that this minimalist agent is a fully functional agent that is every bit as powerful as any agent that may be deployed. Since we can connect to this agent, we can dynamically create new MBeans for it to manage, and classes that aren't available locally can be downloaded from the network (this is covered in "The M-Let Class Loader"). Because resources, services and other connections may be added on-the-fly, this agent can participate in any management scheme.
Of course, it is more efficient for the agent application to perform the initialization, including the creation of all MBeans that are known to be necessary. Typically, an agent application also needs to set up data structures or launch specific applications that its resources require. For example, it may establish a database session that an MBean will use to expose stored information. The agent application usually includes everything necessary for making the intended resources ready to be managed within the intended management solution.
However, there is no single management solution. Many different agent applications could perform the same management function, requiring more or less intervention after they are launched. And the flexibility of the Java Dynamic Management Kit means that there are many different management solutions to achieve the same goal. For example, an MBean could also establish the database session itself during its registration phase.