JMX and MBeans

JMX is used to manage Java processes and to control Java components on a server. MBeans are POJO (Plain Old Java Objects) that have attributes and operations. These MBeans are registered with a JMX Container and are managed through a special web page called a JMX Console. These MBeans, its operations and attributes show up on the JMX console. Through the JMX console (web page) certain operations can be performed on the MBean class, and its attribute values changed. Any Java class can be managed as MBeans registered in a JMX container. MBeans are used for user interaction, more like super-user interaction. The super user (like an administrator) can click on the MBean on the JMX console to perform some operation or change MBean attributes.
So what you do is write a Java class with some methods; then you register that MBean with the JMX container so that it shows up on the JMX console.
Log4j has jmx capability. If log4j is registered in a JMX server then it shows up as an MBean. You can click on it thru the console and change its logging level without having to restart the server. You may want to change error logging from ERROR to DEBUG by clicking on the log4j MBean in the JMX console. After you are done with debugging you could click on it to switch back to ERROR.

HelloMBean.java

/* HelloMBean.java – MBean interface describing the management
operations and attributes for the Hello World MBean. In this case
there is an operation, “concatt”, attribute, “Name”. */


package com.example.mbeans;

public interface HelloMBean {
	// operation

	public String concatt(String x, String y);

	// attribute

	public String getName();

	public void setName(String name);

}

Hello.java

/* Hello.java – MBean implementation for the Hello World MBean. This class must implement all the Java methods declared in the HelloMBean interface, with the appropriate behavior for each one. */


package com.example.mbeans;

import java.lang.*;

public class Hello implements HelloMBean {
	private String name = "Reginald";

	public String concatt(String x, String y) {
		return x + y;
	}

	/*
	 * Getter for the Name attribute. The pattern shown here is frequent: the
	 * getter returns a private field representing the attribute value. Consider
	 * an attribute representing statistics such as uptime or memory usage, for
	 * example. Being read-only just means that it can't be changed through the
	 * management interface.
	 */
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

jmx.jsp

<%@ page import="javax.management.*,java.util.*,java.lang.management.*,com.example.mbeans.*"%>
<%
// Get the Platform MBean mbs mbeansever
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
// Construct the ObjectName for the MBean we will register
ObjectName name = new ObjectName("com.example.mbeans:type=Hello");
Hello mbean = null;
//Update MBean with request parameter
String newvalue = (String)request.getParameter("Name");
if (newvalue == null || newvalue.length() <= 0)
{
// Create the Hello World MBean
mbean = new Hello();
// Register the Hello World MBean
mbs.registerMBean(mbean, name);
}
try {
if (newvalue != null && newvalue.length() > 0) {
Attribute attr = new Attribute("Name", newvalue);
mbs.setAttribute(name, attr );
}
//Get latest mbean attribute
String value = (String)mbs.getAttribute( name,"Name");
out.println("THE attribute of the mbean has changed to "+value);
 
%>
 
 
MBean <%= name.getCanonicalName() %>
 
 
<FORM METHOD="post" ACTION="jmx.jsp">
    <TABLE ALIGN="left" BORDER="1" WIDTH="40%" CELLPADDING="3">
        <TR>
            <TH WIDTH="23%">Attribute</TH>
            <TH WIDTH="35%">Value</TH>
        </TR>
        <TR>
            <TD><B>Enter Name: </B>
            </TD>
            <TD><INPUT TYPE="text" NAME="Name" VALUE="<%= value %>"
                SIZE="34%">
            </TD>
        </TR>
        <TR>
            <TD ALIGN=LEFT><INPUT TYPE="submit" VALUE="UPDATE">
            </TD>
        </TR>
    </TABLE>
</FORM>
 
<%
} catch (Exception e) {
out.println(e.getMessage());
}
 
%>

jmx_call.jsp

<%@ page import="javax.management.*,java.util.*,java.lang.management.*,com.example.mbeans.*"%>
 
<%
    // Get the Platform MBean mbs mbeansever
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    // Construct the ObjectName for the MBean we will register
    ObjectName name = new ObjectName("com.example.mbeans:type=Hello");
    Hello mbean = null;
    //Update MBean with request parameter
    String newvalue1 = (String) request.getParameter("valueA");
    String newvalue2 = (String) request.getParameter("valueB");
    String value = null;
 
    if (newvalue1 == null && newvalue2 == null) {
        // Create the Hello World MBean
        mbean = new Hello();
        // Register the Hello World MBean
        mbs.registerMBean(mbean, name);
    }
    try {
        if ((newvalue1 != null && newvalue1.length() > 0
                && newvalue2 != null && newvalue2.length() > 0)) {
            Object params[] = { newvalue1, newvalue2 };
            String[] signature = { "java.lang.String",
                    "java.lang.String" };
            value = (String) mbs.invoke(name, "concatt", params,
                    signature);
            out.print("THE String is " + value);
        }
 
        //Get latest mbean attribute
%>
MBean
<%=name.getCanonicalName()%>
<FORM METHOD="post" ACTION="jmx_call.jsp">
    <TABLE ALIGN="left" WIDTH="40%" CELLPADDING="3">
        <TR>
            <TH WIDTH="23%">Add</TH>
            <TH WIDTH="35%">These values</TH>
        </TR>
        <TR>
            <TD><B>Enter Values: </B></TD>
            <TD><INPUT TYPE="text" NAME="valueA" VALUE=0 SIZE="34%">
            </TD>
            <TD><INPUT TYPE="text" NAME="valueB" VALUE=0 SIZE="34%">
            </TD>
        </TR>
        <TR>
            <TD ALIGN=LEFT><INPUT TYPE="submit" VALUE="UPDATE"></TD>
        </TR>
    </TABLE>
</FORM>
 
<%
    } catch (Exception e) {
        out.println(e.getMessage());
    }
%>

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: