Using the JMX MBeanServer API
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Servlet
JMX Consoles
Instrumenting
Tutorials
Cookbook

Basic JMX
MBeanServer
Listener
Registration
Basic JMX
Tutorials
Listener

Find this tutorial in: /usr/local/resin/webapps/resin-doc/jmx/tutorial/mbean-server
Try the Tutorial
Example showing JMX-managed resources using the MBeanServer API.

  1. Files in this tutorial
  2. JMX Resource
    1. web.xml configuration
    2. Using MBeanServer
  3. Compatibility

Files in this tutorial

WEB-INF/web.xml Configures the JMX-managed bean
WEB-INF/classes/example/Test.java The resource bean implementation.
WEB-INF/classes/example/TestAdmin.java The management interface for the bean.
index.jsp Using the managed bean.

JMX Resource

Any resource in Resin can be managed by JMX by implementing an MBean interface and by specifying an MBean name. The interface exposes the resource's methods to be managed.

The test resource is identical to the basic example but implements TestAdmin instead of TestMBean. Because the name TestAdmin does not conform to the MBean convention, the web.xml will need to specify the interface explicitly.

Test.java
package example;

public class Test implements TestMBean {
  private String _data = "default";

  public void setData(String data)
  {
    _data = data;
  }

  public String getData()
  {
    return _data;
  }
}

web.xml configuration

The web.xml (or resin.conf) configures the resource with the <resource> tag just as with other resources . The resources is registered as an MBean by specifying an mbean-name.

web.xml
<web-app xmlns="http://caucho.com/ns/resin">
  <resource mbean-name="example:name=basic"
            type="example.Test"
            mbean-interface="example.TestAdmin>
    <init>
      <data>An Example Resource</data>
    </init>
  </resource>
</web-app>

tagdescription
resourcedefines the resource
mbean-namethe MBean name of the resource
typethe class name of the resource bean
mbean-interfacethe class name to use as the managed interface
initAny bean-style configuration goes here
dataThe example bean's setData parameter.

Using MBeanServer

MBeanServer is the main JMX interface for managing resources. Although it is less convenient than Resin's proxy interface, it has the advantage of being part of the JMX standard.

Resin stores the MBeanServer it uses for resources in JNDI under the name java:comp/env/jmx/MBeanServer. Applications will use JNDI to get the MBeanServer. This JNDI name is not specified in any standard, so applications might want to encapsulate getting the MBeanServer in a class that can be changed easily.

All management of an MBean uses the MBeanServer and the MBean's ObjectName. In this case, the ObjectName is "example:name=test".

The MBeanServer has three primary management calls: getAttribute, setAttribute, and invoke. This example just uses getAttribute.

index.jsp
<%@ page import='javax.naming.*, javax.management.*, example.TestAdmin' %>
<%
Context ic = new InitialContext();

MBeanServer server = (MBeanServer) ic.lookup("java:comp/env/jmx/MBeanServer");

ObjectName name = new ObjectName("example:name=test");

out.println("data: " + server.getAttribute(name, "Data"));
%>

data: An example resource

Compatibility

Using the MBeanServer interface is compatible with other JMX implementations. The two Resin-dependencies are the configuration and how to obtain the Resin MBeanServer. Different JMX implementations will use a different technique to get the MBeanServer, so it's a good idea to encapsulate getting the MBeanServer in a class that you can change for different implementations.

Try the Tutorial


Basic JMX
Tutorials
Listener
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.