Retrieve deployed application information using JMX

Every now and then you get to do things you’ve never done before, which gets you a little bit out of your comfort zone. In the past I wasn’t eager going out of my comfort zone, but now it makes me enjoy my job more and it presents some nice challenges.
At my client, I got a request that seems quite easy to solve, but I didn’t have much experience with the solution.

They have a portal that links to all applications a specific user has access to. The general application and access information is fetched from a database.
Now the user wants to be able to see the deployed version of each application. Most applications are deployed on an Oracle WebLogic application server.

You can interrogate the state of the application server through JMX, Java Management Extensions. Through standards-based interfaces that the application server provides, which are called MBeans, you can monitor the application server, the configuration of a domain and the available services and applications. By calling some of the the MBean’s methods, you can also change its configuration or deploy applications.

The WebLogic Server MBean Data Model is a hierarchical model. It is structured according to the XML document structure that is used for the server’s configuration. You can navigate this MBean hierarchy by getting attributes of a specific MBean. Each MBean is defined by its ObjectName. Without going into too much detail, the ObjectName is the name with which an MBean is registered in the MBean server. You can find more information about ObjectNames on this link: WebLogic Server MBean Object Names.

I used specific MBeans to retrieve and get detailed information of all deployed applications in the WebLogic Server. I’ll show you the code.

...
private static final String RUNTIME_MBEAN_SERVER_JNDI_NAME 
       = "java:comp/env/jmx/runtime";
...
private static MBeanServer getMBeanServer() {
    MBeanServer mBeanServer = null;

    try {
        InitialContext initialContext = new InitialContext();
        mBeanServer = 
               (MBeanServer) initialContext.lookup(RUNTIME_MBEAN_SERVER_JNDI_NAME);
    } catch (NamingException e) {
        LOGGER.error("Error connecting to the MBean server", e);
    }

    return mBeanServer;
}

This small piece of code above fetches the MBean server using the JNDI name of the WebLogic’s Runtime MBean Server. It’s pretty straightforward.

public static Map<String, String> getDeployedApplications() {
    Map<String, String> deployedApplications = 
           new HashMap<String, String>();

    try {
        MBeanServer mBeanServer = getMBeanServer();
        ObjectName domainConfiguration =
               (ObjectName) mBeanServer.getAttribute(
                      new ObjectName(RuntimeServiceMBean.OBJECT_NAME), 
                      "DomainConfiguration");
        ObjectName[] appDeployments = 
               (ObjectName[]) mBeanServer.getAttribute(
                      domainConfiguration, 
                      "AppDeployments");
        for (ObjectName appDeployment : appDeployments) {
            try {
                Object applicationName = 
                       mBeanServer.getAttribute(
                              appDeployment, 
                              "ApplicationName");
                Object versionIdentifier = 
                       mBeanServer.getAttribute(
                              appDeployment, 
                              "VersionIdentifier");
                if (versionIdentifier != null) {
                    deployedApplications.put(
                           applicationName.toString(), 
                           versionIdentifier.toString());
                }
            } catch (Exception e) {
                LOGGER.error(String.format("Error fetching deployment information for '%s'", 
                       appDeployment), e);
            }
        }
    } catch (Exception e) {
        LOGGER.error("Error fetching deployed applications", e);
    }

    return Collections.unmodifiableMap(deployedApplications);
}

Here I navigate the MBean hierarchy depicted in the The WebLogic Server® MBean Reference to the AppDeploymentMBean. I quote:

This MBean is used to configure all physical package types which can be deployed on a WebLogic domain, for instance, EAR files and standalone Java EE and non-Java EE modules.

On the AppDeploymentMBean reference you see that one of the access points is through the DomainMBean.AppDeployments attribute. Always choose the shortest and easiest path. When you click on the link, you go to the DomainMBean reference. On that page, you see that one of the access points is the RuntimeServiceMBean.DomainConfiguration attribute. The RuntimeServiceMBean can be found directly under the MBeanServer Services. From the AppDeploymentMBean you can then retrieve the ApplicationName and VersionIdentifier attributes of the deployed application.

I hope I could help you with your problem or that you gained some new interesting information. If you have questions or a suggestion, please comment underneath this post.

You can find more information about WebLogic (12.1.2) MBeans on the following links:

Advertisements

About Steve Schols

I am Steve Schols, a senior Java consultant working for several clients in Belgium. I mainly blog about the Java language and relevant frameworks and technologies. All statements made here are solely my own and do not represent the opinion of my employer, colleagues or my clients.
This entry was posted in Java, WebLogic and tagged , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Retrieve deployed application information using JMX

  1. Kenny De Smedt says:

    If you ever get the chance, follow a spring integration training. It explains all about exposing services by JMX if you are dealing with just a regular application server other then the expensive Weblogic series.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s