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();

Latest Comments

  1. Raj November 30, 2009
  2. sreecharan March 4, 2010

Leave a Reply