Feb 16 2009

Exposing MBeans with Spring

Published by Gilles at 9:21 pm under J2EE, Java, Spring

Websphere specifics

logo_ibm_websphere.gif
In the above example, annotation has a naming convention for objectName. It is "domain:key=value". Later on, class "org.springframework..support.MBeanRegistrationSupport will fetch the domain and use it to register the MBean to MBeanServer. The domain can be used to group the registered MBeans.

IBM Websphere handles MBeans a little different than most EE servers. Whenever one registers an MBean within Websphere the container will 'extend' its name to include the actual deployment location of the bean. This is required to avoid conflicts when registering beans in clusters and or cells of servers.

This behavior complicates things when one needs to unregister an MBean. the actual name needed to unregister the MBean is unknown to the application as it has been altered by Websphere. Unregistering and registering beans is required when one restarts an application without restarting the Websphere server it runs on. If this action is not performed new performance data will not be exposed.

The best approach is to implement org.springframework..export.naming.ObjectNamingStrategy, which is an interface that encapsulates the creation of ObjectName instances and is used by the MBeanExporter to obtain ObjectNames when registering beans. An example is available on the Framework forum. You can add the ObjectNamingStrategy instance to the bean that you register. This will ensure that the MBean is properly de-registered when the application is uninstalled.

Get ahold of WebSphere's admin service

<bean id="adminService" class="com.ibm.websphere.management.AdminServiceFactory" factory-method="getAdminService"/>

Get the bits from the admin service needed for the object name.

XML:
  1. <bean id="adminService.defaultDomain" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
  2. <bean id="adminService.cellName" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
  3. <bean id="adminService.nodeName" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
  4. <bean id="adminService.processName" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>

Create the WebSphere object naming strategy

XML:
  1. <bean id="websphereNamingStrategy" class="WebsphereNamingStrategy">
  2. <property name="domainName" ref="adminService.defaultDomain"/>
  3. <property name="cellName" ref="adminService.cellName"/>
  4. <property name="nodeName" ref="adminService.nodeName"/>
  5. <property name="processName" ref="adminService.processName"/>
  6. </bean>

Export someBean with using the WebSphere object naming strategy.

XML:
  1. <bean id="beanExporter" class="org.springframework..export.MBeanExporter">
  2. <property name="beans">
  3. <map>
  4. <entry key="someBean" value-ref="someBean"/>
  5. </map>
  6. </property>
  7. <property name="namingStrategy" ref="websphereNamingStrategy"/>
  8. </bean>

WebSphere Object Naming Strategy. Creates an object name with the WebSphere required bits.

JAVA:
  1. public class WebsphereNamingStrategy implements ObjectNamingStrategy
  2. {
  3. private String domainName;
  4. private String cellName;
  5. private String nodeName;
  6. private String processName;
  7.  
  8. public ObjectName getObjectName(Object object, String name)
  9. throws MalformedObjectNameException
  10. {
  11.     StringBuffer objectName = new StringBuffer();
  12.     objectName.append(domainName);
  13.     objectName.append(":cell=");
  14.     objectName.append(cellName);
  15.     objectName.append(",name=");
  16.     objectName.append(name);
  17.     objectName.append(",type=");
  18.     objectName.append(ClassUtils.getShortName(object.getClass()));
  19.     objectName.append(",node=");
  20.     objectName.append(nodeName);
  21.     objectName.append(",process=");
  22.     objectName.append(processName);
  23.    
  24.     return ObjectNameManager.getInstance(objectName.toString());
  25. }
  26.  
  27. public String getCellName(){
  28.     return cellName;
  29. }
  30.  
  31. public void setCellName(String cellName){
  32.     this.cellName = cellName;
  33. }
  34.  
  35. public String getDomainName(){
  36.     return domainName;
  37. }
  38.  
  39. public void setDomainName(String domainName){
  40.     this.domainName = domainName;
  41. }
  42. public String getNodeName(){
  43.     return nodeName;
  44. }
  45.  
  46. public void setNodeName(String nodeName){
  47.     this.nodeName = nodeName;
  48. }
  49.  
  50. public String getProcessName(){
  51.     return processName;
  52. }
  53.  
  54. public void setProcessName(String processName){
  55.     this.processName = processName;
  56. }
  57. }

Pages: 1 2 3 4 5

5 Responses to “Exposing MBeans with Spring”

  1. KPon 25 Feb 2009 at 10:27 pm

    I was trying to set up a JMX managed standalone Spring application and the article helped a lot. Thanks!!!

  2. markon 06 Nov 2009 at 11:49 am

    Hello, I found your post about the Spring mbeans in the WebSphere 6.1. I am new to Spring and JMX and looking for a step by step guide to register and calling by wsadmin some Spring’s mbeans provided by the app developer. I read your post and do understand the websphere naming strategy but where do I need to place this class WebsphereNamingStrategy ?

    br
    Mark

  3. Gilleson 06 Nov 2009 at 11:59 am

    Hello Mark,
    You can place your WebsphereNamingStrategy class where you want. For my part, I place it in an “util” package…
    Cheers,
    Gilles

  4. markon 06 Nov 2009 at 12:24 pm

    Hello Gilles,
    Thanks a lot for a quick response. I just have one question if there is any possibility to enable the Spring’s mbean in WAS 6.1 without adding any new code to the project ? I do not have the source code and do not want to extend the EAR file.
    br
    Mark

  5. hoschilordon 26 Aug 2010 at 8:23 am

    excellent info, thanks!
    was trying to get this running on websphere and also figured out that websphere changes the objectnames of the mbeans..your sources saved a lot of time for me yeehaa :-)

Trackback URI | Comments RSS

Leave a Reply