Central Version reporting of deployed artifacts with JEE

While building our RESC.Info solution stack we found ourselves with the challenge to query the build versions of the components installed. After searching the web and consulting various communities we found out there is no ‘standard’ way of solving this.

What we actually wanted is a way to query the installed version of the various modules we deployed to the Java Container without any predefine knowledge which modules are actually installed. We did not want to rely on any platform/container specific solution to solve this.

The approach taken

Java comes standard with JMX, Java Management Extension, it would be preferable to utilize such a infrastructure instead of coming up with our own.
So we decided the technical approach to take would be JMX, now we need to define what we wanted to expose, we decided to stay very close our original goal, extract version information from the installed artifacts programmatically.

We identified 4 main attributes:

  • Name
  • Full name
  • Artifact
  • Version

Since we use Maven for our build process ideally this information would come from the build process itself, there are tons of references on the web on how to do this. We decided to use the Maven resource filtering to generate a properties files, in our pom.xml we have:

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>

in src/main/resources we placed a version.properties file which looks like:

version=${project.version}
name=${project.build.finalName}
fullname=${project.name}
artifact=${project.groupId}.${project.artifactId}

During the maven build process the variables are replaced by the actual data from the POM file which we then can reference from the version reporting code.

Drop in version reporting

On of the other requirements was to have ‘drop-in’ version reporting module. You don’t want to code version reporting over and over again since we would have the version in the same place all the time anyway. So we created a Maven artifact we can simply add as a dependency to our project. This leaves only the task create and modify some files in our project and from that moment on everything goes automatically.

Exposing the information

To expose information to JMX the easiest approach is to expose a MBean to the containers MBeanServer. This is a two step approach

  1. create a interface which name ends with MBean.
  2. create the actual implementation of the interface.

Since we are actually interested in some metadata of our artifact we will call our interface MetaDataMBean

package com.example;

/**
 * Interface for Metadata exposure
 */
public interface MetaDataMBean {
    public String getVersion();

    public String getName();

    public String getFullName();

    public String getArtifact(););
}

Next we will do the implementation of our MetaData class

package com.example;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 *
 */
public class MetaData implements MetaDataMBean {
    /*
     */
    @Override
    public String getVersion() {
        try {
            return ResourceBundle.getBundle("version").getString("version");
        } catch (MissingResourceException e) {
            // the version.properties file is not present
            return null;
        }
    }

    /*
     */
    @Override
    public String getName() {
        try {
            return ResourceBundle.getBundle("version").getString("name");
        } catch (MissingResourceException e) {
            // the version.properties file is not present
            return null;
        }
    }

    /*
     */
    @Override
    public String getFullName() {
        try {
            return ResourceBundle.getBundle("version").getString("fullname");
        } catch (MissingResourceException e) {
            // the version.properties file is not present
            return null;
        }
    }

    /*
     */
    @Override
    public String getArtifact() {
        try {
            return ResourceBundle.getBundle("version").getString("artifact");
        } catch (MissingResourceException e) {
            // the version.properties file is not present
            return null;
        }
    }
}

As you can see not to much rocket science here, we simple return the strings we get from our version.properties file which is created during our build process.

Hooking up to JMX

In the web.xml of your application you can define a <listner> tag which will be called as soon as the application is deployed. the <listner> tag requires the name of a class within your war which subclasses javax.servlet.ServletContextListener. When your war is deployed the contextInitialized override you need to define gets called, this is the ideal location to do one time initializations like the version reporting.
We create a Class, LifeCycleListner which extends ServletContextListner, on initilization we create the MBean, and register it with the containers MBeanServer. When the context is destroyed we will deregister it.
As registration name and type we use the full package name and choose a type identifier. This identifier is the actual key by which later you can retrieve the version information of multiple artifacts with a JMX query. In our example we chose for the type name ‘EXVersion’ which is a arbitrary name, but it needs to be the same for all your projects !

The context listner looks like this:

package com.example;

import java.lang.management.ManagementFactory;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 *
 */
public class LifeCycleListner implements ServletContextListener {
    private MetaData metaData;
    private ObjectName beanName;

    /*
     */
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        try {
            mbs.unregisterMBean(beanName);
        } catch (MBeanRegistrationException | InstanceNotFoundException e) {
            e.printStackTrace();
        }
    }

    /*
     */
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

        try {
            this.beanName = new ObjectName(ResourceBundle.getBundle("version")
                    .getString("artifact") + ":type=EXVersion");
            this.metaData = new MetaData();
            mbs.registerMBean(this.metaData, this.beanName);
        } catch (MalformedObjectNameException | InstanceAlreadyExistsException
                | MBeanRegistrationException | NotCompliantMBeanException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MissingResourceException e) {
            // the version.properties file is not present
            System.out.println("version.properties is not present!!");
        }
    }
}

In the context initialized we already use the version.properties file to get the artifact name. This is also a direct test to see if the version.properties file is present at all, if not we don’t even bother registering the MBean.
We save the MBean name to destroy it when the application gets undeployed.

To have this code called on application deployment we need to register a listner in web.xml:

 <listener>
    <listener-class>com.example.LifeCycleListner</listener-class>
</listener>

Packing it up

If you create a standard Maven project which you deploy to your local repo with the above files included you can use it as a dependency on your other projects.

In your project you need to do 3 things.

  1. add the version.properties template in src/main/resources ( sample above )
  2. add the extra lines to the POM.XML ( sample above )
  3. add extra lines to web.xml ( sample above )

Querying the information

So now we get to what it was all about, how do we extract the version information without any predefined knowledge of what is installed ? We use the simple JMX api to query for our EXVersion type, a code snippet looks like this:

MBeanServer thisServer = ManagementFactory.getPlatformMBeanServer();
        try {

            ObjectName query = new ObjectName("*:type=EXVersion");

            for (ObjectName name : thisServer.queryNames(query, null)) {
                out.println("module: " + thisServer.getAttribute(name, "Name")
                        + "tName: "
                        + thisServer.getAttribute(name, "FullName")
                        + "tVersion: "
                        + thisServer.getAttribute(name, "Version"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

This will print out a set of lines with information about ALL the deployed artifacts which expose the EXVersion type bean.

Rounding up

This is very simple approach of creating a centralized version reporting method without to much infrastructure. It is easy enough to simply include in all your projects with a minimum amount of configuration. It does have some limitations, e.g. 2 deployments of the same artifact on different contexts will not work.

For our internal use we did extend the MetaData class to have some extra properties, but those were very specific to our project.

76 thoughts on “Central Version reporting of deployed artifacts with JEE

  1. I’m so happy to read this. This is the type of manual that needs to be given and not the accidental misinformation that is at the other blogs. Appreciate your sharing this best doc.

  2. obviously like your web site but you need to check the spelling on quite a few of your posts. A number of them are rife with spelling issues and I find it very troublesome to tell the reality however I’ll definitely come again again.

  3. I will right away clutch your rss feed as I can not to find your e-mail subscription link or
    newsletter service. Do you have any? Please let me recognise
    so that I may subscribe. Thanks.

  4. I have seen that smart real estate agents all around you are warming up to FSBO Promotion. They are seeing that it’s more than just placing a poster in the front property. It’s really regarding building interactions with these dealers who someday will become customers. So, whenever you give your time and effort to supporting these vendors go it alone — the “Law associated with Reciprocity” kicks in. Great blog post.

  5. I was suggested this blog by means of my cousin. I’m now not certain whether or not this post is
    written via him as nobody else recognise such designated approximately
    my problem. You are wonderful! Thank you!

  6. I will immediately clutch your rss feed as I can not in finding your
    e-mail subscription hyperlink or e-newsletter
    service. Do you have any? Kindly allow me recognise so that
    I may subscribe. Thanks.

  7. Oh my goodness! Awesome article dude! Thank you, However I am experiencing difficulties with your
    RSS. I don’t know why I cannot subscribe to it. Is there anyone else getting similar RSS problems?

    Anyone that knows the solution will you kindly respond?
    Thanx!!

  8. Hello, Neat post. There is a problem with your site in internet explorer,
    may test this? IE still is the marketplace chief and a good component
    to other folks will pass over your wonderful writing because of this problem.

  9. What i do not realize is actually how you are no longer actually a lot more well-liked than you may be now.
    You’re very intelligent. You know therefore considerably relating to this topic, produced me
    in my opinion imagine it from a lot of various angles.
    Its like men and women don’t seem to be interested except
    it’s one thing to do with Girl gaga! Your individual stuffs great.

    All the time handle it up!

  10. I’ve been surfing online more than three hours these days, yet I never
    discovered any fascinating article like yours.
    It’s beautiful value enough for me. In my view, if all site owners and bloggers made just right content material as you did, the net will be a lot more
    useful than ever before.

  11. I have been browsing on-line more than three hours these days, but I never discovered any attention-grabbing article like yours.
    It’s lovely worth sufficient for me. In my opinion, if all site
    owners and bloggers made excellent content as you did, the web will likely be a lot more helpful than ever before.

  12. It’s appropriate time to make some plans for the future and it’s time to be happy.
    I’ve read this post and if I could I want to suggest you some
    interesting things or advice. Perhaps you could write next articles referring to this article.
    I desire to read more things about it!

  13. Hey would you mind letting me know which webhost you’re working with?
    I’ve loaded your blog in 3 completely different browsers and I must say this blog loads a lot faster then most.
    Can you recommend a good internet hosting
    provider at a fair price? Kudos, I appreciate it!

  14. I’ve been browsing online more than 3 hours these days, but
    I never discovered any interesting article like yours.
    It’s lovely value enough for me. Personally, if all webmasters and bloggers
    made excellent content as you did, the web will probably be a lot more useful than ever before.

  15. Hi! I’ve been reading your blog for a while now and finally got the bravery to
    go ahead and give you a shout out from Austin Tx!
    Just wanted to tell you keep up the fantastic work!

  16. you are truly a just right webmaster. The site
    loading velocity is amazing. It seems that you’re doing any unique trick.

    In addition, The contents are masterpiece. you’ve performed a great activity in this matter!

  17. Greetings from Ohio! I’m bored at work so I decided to check out your blog on my iphone during lunch break.
    I enjoy the info you present here and can’t wait to take a look when I get home.
    I’m amazed at how fast your blog loaded on my cell phone ..
    I’m not even using WIFI, just 3G .. Anyways, amazing blog!

  18. I’ll immediately take hold of your rss as I can not in finding
    your email subscription link or newsletter service. Do you have any?
    Kindly allow me recognise in order that I may
    subscribe. Thanks.

  19. Hi, Neat post. There is a problem along
    with your site in internet explorer, would check this? IE nonetheless
    is the marketplace leader and a large element of
    people will pass over your wonderful writing because of this problem.

  20. I have been surfing online greater than 3 hours lately, but I
    never discovered any interesting article like yours. It’s pretty price sufficient for me.
    Personally, if all web owners and bloggers made excellent content
    as you did, the internet will be much more helpful than ever before.

  21. Howdy just wanted to give you a quick heads up. The text
    in your content seem to be running off the screen in Chrome.
    I’m not sure if this is a formatting issue or something to do with internet browser compatibility but I figured I’d post to let
    you know. The design and style look great though! Hope you get the problem
    resolved soon. Cheers

  22. My partner and I absolutely love your blog and find many of your post’s to be what precisely
    I’m looking for. Does one offer guest writers to write content
    for you? I wouldn’t mind writing a post or elaborating on some of the subjects you write related to here.
    Again, awesome web site!

  23. I think this is among the most important information for me.
    And i’m glad reading your article. But should remark on some general things, The web
    site style is perfect, the articles is really excellent : D.
    Good job, cheers

  24. When I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Thanks!

  25. really nice

    This is definitely credible data my friend. you are a remarkably well-proportioned writer . i hunger to helping with you my website as well. command me what do you believe approximately it

    buy youtube views

    Thankyou again for posting such good content. Cheers

Leave a Reply to Michael Sainz Cancel reply

Your email address will not be published. Required fields are marked *