Most of the time developers likes manage their application configuration on separate file, which contains name value pair. In my current project one of my team member also implements such a configuration through Spring. Put the file on Jboss %jboss-as%/server/xyz/conf folder which will picked by the spring on the startup of the server. I have asked him, what should i do to change the value of the configuration. He replied, you have to change the file and restart the server or start and stop the application ))). Certainly we face a lof of times these type of use cases. I told him about JMX and decided to make some quick change on code. Todays post is about JMX. For more information about JMX use cases, check the following links JMX use cases.
At first we will create one interface and his implements, which will be our resource to manage by JMX. here is the fragment code of the classes:
public interface ConfigMBean { public void setURL(String url); public String getURL(); public void setUserName(String useName); public String getUserName(); public void setPassword(String password); //public String getPassword(); public void setDownloadTimeout(long timeout); public long getDownloadTimeout(); }Implemention of the interface goes here:
public class ConfigImpl implements ConfigMBean { private String url; private String userName; private String password; private long downloadTimeout; @Override public void setURL(String url) { this.url = url; } @Override public String getURL() { return url; } @Override public void setUserName(String useName) { this.userName = useName; } @Override public String getUserName() { return userName; } @Override public void setPassword(String password) { this.password = password; } @Override public void setDownloadTimeout(long timeout) { this.downloadTimeout = timeout; } @Override public long getDownloadTimeout() { return downloadTimeout; } }Now it's time to configure spring context to initialize our beans.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <import resource="classpath:META-INF/cxf/osgi/cxf-extension-osgi.xml"/> <context:component-scan base-package="com.blu"/> <bean name="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="locations"> <list> <value>classpath:local-serviceregistry.properties</value> <value>classpath*:serviceregistry.properties</value> </list> </property> </bean> <bean id="ConfigJMX" class="xyz.config.ConfigImpl"> <property name="URL" value="${BasePath}"/> <property name="userName" value="${Login}"/> <property name="password" value="${Password}"/> <property name="downloadTimeout" value="${DownloadTimeout}"/> </bean> <!-- JMX config--> <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false"> <property name="beans"> <map> <entry key="xyz:name=JConfig" value-ref="ConfigJMX"/> </map> </property> </bean> </beans>Code snippet is self explainable, however, bean named placeholderConfig used for reading name value pair from the file system when application starts. Configuration file is as follows:
BasePath=http://172.18.5.78:8080/jui Login=jboss Password= DownloadTimeout=1000Through spring Mbean exporter we exposes our pojo ConfigImpl to MBean server, and that's it. We can also use annotation @ManagedResource to expose pojo as a Mbean and auto wire by annotation @Component. Now we can deploy our application on Jboss and our Mbean is ready to use. We can use Jconsole to use our Mbean, which is shipped with JDK 1.5 and above. In order to use Jconsole to display Jboss Mbeans we have to add some properties on server boot time. Add these followings params to %JBOSS_HOME%/bin/run.conf.sh
# Enable the jconsole agent locally JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote" # Tell JBossAS to use the platform MBean server JAVA_OPTS="$JAVA_OPTS -Djboss.platform.mbeanserver" # Make the platform MBean server able to work with JBossAS MBeans JAVA_OPTS="$JAVA_OPTS -Djavax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl"see the following link for more information. Now you can run Jconsole and connect to your Jboss application server locally or remotely and set URL or user name, see the image below. You can also use Jboss JMX Console to edit Mbean properties. For more information to configure JMX through spring see here. Happy coding)))
Comments