# Working with Management Beans

The content of this guide is the following:

1. Target Audience and Rationale
2. About JOnAS MBeans and their use in JonaSAdmin
3. Using JOnAS MBeans in a Management Application
4. Registering User MBeans

## Target Audience and Rationale

This chapter is intended for advanced JOnAS users who are interested in understanding management facilities that JOnAS provides, and possibly extending these facilities for their application needs.

JOnAS management facilities are based on Management Beans (MBeans) compliant to JMX Specification. Moreover, JOnAS implements JSR 77 specification, which defines the management model for J2EE platforms.

## About JOnAS MBeans and their use in JonaSAdmin

MBeans provide access to management functions such as configuration, deployment/undeployment, monitoring of resources and application modules.

MBeans are created not only by the different JOnAS services, but also by the components integrated in JOnAS (Web server Tomcat or Jetty, JORAM MOM, etc.). They are registered in the current MBean Server, which is started by each JOnAS server instance. Remote access to the MBean Server is facilitated by JMX remote connectors compliant to the JSR 160 specification. See more information about connectors here.

JonasAdmin application implements the management functions listed above using the different MBeans registered in the MBeanServer of the JOnAS instance currently being managed. This is usually the server on which JonasAdmin is deployed, but it may be another server running in the same management domain.

JonasAdmin also presents, in a structured way, all the registered MBeans, their attributes and operations. In the future, JonasAdmin will probably be extended to allow setting attributes and invoking operations.

## Using JOnAS MBeans in a Management Application

In order to invoke a management operation on a MBean, the caller must access to the MBean server.

### Local MBean Server

When the caller is located in the same JVM as the MBean Server, it can use `javax.management.MBeanServerFactory` class to obtain a reference to the MBean Server:

                    List mbeanServers = MBeanServerFactory.findMBeanServer(null);
                    if (mbeanServers != null && mbeanServers.size() > 0) {
                            return (MBeanServer) mbeanServers.get(0);
                    }

### Using Remote Connectors

When the caller is remote, it can use a JMX remote connector to establish a connection with the MBean Server and obtain a `javax.management.MBeanServerConnection` object.

Suppose that the connector server has the following address: `service:jmx:jrmp://host/jndi/jrmp://host:1099/jrmpconnector_jonas`, which is the default for a JOnAS server called `jonas`.

                    JMXServiceURL connURL = new JMXServiceURL("service:jmx:jrmp://host/jndi/jrmp://host:1099/jrmpconnector_jonas");
                    JMXConnector connector = JMXConnectorFactory.newJMXConnector(connURL, null);
                    connector.connect(null);
                    MBeanServerConnection conn = connector.getMBeanServerConnection();
                    return conn;

### Using a Management EJB

A remote caller can also use the MEJB provided by the JOnAS distribution. A Management EJB implementation, compliant to the JSR 77, is packed in the `mejb.ear` installed in the `JONAS_ROOT/ejbjars/autoload` directory. Thus, the MEJB is automatically deployed at JOnAS start-up. Its `Home` is registered in JNDI under the name `ejb/mgmt/MEJB`. JOnAS distribution also contains an example using the MEJB in a J2EE application, the `j2eemanagement` sample.

### Using the Management Web Service Endpoint

A remote caller can use the Management Web Service endpoint packaged as a part of the `mejb.ear` installed in the `JONAS_ROOT/ejbjars/autoload` directory. Thus, the management endpoint is automatically deployed at JOnAS start-up. Check `http://<hostname>:<port>/mejb/ManagementEndpoint/ManagementEndpoint?wsdl` for the WSDL file on a running JOnAS instance. The endpoint allows light-weight clients to query JOnAS MBeans from virtually any platform by leveraging the platform-independant nature of Web Services.

## Registering User MBeans

Application MBeans can be created and registered in the MBean server by calling `registerMBean` method on the `MBeanServer` object or `createMBean` method on the `MBeanServerConnection` object. Also, MBeans can be loaded using the m-let service, a dynamic loading mechanism provided by the JMX implementation. This mechanism allows loading MBeans from a remote URL. The information on the MBeans to load is provided in a m-let text file. Refer to JMX implementation documentation for details concerning this file. In addition, the howto document JOnAS and JMX, registering and manipulating MBeans illustrates the use of this m-let mechanism. In order to make an m-let text file accessible to JOnAS applications, it can be installed in the `JONAS_ROOT/conf` directory.