boolean isMXBean) {
super(implementation, mbeanInterface, isMXBean);
}
public
throws NotCompliantMBeanException {
super(implementation, mbeanInterface);
}
@Override
public int getFailedServiceCalls() {
return totalFailedCalls;
}
@Override
public long getMaxResponseTime() {
return maxResponseTime;
}
@Override
public long getMinResponseTime() {
return minResponseTime;
}
@Override
public String getName() {
return "AService-Component";
}
@Override
public String setName(String name) {
//name = name;
}
@Override
public int getSuccessServiceCalls() {
return totalCalls - totalFailedCalls;
}
@Override
public int getTotalServcieCalls() {
return totalCalls;
}
@Override
public long getAverageResponseTime() {
// return totalResponseTime/(totalCalls-totalFailedCalls);
return avgResponseTime;
}
/**
* methods exposed to business component, via which they can feed the data to MBean.
*
**/
public static void addAServiceCall(boolean result, long responseTime) {
totalCalls ++;
if(result) {
if(responseTime < minResponseTime) minResponseTime = responseTime; if(responseTime > maxResponseTime) maxResponseTime = responseTime;
totalResponseTime = ++ responseTime;
} else {
totalFailedCalls++;
}
}
public static void setMaxTime(long maxResultTimeMilli) {
maxResponseTime = maxResultTimeMilli;
}
public static void setAvgTime(long averageResultTimeMilli) {
avgResponseTime = averageResultTimeMilli;
}
public static void setTotalCount(int thread_count) {
totalCalls = thread_count;
}
public static void setMinTime(long minResultTimeMilli) {
minResponseTime = minResultTimeMilli;
}
}
Register the MBean with JVM's MBean Server
Once an MBean is implemented, it needs to be registered with platform MBean, so the MBean is visible to the internal MBean Server. For simplicity you can have the registering the logic as part of the custom MBean implementation. But I would prefer to isolate the registration logic from business computation logic, into another class solely responsible to register and retrieve the custom MBeans.
• Get instance of MBeanServer using ManagementFactory.getPlatformMBeanServer()
• Create an instance of custom MBean with a name
• Register with the MBeanServer
try {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
AServiceMBeanImpl mbean = new AServiceMBeanImpl();
ObjectName name = new ObjectName(
"com.sample.mbean:type=AServiceMBean");
mbs.registerMBean(mbean, name);
} catch (MalformedObjectNameException e) {
e.printStackTrace();
System.exit(0);
} catch (InstanceAlreadyExistsException e) {
e.printStackTrace();
System.exit(0);
} catch (MBeanRegistrationException e) {
e.printStackTrace();
System.exit(0);
} catch (NotCompliantMBeanException e) {
e.printStackTrace();
}
Expose methods for business component
It is a method exposed to business component. A business component invokes this method and feeds the component's raw data in. It the custom bean applies the logic on and prepares the metrics ready
public static void addAServiceCall(boolean result, long responseTime) {
totalCalls ++;
if(result) {
if(responseTime < minResponseTime) minResponseTime = responseTime; if(responseTime > maxResponseTime) maxResponseTime = responseTime;
totalResponseTime = ++ responseTime;
} else {
totalFailedCalls++;
}
}
Executing the MBean
Once all the above steps are complete, make sure invoke the register method during application star up. It can be invoked however you want but need to make sure the MBean registered once before trying to access from the JMX monitoring tool.
As and when the business component wants feed the data, invoke the exposed method as below.* It can use any of the exposed to methods.
//Feed the MBean...
AServiceMBeanImpl. addAServiceCall(true, timeinmilli);
AServiceMBeanImpl.setMaxTime(maxResultTimeMilli);
AServiceMBeanImpl.setMinTime(minResultTimeMilli);
AServiceMBeanImpl.setAvgTime(averageResultTimeMilli);
AServiceMBeanImpl.setTotalCount(THREAD_COUNT);
Monitoring the MBean
Using the regular approach connect any JMX based monitoring tool (eg. Visual VM), navigate to MBean section, the custom AServiceMBean Service will be shown with the available data.