Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos
h3. The problem The Spring Framework is a lightweight container based on Dependency Injection pattern and AOP. The base idea behind Spring is to simplify Java/J2EE development. Many of the ideas of the Spring was adopted by EJB3 specification. Full documentation could be found on http://www.springframework.org (http://www.springframework.org). This blog describes how to integrate Spring with SAP WAS JMX Server. The requirement is to publish some of the beans from Spring Application Context as MBeans to JMX server and to enable management from Visual Administrator or JMX console. The blog could be useful for ISV that ports existing Java applications developed with the help of the Spring Framework to SAP WAS and also for the companies that use the Spring Framework as the basis for J2EE Application and want to use existing infrastructure of SAP WAS in order to manage application. h3. Solutions There are two ways. 1. It is possible to register any valid MBean on existing JMX Server. But this MBean will not be available inside Visual Administrator. The only way to manage it is to use a third party JMX console or to write a custom Java code. This approach requires less coding. 2. In order to display MBeans in the Visual Administrator it is necessary to use com.sap.engine.services.basicadmin.mbean.BroadcastingStandardMBeanWrapper from basicadmin.jar. In this case we have to instantiate our MBean manually. I am going to describe both. Lest suppose that we have an application where beans that implements business and persistence logic are described in the Spring XML config. We need to register all beans with business logic as MBeans on the JMX server. h3. Approach 1 *1.* Implement org.springframework.jmx.export.naming.KeyNamingStrategy interface in order to create valid MBean name with help of com.sap.jmx.ObjectNameFactory . public class SAPKeyNamingStrategy extends KeyNamingStrategy { private String type; public void setType(String type) { this.type = type; } public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException { return ObjectNameFactory.getNameForServerChildPerNode(type, beanKey, null, null); } } *2.* Write FactoryBean for MBean server. public class SAPMBeanServerFactoryBean implements FactoryBean, InitializingBean { private static Log logger = LogFactory.getLog(SAPMBeanServerFactoryBean.class); private static final String JMX = "jmx"; private MBeanServer mbeanServer; private String username; private String password; private String jndiName = JMX; public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setJndiName(String jndiName) { this.jndiName = jndiName; } public void afterPropertiesSet() throws MBeanServerNotFoundException { logger.debug("Attempt to lookup the MBean server"); try { this.mbeanServer = lookup(); } catch (ClassCastException cce) { logger.error("Could not cast returned object from JNDI ["+jndiName+"] to " + MBeanServer.class.getName(), cce); throw cce; } catch (Exception ex) { logger.error("Could not locate JMX server", ex); throw new MBeanServerNotFoundException("Could not locate JMX server from JNDI ["+jndiName+"]", ex); } logger.debug("The MBean server has been retrieved. Default Domain=" + mbeanServer.getDefaultDomain()); } private MBeanServer lookup() throws NamingException { InitialContext ctx = new InitialContext(getJndiProperties()); Object objMBeanServer = ctx.lookup(this.jndiName); return (MBeanServer) objMBeanServer; } private Hashtable getJndiProperties() { Hashtable properties = new Hashtable(); properties.put(Context.SECURITY_PRINCIPAL, this.username); properties.put(Context.SECURITY_CREDENTIALS, this.password); return properties; } public Object getObject() { return this.mbeanServer; } public Class getObjectType() { return (this.mbeanServer != null ? this.mbeanServer.getClass() : MBeanServer.class); } public boolean isSingleton() { return true; } }   *3.* Add the following lines in Spring XML config. *BEAN_NAME* – JMX MBean name. *BEAN_ID* – spring bean id. Bean with that id must exists in XML config. *registrationBehavior* - Specify what action should be taken when attempting to register an MBean. Possible values are REGISTRATION_FAIL_ON_EXISTING (0), REGISTRATION_IGNORE_EXISTING (1), REGISTRATION_REPLACE_EXISTING (2) It is necessary to pass credentials for “sapMBeanServer” in order to access JMX server. User must belong to Administrator role. All beans will be registered in initialization time of Spring context. h3. Approach 2 In this case SAPMBeanExporter uses proxy instances (*PROXY_ID*) instead of a real bean and wrapps it by BroadcastingStandardMBeanWrapper. Each proxy knows which interfaces it intercept (property *proxyInterface*) and real instance of a bean (*BEAN_ID*).