If you deploy the ear then you have in the administration page of the deployed application a link to the mbeans.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjmaZeYGRT3tAhoC3_ZNEVxp_RryWkw_oPazmchqzKxwEjiLZ4px8aAzOuFWngwBiOVrEDUvUqEfUI9xGIyoMGdw-iIxItg82zIAQs_hnq0X8cU7VvBUZ0aS5NAltcF0ZUVzgmk9C4ZOk/s320/jmx_1.jpg)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgY2ka3df3fHRKviG8rFWbptIUEnmUCARWKLzZ6eSTuPDj6KzGvHlnlbEAMt_5jpOHscrK-3noNwM-wjSRcnALfmydZnnrbCr1jUW8DnR8fvr6deI1-BkNxig-ph7e8RynrgnfScQY7ZO4/s320/jmx_2.jpg)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNUh-q_-fHqosxOJrBAqpqAPiRLRWpcGXk3Ycnkk9pV70QnFVgr1X9flq-CmQwyvf76RhrzoVpucjzD-LlmE5bhQCT0i3uDiWcvZ6HmobXNa24gK0l77wOmqWiCce38l24ablsIC75TAY/s320/jmx_3.jpg)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiokZjBYwi4eNxT3vuvE-IRQbFcIpomOu454cAinXm1-Wq9O4XkUKu0UrxYUHqWfNFBcaZku9ddVPt6dWEgOaJ4LKbiGBBTuJJtxbNb5HfOVVckgH0ReJLBxLOBX1nj3vq_YVcO6tTm33s/s320/jmx_4.jpg)
In a mbean you have to define some methods
public MBeanAttributeInfo[] getMBeanAttributeInfo() , the attribute information
public MBeanOperationInfo[] getMBeanOperationInfo(), the supported operations
public String getMBeanName() , the name of the bean
public String getMBeanDescription() , bean description
public MBeanConstructorInfo[] getMBeanConstructorInfo() constructor
Here you see the bean.
The next step is to make a ServletContextListener which register the bean when the webapp starts up
the last step is we edit the web.xml for the servercontextlistener
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-hpaCx_LYxuBdn_PRPUnFN3ZpMnN0dCAqN54NrzEQBrn1W0LGuWUzIPOHD0n6LSms1EbCPmvDaAUXekCc1DaALD517ggjh1T4-FlwfCfcLa9QIiZ_sjxFCHIaC_TW6AUZ2tNLlvr6TLg/s320/jmx_8.jpg)
here is the example workspace
public class EmployeesMBean extends StandardMBean implements ManagementMBean {
public EmployeesMBean() throws NotCompliantMBeanException {
super(ManagementMBean.class);
}
private Date time;
public String getMBeanName() {
return this.getClass().getName();
}
public String getMBeanDescription() {
return "jmx example";
}
public MBeanConstructorInfo[] getMBeanConstructorInfo() {
return new MBeanConstructorInfo[] { new MBeanConstructorInfo(getMBeanName(),
"Constructor MBean",
null) };
}
public MBeanAttributeInfo[] getMBeanAttributeInfo() {
MBeanAttributeInfo[] attr =
{ new MBeanAttributeInfo("time", "java.util.Date",
"time when the service is started", true,
false, false)
};
return attr;
}
public MBeanOperationInfo[] getMBeanOperationInfo() {
MBeanOperationInfo[] ops =
new MBeanOperationInfo[] { new MBeanOperationInfo("start",
"start the service.",
null, "void",
MBeanOperationInfo.ACTION),
new MBeanOperationInfo("databaseaction",
"Get the database time",
null, "void",
MBeanOperationInfo.ACTION) };
return ops;
}
public void start() {
this.time = new Date();
}
public void databaseaction() {
this.time = new Date();
}
public Date getTime() {
return time;
}
}
The next step is to make a ServletContextListener which register the bean when the webapp starts up
public class ScottServletContextListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent sce) {
}
public void contextInitialized(ServletContextEvent sce) {
try {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
//Taken:
registerMBean(mbs,
"nl.ordina.scott.jmx.EmployeesMBean",
":type=Scott,name=EmployeesMBean");
startServices();
} catch (Throwable t) {
t.printStackTrace();
}
}
private void registerMBean(MBeanServer server, String beanClass,
String naam) {
try {
Object bean = Class.forName(beanClass).newInstance();
ObjectName oName =
new ObjectName(server.getDefaultDomain() + naam);
server.registerMBean(bean, oName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (MalformedObjectNameException e) {
e.printStackTrace();
} catch (NotCompliantMBeanException e) {
e.printStackTrace();
} catch (InstanceAlreadyExistsException e) {
e.printStackTrace();
} catch (MBeanRegistrationException e) {
e.printStackTrace();
}
}
private void startServices() {
startService(":type=Scott,name=EmployeesMBean");
}
private void startService(String objectname) {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
try {
ObjectName name =
new ObjectName(mbs.getDefaultDomain() + objectname);
mbs.invoke(name, "start", null, null);
} catch (MalformedObjectNameException e) {
e.printStackTrace();
} catch (MBeanException e) {
e.printStackTrace();
} catch (ReflectionException e) {
e.printStackTrace();
} catch (InstanceNotFoundException e) {
e.printStackTrace();
}
}
private void stopServices() {
//Taken:
stopService(":type=Scott,name=EmployeesMBean");
}
private void stopService(String objectname) {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
try {
ObjectName name =
new ObjectName(mbs.getDefaultDomain() + objectname);
mbs.invoke(name, "stop", null, null);
} catch (MalformedObjectNameException e) {
e.printStackTrace();
} catch (MBeanException e) {
e.printStackTrace();
} catch (ReflectionException e) {
e.printStackTrace();
} catch (InstanceNotFoundException e) {
e.printStackTrace();
}
}
}
the last step is we edit the web.xml for the servercontextlistener
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-hpaCx_LYxuBdn_PRPUnFN3ZpMnN0dCAqN54NrzEQBrn1W0LGuWUzIPOHD0n6LSms1EbCPmvDaAUXekCc1DaALD517ggjh1T4-FlwfCfcLa9QIiZ_sjxFCHIaC_TW6AUZ2tNLlvr6TLg/s320/jmx_8.jpg)
here is the example workspace
wht Scott means?
ReplyDeleteHi,
ReplyDeleteScott is a Oracle database schema with a department and employee table.
nothing special
thanks Edwin