Skip to main content

Manage application configuration with JMX in JBOSS application server

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=1000
Through 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

Popular posts from this blog

8 things every developer should know about the Apache Ignite caching

Any technology, no matter how advanced it is, will not be able to solve your problems if you implement it improperly. Caching, precisely when it comes to the use of a distributed caching, can only accelerate your application with the proper use and configurations of it. From this point of view, Apache Ignite is no different, and there are a few steps to consider before using it in the production environment. In this article, we describe various technics that can help you to plan and adequately use of Apache Ignite as cutting-edge caching technology. Do proper capacity planning before using Ignite cluster. Do paperwork for understanding the size of the cache, number of CPUs or how many JVMs will be required. Let’s assume that you are using Hibernate as an ORM in 10 application servers and wish to use Ignite as an L2 cache. Calculate the total memory usages and the number of Ignite nodes you have to need for maintaining your SLA. An incorrect number of the Ignite nodes can become a b...

Quick start with In memory Data Grid, Apache Ignite

UP1: For complete quick start guide, see also the sample chapter of the book "High performance in-memory computing with Apache Ignite" here . Even you can find the sample examples from the GitHub repository . IMDG or In memory data grid is not an in-memory relational database, an NoSQL database or a relational database. It is a different breed of software datastore. The data model is distributed across many servers in a single location or across multiple locations. This distribution is known as a data fabric. This distributed model is known as a ‘shared nothing’ architecture. IMDG has following characteristics: All servers can be active in each site. All data is stored in the RAM of the servers. Servers can be added or removed non-disruptively, to increase the amount of RAM available. The data model is non-relational and is object-based.  Distributed applications written on the platform independent language. The data fabric is resilient, allowing non-disruptive au...

Analyse with ANT - a sonar way

After the Javaone conference in Moscow, i have found some free hours to play with Sonar . Here is a quick steps to start analyzing with ANT projects. Sonar provides Analyze with ANT document to play around with ANT, i have just modify some parts. Here is it. 1) Download the Sonar Ant Task and put it in your ${ANT_HOME}/lib directory 2) Modify your ANT build.xml as follows: <?xml version = '1.0' encoding = 'windows-1251'?> <project name="abc" default="build" basedir="."> <!-- Define the Sonar task if this hasn't been done in a common script --> <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml"> <classpath path="E:\java\ant\1.8\apache-ant-1.8.0\lib" /> </taskdef> <!-- Out-of-the-box those parameters are optional --> <property name="sonar.jdbc.url" value="jdbc:oracle:thin:@xyz/sirius.xyz" /> <property na...