Hi,

Jay SenSharma

Jay SenSharma

Here is a Simple Demo of registering and invoking a Custom MBean in WebLogic Server. This demo requires that you should have a bit idea of using Standard J2SE Reflection APIs.

Step1). Developing the MBean Interface … “MyCustomBean.java Note: Don’t use prefix or Postfix as MBean while declaring your CustomMBean oterwise you need to follow a lot of rules and regulations to make it JMX Compliant….So better use some class/interface names which doesn’t contain word “MBean”…

“MyCustomBean.java”

package mymbeans;
public interface MyCustomBean {
public void helloWorld(String str);
}

Step2). Now develop the Implementation class of above interface which should also extend “javax.management.StandardMBean ”
“MyCustomBean_Impl.java”

package mymbeans;
public class MyCustomBean_Impl extends javax.management.StandardMBean implements MyCustomBean
{
// The Following constructor is Mandatory...as we are extending StandardMBean class
public MyCustomBean_Impl()
{
super(MyCustomBean.class, false);
}
public void helloWorld(String str) {
System.out.println("nnt helloWorld("+str+") Method of  MyCustomBean_Impl invoked...ntHello " + str + "'!");
}
}

Step3). Now Compile the Above two classes and make a JAR of it …“MyCustomBeans.jar” Now Place this Jar inside your <BEA_HOME>/<WL_HOME>/server/lib/mbeantypes

Example: C:bea103wlserver_10.3serverlibmbeantypes

Step4). Now restart your Server

Step5). Now develop a Simple WebApplication to Register and Invoke the Above MBean… which contains following two JSPs…

<%@ page import="javax.management.*,javax.naming.*,java.util.*"%>
<%@ page import="mymbeans.*"%>
<html>
<body>
<%
MyCustomBean CustomBean = new MyCustomBean_Impl();
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 = "mymbeans:Name=MyCustomBean_Impl,Type=MyCustomBean";
registerMBean(mbeanServer,MBEAN_OBJECT_NAME,CustomBean);
out.println("<center><h3>MBean Is Registered with ObjectName: "+MBEAN_OBJECT_NAME);
//unregisterMBean(mbeanServer,MBEAN_OBJECT_NAME);
out.println("<center><h4><a href=invoke.jsp>Click Here To Invoke Above MBean</a>");
%>

<%!
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 Successfully with ObjectName: " + objectName);
}
catch (Exception e)
{
System.out.println("ntUnable to Register MBean " + 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>

Following is the “invoke.jsp” code…

“invoke.jsp”

<%@ page import="javax.management.*,javax.naming.*,java.util.*"%>
<%@ page import="mymbeans.MyCustomBean"%>

<html>
<body>
<p>Invoking Custom Mbeans.</p>
<%
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");
try
{
String MBEAN_OBJECT_NAME = "mymbeans:Name=MyCustomBean_Impl,Type=MyCustomBean";
MBeanServer server = (MBeanServer) ic.lookup("java:comp/env/jmx/runtime");
ObjectName objName = new ObjectName(MBEAN_OBJECT_NAME);
Object[] params = new Object[] {"Java"};
String[] sigs = new String[] {"java.lang.String"};
server.invoke(objName, "helloWorld", (Object[])params, sigs);
out.println("<h2>check The Server STDOUT the CustomBean is Invoked...");
}
catch(Exception e)
{
e.printStackTrace();
}
ic.close();
%>
</body>
</html>

Step6). Now Place the “MyCustomBeans.jar” inside “WEB-INF/lib” of your Application and then Deploy and Hit the Application…

————————————-
.
.

Thanks
Jay SenSharma

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.