The XMOJO Project
<< Prev Chapter 2.1.1 Writing a Standard MBean Next >>

Writing Your Own Standard MBean


Let us create a standard MBean to manage the details of a Web server.

The following details can be exposed as attributes:
  1. ServerName: The name of the machine where the server is running.
  2. ServerId: An identifier for the Server. In a same machine, multiple servers may run at different ports.
  3. ServerStarted: To check whether the server is alive.
  4. Port: The port at which the server is listening.
The following details can be exposed as operations:
  1. startService: To start the server
  2. stopService: To stop the server
For an MBean to be of type standard MBean, it should implement its own corresponding MBean interface.

First, we will define the interface for this standard MBean. Let our standard MBean be ServerInfo. So, our management interface should be ServerInfoMBean (following the lexical naming pattern for standard MBean definition defined in JMX specifications).

public abstract interface ServerInfoMBean
{
      public String getServerName( );

      public String getServerId( );

      public boolean isServerStarted( );

      public int getPort( );

      public void setPort(int port);

      public void startService( );

      public void stopService( );
}


From the above interface we designed, the MBean has get methods for the attributes ServerName, ServerId, ServerStarted, and Port. The set methods are available only for the attribute Port. So, ServerName, ServerId, and ServerStarted form the read only attributes, and Port is a read write attribute.


public class ServerInfo implements ServerInfoMBean  
{

      private String serverName = null;

      private String serverId = null;

      private boolean serverStarted;

      private int port;

      // At least one public constructor is required
   
      public ServerInfo( )
      {
          serverName = "test-server";
          serverId = "test-server_1";
          serverStarted = true;
          port = 8072;
      }

      // overloaded public constructor
   
      public ServerInfo(String serverName, String serverId, boolean serverStarted,  int port)
      {
          this.serverName = serverName ;
          this.serverId = serverId ;
          this.serverStarted = serverStarted ;
          this.port = port ;
      }

      // Implementating the ServerInfoMBean

      public String getServerName( )
      {
          // get the ServerName
          return serverName;
      }

      public String getServerId( )
      {
          // get the ServerIdentifier
          return serverId;
      }

      public boolean isServerStarted( )
      {
          // check whether the Server is Started
          return serverStarted;
      }

      public int getPort( )
      {
          // get the ServerPort
          return port;
      }

      public void setPort(int port)
      {
          // Stop the server.  Set the ServerPort.  Start the server.
          stopService( );
          this.port = port;
          startService( );
      }

      public void startService( )
      {
          System.out.println
("Starting server..... ");
          // start the Server
          System.out.println
("Server started successfully. ");
          serverStarted =true;
      }

      public void stopService( )
      {
          System.out.println
("Stopping server..... ");
          // stop the Server
          System.out.println
("Server stopped ");
          serverStarted =false;
      }

       // Additional methods not exposed for management

      public void restart( )
      {
          stopService( );
          startService( );
      }
}

In the above MBean, the method restart will not be exposed since it is not defined in the management interface.

To see a working example of the above Standard MBean, try out this example.

<< Prev Home Next >>
Standard MBean Attribute Name Overloading