vendredi 23 mars 2012

Supervision in JAVA applications / Using JMX



I will present a few tricks I'm using systematically to create Java applications easily supervisable ( the  best practices I'm following ). My first POST, on that subject, will introduce how I'm using JMX to deliver peaces of software that will be correctly managed and monitored by the team of technicians in charge of it.


1 - JMX (Java Management Extensions) and MBeans objects


This is a standard technology defining architecture and design patterns for application management and monitoring. Using this technology, the attributs exposed and the operations offered are provided by objects called "Managed Mbean" ( MBean ). See this page for an overview of this technology.


2 - Using MBeans to make the application configurable 'in live'


Changing the configuration of an application without restarting it is sometimes very valuable. MBeans are made for that. Using these objects, you will be able to change the behavior of your soft without effort. So, since the beginning of a new project, I tried to put as much of the configuration properties as possible in these objects. Doing that, your application will be dynamically managed without stopping the production.


3 - Why using this standard


If you choose this technology, you will not have to construct any GUI ( graphical user interface ) to make these objects accessible to your users. Many tools are already capable to connect to a running JVM and to interact with its registered MBeans. For example, the jconsole, provided with every JDK, display all the registered MBean and allow the user to modify the properties and to call the operations of these MBeans.


4 - Persisting the MBean in an XML file


All the MBean I create are saving there configuration in an XML file, so that, after restarting the application, the changes applied will be restored. To make the code of the MBean very simple, I'm using JAXB to marshall all the properties in the target XML File. Let's take an example :
@XmlRootElement(name="test")
public class Test implements TestMBean {

    @XmlElement(name="count")
    @Override
    public Integer getCount() {
        return(this.count);
    }
    @Override
    public void setCount(Integer count) {
        this.count=count;
    }

    /**
     * persist in the XML File
     */
    protected synchronized void save() {
        try {
            Marshaller ma = context.createMarshaller();
            ma.marshal(this, new File(configFileName));
        } catch(Exception e) {
            System.err.println("unable to save file"+e.getMessage());
        }
    }
    
    /**
     * construct the MBean from the XML file
     * @param xmlFileName
     * @return
     */
    protected static Test makeFromXMLFile(String xmlFileName) {
        File configFile = new File(xmlFileName);
        Test result = null ;
        if (configFile.exists()) {
            try {
                Unmarshaller un = context.createUnmarshaller();
                result=(Test) un.unmarshal(configFile);
            } catch (Exception e) {
                throw new RuntimeException("unable to read config file",e);
            }
        }
        if (result==null) {
            result = new Test();
        }
        result.configFileName = xmlFileName ;
        result.save();
        return(result);
    }

    private Integer count ;
    private String configFileName;
    
    private final static JAXBContext context  ;

    static {
        try {
            context = JAXBContext.newInstance(Test.class);
        } catch (JAXBException e) {
            throw new RuntimeException("Programmatic error :",e);
        }
    }
}


As you can see in this exemple, you just need to add some very simple annotations on the bean's properties you want to be saved in the XML file : that's what we did on the Count MBean attribut.

5 - Conclusion


Don't hesitate to provide many MBeans in your software : with a very little cost, you will be able to deliver an application configurable in live, easily monitored and managed.

Aucun commentaire:

Enregistrer un commentaire