Registering Custom MBeans with Weblogic Server

Registering Custom Mbeans with Weblogic Server.

Example.java

package jmxMbeans;

public class Example implements ExampleMBean {

public void sayHello(String str) {

System.out.println(“Hello ” + str + “‘!”);

}

}

ExampleMbean.java

package jmxMbeans;

public interface ExampleMBean {

void sayHello(String name);

}

Index.jsp

<%@ page import=”javax.management.MBeanServer”%>

<%@ page import=”javax.management.ObjectName”%>

<%@ page import=”javax.naming.Context”%>

<%@ page import=”javax.naming.InitialContext”%>

<%@ page import=”java.util.Hashtable”%>

<%@ page import=”jmxMbeans.ExampleMBean”%>

<%@ page import=”jmxMbeans.Example”%>

<html>

<body>

<p>Registering Mbeans.</p>

<%

Example ex = new Example();

MBeanServer mbeanServer = null;

Hashtable<String, String> env = new Hashtable<String, String>();

env.put(Context.SECURITY_PRINCIPAL, “weblogic”);

env.put(Context.SECURITY_CREDENTIALS, “weblogic”);

InitialContext ic = new InitialContext(env);

mbeanServer = (MBeanServer) ic.lookup(“java:comp/env/jmx/runtime”);

String MBEAN_OBJECT_NAME = “jmxMbeans:Name=Example,Type=ExampleMBean”;

registerMBean(mbeanServer,MBEAN_OBJECT_NAME,ex);

unregisterMBean(mbeanServer,MBEAN_OBJECT_NAME);

%>

<%!

void registerMBean(MBeanServer server,String mbeanObjectName,Object obj) {

ObjectName objectName = null;

try {

objectName = new ObjectName(mbeanObjectName);

server.registerMBean(obj, objectName);

System.out.println(“MBean registered:” + objectName);

} catch (Exception e) {

//log.log(Level.SEVERE, “Error registering MBean ” + objectName, e);

}

}

%>

<%!

void unregisterMBean(MBeanServer server,String mbeanObjectName) {

ObjectName objectName = null;

try {

objectName = new ObjectName(mbeanObjectName);

server.unregisterMBean(objectName);

System.out.println(“MBean unregistered: ” + objectName);

} catch (Exception e) {

System.out.println(“Error unregistering MBean ” + objectName);

e.printStackTrace();

}

}

%>

</body>

</html>


Invoking the Custom MBean

test.jsp

Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,”weblogic.jndi.WLInitialContextFactory”);
env.put(Context.SECURITY_PRINCIPAL, “weblogic”);
env.put(Context.SECURITY_CREDENTIALS, “weblogic”);
InitialContext ctx = new InitialContext(env);

try{

String MBEAN_OBJECT_NAME = “jmxMbeans:Name=Example,Type=ExampleMBean”;
MBeanServer server = (MBeanServer) ctx.lookup(“java:comp/env/jmx/runtime”);
ObjectName objName = new ObjectName(MBEAN_OBJECT_NAME);
Object[] params = new Object[] {“Faisal”};
String[] sigs = new String[] {“java.lang.String”};
server.invoke(objName, “sayHello”, (Object[])params, sigs);
}
catch(Exception e){
e.printStackTrace();
}
ctx.close();

2 comments

  1. Good post.But i have a different issue. If I create my own MBean Server like belowMBeanServerFactory.createMBeanServer("mydomain");I can able to access this custom MBean Server within the application. If i want to access it from a stand alone client program, How to do that?ThanksSree

Comments are closed.