Sun Microsystems
Products & Services
 
Support & Training
 
 

Previous Previous     Contents     Index     Next Next
Chapter 5

Base Agent

An agent application is a program written in the Java language that contains an MBean server and a means of accessing its functionality. The base agent demonstrates the programming details of writing an agent application. We will cover the MBean server classes, how to reference MBeans, how to access the MBean server, use it to create MBeans, and then interact with those MBeans.

Interacting programmatically with the MBean server gives you more flexibility in your agent application. This chapter presents the MBean server and covers the three main ways to use it to create MBeans, how to interact with these MBeans, and how to unregister them.

The program listings in this tutorial show only functional code: comments and output statements have been modified or removed to conserve space. However, all management functionality has been retained for the various demonstrations. The complete source code is available in the BaseAgent and StandardMBean example directories located in examplesDir/current (see Directories and Classpath in the Preface).

This chapter covers the following topics:

5.1 MBean Server Classes

Before writing an agent application, it is important to understand the functionality of the MBean server. It is actually an interface and a factory object defined by the agent specification level of the JMX specification. The Java DMK provides an implementation of this interface and factory. The factory object finds or creates the MBean server instance, making it possible to substitute different implementations of the MBean server.

5.1.1 MBeanServer Interface

The MBeanServer interface is an extension of the MBeanServerConnection interface, and represents the agent-side interface for interacting with MBeans.

The specification of the interface defines all operations that can be applied to resources and other agent objects through the MBean server. Its methods can be divided into three main groups:

  • Methods for controlling MBean instances:

    • createMBean, or instantiate and registerMBean add a new MBean to the agent

    • unregisterMBean removes an MBean from the agent

    • isRegistered and getObjectInstance associate the class name with the MBean's management name

    • addNotificationListener and removeNotificationListener control event listeners for a particular MBean

    • getClassLoader is used to download new MBean classes

  • Methods for accessing MBean attributes and operations. These methods are identical to those presented in 2.1.1 DynamicMBean Interface, except they all have an extra parameter for specifying the target MBean:

    • getMBeanInfo

    • getAttribute and getAttributes

    • setAttribute and setAttributes

    • invoke

  • Methods for managing the agent as a whole:

    • getDefaultDomain (domains are a way of grouping MBeans in the agent)

    • getMBeanCount counts all MBeans in an agent

    • queryMBeans and queryNames to find MBeans by name or by value

5.1.2 MBean Server Implementation, Builder and Factory

The MBeanServer implementation class in Java DMK implements the JdmkMBeanServer and MBServerInt interfaces if the property javax.management.builder.initial has been set to use com.sun.jdmk.JdmkMBeanServerBuilder. This implementation of MBeanServer wraps the JMX MBean server.

The older MBeanServerInt interface and the MBeanServerImpl class are still implemented for reasons of backwards compatibility, but in Java DMK 5.1, JdmkMBeanServer is the prefered interface.

The MBeanServerFactory makes the agent application independent of the MBean server implementation, and thus allows applications to provide custom MBeanServer implementations. It resides in the Java virtual machine and centralizes all MBean server instantiation. The MBeanServerFactory class provides two static methods:

  • createMBeanServer returns a new MBean server instance.

  • findMBeanServer returns a list of MBean servers in the Java virtual machine.

You must use the MBeanServerFactory classes to create an MBean server so that other objects can obtain its reference by calling the findMBeanServer method. This method enables dynamically loaded objects to find the MBean server in an agent that has already been started.

Previous Previous     Contents     Index     Next Next