Skip to main content

Self Managing the JBI runtime in GlassFish

Posted by bhavanishankar on May 22, 2007 at 3:49 AM PDT

 
It is possible to self manage the JBI runtime using the self management module of the GlassFish application server. I am showing one such example here.

In this example, we have a timer service registered with the self management module which checks whether all the required components for the deployed JBI service assemblies are UP or not. Corrective action :

    (1) Try to bring up the components required by the running JBI service assemblies.
    (2) If (1) fails, log the error in server.log. (The example can be enhanced to send an email to administrator).

Here is the MBean code which receives the notification from the self management module, does the necessary checks and takes the corrective action.

SelfManageJBIComps.java :


package com.sun.samples.appserver;

import java.util.ArrayList;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.ObjectName;

/**
*
* @author Bhavanishankar <bshankar@sun.com>
*
*/

public class SelfManageJBIComps
implements SelfManageJBICompsMBean, SelfManageJBICompsConstants {

public synchronized void handleNotification(
Notification notification,
Object obj) {

StringBuffer debugMessage = new StringBuffer
("\n\nSelfManageJBIComps :: \n");

System.out.println("SelfManageJBIComps.handleNotification : " +
notification.getClass().getName());

/**
* Allow the JBI framework to initialize itself properly.
*/
if(firstInvocation) {
firstInvocation = false;
return;
}

/**
* Step 1. Get the connection to the MBean server.
*/
initMBeanConnection();

/**
* Step 2. Get the list of deployed service assemblies.
*/
Object ret = invokeMBean(
getDeploymentServiceMBeanName(),
"getDeployedServiceAssemblies",
new Object[]{},
new String[]{});

if(ret == null) {
return;
}
String[] serviceAssemblies = (String[]) ret;

/**
* Step 3. For each service assembly which is in "Started"
* state, if the required component's state is not started
* then start the component.
*/

for(String sa : serviceAssemblies) {
debugMessage.append("\n\tProcessing ServiceAssembly " +
sa + "...");
/**
* Step 3.1. Get the state of the service assembly.
*/
ret = invokeMBean(
getDeploymentServiceMBeanName(),
"getState",
new Object[] {sa},
new String[] {"java.lang.String"});
if(ret == null) {
continue;
}
String saState = (String)ret;

debugMessage.append("\n\tState of the ServiceAssembly is " +
saState);

/**
* Step 3.2 If the state is "Started" then
* get the list of Components for this service assembly.
*/
if(!"Started".equalsIgnoreCase(saState)) {
continue;
}

ret = invokeMBean(
getDeploymentServiceMBeanName(),
"getComponentsForDeployedServiceAssembly",
new Object[] {sa},
new String[] {"java.lang.String"});
if(ret == null) {
continue;
}
String[] jbiComps = (String[])ret;

for(String jbiComp : jbiComps) {
debugMessage.append("\n\n\t JBIComponent used " +
"by " + sa + " is " + jbiComp);

ret = invokeMBean(
getAdminServiceMBeanName(),
"getComponentByName",
new Object[]{jbiComp},
new String[]{"java.lang.String"});
if(ret == null) {
continue;
}
ObjectName compName = (ObjectName)ret;

ret = invokeMBean(
compName,
"getCurrentState",
new Object[]{},
new String[]{});

if(ret == null) {
continue;
}

String componentState = (String) ret;
debugMessage.append("\n\t State of the " + jbiComp +
" is " + componentState);

if(!"started".equalsIgnoreCase(componentState)) {
debugMessage.append("\n\t Starting " +
jbiComp + "...");
ret = invokeMBean(
compName,
"start",
new Object[] {},
new String[] {});
debugMessage.append("\n\t Successfully started " +
jbiComp);
}
}
}
System.out.println(debugMessage.toString());
}

private static MBeanServer mBeanServer;
private static boolean firstInvocation = true;

/**
* Get the connection to the MBean server.
*/
private synchronized void initMBeanConnection() {
if(mBeanServer == null) {
ArrayList<MBeanServer> mBeanServers =
MBeanServerFactory.findMBeanServer(null);
if(!mBeanServers.isEmpty()) {
mBeanServer = mBeanServers.get(0);
}
if(mBeanServer == null) {
System.err.println("Unable " +
"to obtain connection to MBean server");
}
}
}

private Object invokeMBean(
ObjectName mBeanName,
String methodName,
Object[] params,
String[] signatures) {
Object output = null;
try {
output = mBeanServer.invoke(mBeanName,
methodName, params, signatures);
} catch(Exception ex) {
ex.printStackTrace();
}
return output;
}

private ObjectName getDeploymentServiceMBeanName() {
ObjectName objName = null;
try {
objName = new ObjectName(DEPLOYMENT_SERVICE_MBEAN);
} catch(Exception ex) {
ex.printStackTrace();
}
return objName;
}

private ObjectName getAdminServiceMBeanName() {
ObjectName objName = null;
try {
objName = new ObjectName(ADMIN_SERVICE_MBEAN);
} catch(Exception ex) {
ex.printStackTrace();
}
return objName;
}

}


SelfManageJBICompsMBean.java :
[prettifyclass="prompt"]package com.sun.samples.appserver;<br /><br />import javax.management.NotificationListener;<br /><br />/**<br /> *<br /> * @author Bhavanishankar &lt;bshankar@sun.com&gt;<br /> *<br /> */<br /><br />public interface SelfManageJBICompsMBean <br /> extends NotificationListener {<br /> <br />}<br />[/prettify]

 

Related Topics >>