Wednesday 24 February 2010

Invoking Custom MBeans via Java

A third way of invoking a method on a custom MBean is to actually embed the call within a Java method.

I already have a web application with EJB tier behind it so decided to enable a button on the web app which would call through to the EJB and then lookup the custom MBean and call the initialise method on it.

Within the EJB container you can get a default InitialContext using the authenticated user's credentials:

Context ctx = new InitialContext();

Then lookup WebLogic's runtime MBean Server as follows:

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

Call to retrieve all MBeans:
Set names = server.queryNames(null, null);

Loop through to find your desired bean and call invoke the on the WebLogic server runtime object:

for(ObjectName name:names)
{
if(name.toString().equals("com.workflow.server.infrastructure:type=WFEngineMBean,name=WFEngineMBean")))
{
server.invoke(name, "forceEngineInitialisation", null, null);
break;
}
}

This is a very quick knock up of how to do this programmatically. I believe you can improve your search results from the server by modiying the method: server.queryNames(null, null); and passing details of the MBean you require. Haven't got that to work yet though.

No comments: