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

Tip: SQL client for Apache Ignite cache

A new SQL client configuration described in  The Apache Ignite book . If it got you interested, check out the rest of the book for more helpful information. Apache Ignite provides SQL queries execution on the caches, SQL syntax is an ANSI-99 compliant. Therefore, you can execute SQL queries against any caches from any SQL client which supports JDBC thin client. This section is for those, who feels comfortable with SQL rather than execute a bunch of code to retrieve data from the cache. Apache Ignite out of the box shipped with JDBC driver that allows you to connect to Ignite caches and retrieve distributed data from the cache using standard SQL queries. Rest of the section of this chapter will describe how to connect SQL IDE (Integrated Development Environment) to Ignite cache and executes some SQL queries to play with the data. SQL IDE or SQL editor can simplify the development process and allow you to get productive much quicker. Most database vendors have their own fron...

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...

Load balancing and fail over with scheduler

Every programmer at least develop one Scheduler or Job in their life time of programming. Nowadays writing or developing scheduler to get you job done is very simple, but when you are thinking about high availability or load balancing your scheduler or job it getting some tricky. Even more when you have a few instance of your scheduler but only one can be run at a time also need some tricks to done. A long time ago i used some data base table lock to achieved such a functionality as leader election. Around 2010 when Zookeeper comes into play, i always preferred to use Zookeeper to bring high availability and scalability. For using Zookeeper you have to need Zookeeper cluster with minimum 3 nodes and maintain the cluster. Our new customer denied to use such a open source product in their environment and i was definitely need to find something alternative. Definitely Quartz was the next choose. Quartz makes developing scheduler easy and simple. Quartz clustering feature brings the HA and...