YOUR FEEDBACK
Brian Vicente wrote: Where are listing 3 and listing 4?


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


Managing the Unmanaged
Using JMX To Manage Your Preexisting JAVA Applications

Hardly anyone ever thinks about application management frameworks until the application is running in a production environment and the application needs managing. The use of Java and J2EE has allowed business code to be written at lightning fast speed and application management is normally considered only as an afterthought. Without the convenience of an application server to start/stop and change various settings, these applications become monsters to administer and manage. Although JMX is a technology that is most often thought of for J2EE integration, JMX can also be used to quickly provide a management console for any well-designed standalone Java application with only minor refactoring efforts.

Sample Situation

A widget manufacturing company has a custom-built application that controls the rate of widget production. Every 10 seconds a new widget is produced. Most of the time the manufacturing line gets ahead of schedule and the workers sit idly by. Sometimes the line gets behind schedule at which point any onlooker would be reminded of Lucy and Ethel at the candy factory. When workers want to go to lunch, the application that controls everything must be killed. When lunch is over, the application must be manually restarted, a very lengthy process. Overall the production of widgets has become very inefficient.

Seeing these inefficiencies the business managers decide that they would like a way to change the rate at which widgets are produced. When the line is ahead of schedule, they'd like to fill capacity. When the line is slow and widgets are backing up, they would like to slow things down. Instead of killing the application, a system manager would be able to pause the application until everyone gets back from lunch. All of these functions should be administered from a nice management console that won't require too much training.

Charged with updating the system, the IT shop understands that an easy-to-adapt nonintrusive application management framework is needed to make their custom, standalone Java application manageable. The team decides that the JMX framework would be a perfect fit.

What Is JMX?

The specification for Java Management Extensions (JMX) addresses the need for a common framework for managing applications. It provides Java developers a much-needed management architecture, a set of APIs, and several management services that can easily be added to enhance any Java application. Currently JMX is used heavily in the J2EE application server world. As more and more software products become J2EE enabled, JMX is becoming the de facto standard framework for Java-based application management. It's not only J2EE services that can take advantage of JMX but also standalone Java applications that can also benefit. Developing these features may take place either during the initial build of the application, or, as the example presents, as an afterthought.

First let's get an understanding of the JMX architecture. The JMX architecture is three tier and easily integrated into any existing Java application. Figure 1 is adapted from the Sun Specification of JMX. It shows the three tiers and their components. It also shows where your preexisting application resides with respect to the rest of the management framework.

The Instrumentation Layer

In the Instrumentation Layer, the Java developer exposes critical interfaces, objects, and components to the management interface by creating managed beans. JMX uses reflection to understand that an object is manageable, as such a managed bean (or MBean) is a class or interface whose name ends in the text "MBean", or a class that has in its hierarchy a class name ending in MBean (see www.sys-con.com/java/sourcec.cfm for more details). For example, a class Service would be considered an MBean if it was either renamed ServiceMBean or implemented an interface ServiceMBean.

The standard MBean allows each object's members to be exposed based on a certain set of rules.

  • All attributes that have get methods are exposed for viewing in the management console.
  • All attributes that have set methods are exposed for altering in the management console.
  • All other public methods defined in the MBean are exposed as operations.
By creating and registering MBeans the instrumentation layer defines a core set of managed resources to the agent layer.

The Agent Layer
The JMX agent layer provides the magic of using the JMX framework. At the heart of the agent layer resides the MBeanServer. The MBeanServer acts as a repository and registry for all of a JVM's MBeans. The MBeanServer also provides many of JMX's services including event monitoring and timer services. The JMX agent provides the means to hook custom Java code into the JMX framework using the MBeanServer's register() method. The agent layer can be viewed as a black box to the Java developer since your JMX provider already implements the MBeanServer for you. JMX providers include JBoss, WebSphere, and, for this example, the Sun Reference Implementation of JMX. The agent layer uses a set of adapters and connectors that may be custom written or provided by your JMX implementation. These adapters and connectors act as a bridge between the agent layer and the distributed services layer.

The Distributed Services Layer
The last layer, Distributed Services Layer, provides the interfaces and components that remote tools use to interface with agents. Using the hooks provided by this layer, your JMX instrumented application can be accessed through HTTP, RMI, JMS, SNMP, or any other distributed protocol that makes sense to your application. Each JMX provider may supply adapters that make using these hooks even easier. In our example, the Sun Reference Implementation provides a simple, yet elegant, HTTP adapter that displays agent information through a set of HTML screens. (The source code can be downloaded from www.sys-con.com/java/sourcec.cfm.)

What to Do?

Now that we know what JMX is and how it can help us, let's walk through the steps involved in making your existing standalone Java application manageable through a simple HTTP service.

Step 1: Figure out what needs to be managed.
What does management mean to your application?

Figuring out what needs to be done will lay a solid groundwork for understanding the best approach to exposing application functionality. This process will determine what components reside in your instrumentation layer. Our example involves a server that runs when a message arrives in the system and waits with a time interval for another message to come. We want to add the ability to manage the wait time. We would also like to be able to pause and resume this process as well as start and stop the thread from an object perspective. In our example we have a preexisting class named Service. Service has the main method for our application. It spawns a thread that checks the status of the object (started, stopped, etc.). In a loop, the server thread then calls the process() method.

Step 2: Find the application code that will help expose management functions.
Many of the management requirements will be able to be satisfied by simply exposing existing code. New code would be used to expose logic that wasn't exposed before. In our example we would like the Service class to be exposed to a management console. Since the existing Service class has all the methods of interest already implemented, we don't have to implement new application code (see Figure 2).

Step 3: Write proxy MBeans that expose these functions.
Once you know which methods you want to expose to the management console, develop MBean interfaces and make your classes implement them. MBeans are simple to write since they shouldn't contain any of the logic that you want to expose. For our example we create a new interface: ServiceMBean. ServiceMBean defines the methods that we wish to expose. Note that "MBean" in the name is needed. The Service class must also now implement MBeanRegistration's methods. These implemented methods may be empty since we don't want to override any default behavior. Our original class Service should now implement ServiceMBean (see Figure 3).

Step 4: Write the code that will register MBeans with the MBean Server.
MBean objects need to be registered with an MBean Server. Registering them is a straightforward task. For our example, Service acts as the main class. The main method uses code in Listing 1 to create the javax.management.MBeanServer and register our instance of Service to it.

Step 5: Provide the MBean Server to a distributed adapter.
For our example, we use the Sun Reference Implementation of JMX, which provides us with an HTTP adapter and server and is found in jmxtools.jar. Using this adapter is a perfect way to quickly expose your MBean interfaces. The implementation provides an HTML screen that lets you browse and edit your MBeans; it is perfect for quick implementations. Again in our Service class's main method we put the code shown in Listing 2.

Step 6: Manage your application.
Now that you have written and registered all of your MBeans, and instantiated and started your adapter, you are ready to manage your application. In our example we can simply point a browser (Mozilla, IE, or even Lynx) to localhost:8080 and begin to manage our application. The Sun RI provides an intuitive user interface that allows each MBean's attributes to be changed and its methods invoked. Figure 4 shows what comes with the Sun RI.

Conclusion

Using JMX as described can allow developers to quickly extend application components to deliver a simple yet functional management console. The example shown here shows the power of simply adding MBeans and JMX to your standalone application. The HTML adapter that is provided with the JMX Reference Implementation is enough to get a standalone Java application manageable in a short period of time. The JMX world becomes even more powerful when other adapters and distributed services are used. By using these and other features of JMX, more complex and robust solutions are easily contrived.

Resources

  • Perry, J.S. (2002). Java Management Extensions. O'Reilly: www.oreilly.com/catalog/javamngext
  • Sun JMX Specification and reference implementation: http://java.sun.com/products/JavaManagement
  • About Gil Salu
    Gil Salu is an IT architect with IBM Global Services' Boston Center for Business Innovation (www.ibm.com/services/innovation/boston/).

    About Greg DeMelo
    Greg DeMelo is an IT architect with IBM Global Services' Boston Center for Business Innovation (www.ibm.com/services/innovation/boston/).

    LATEST JAVA STORIES & POSTS
    Unit testing is hard. There I said it. Although I have been developing software for the past 18 years I still find that putting my applications through their paces via unit testing is difficult. I have learned the lesson (I'm sure like many of you) the hard way. Unit testing is p...
    Continuent has announced support and enhancements to MySQL Server 5.1.30 GA release, the 5.1 production version of the open source database. MySQL 5.1.30 is recommended for use on production systems by the MySQL build team at Sun Microsystems. Continuent Tungsten provides advance...
    As a software journalist, there are times when certain vendors will shut the door on reporting opportunities that might represent too much of an "inside view" of their technology or their organization. I've been to more developer events than I can remember where I've been handed ...
    Active Endpoints has announced the general availability of ActiveVOS 6.0.2, in response to ever increasing demands for improved process performance and efficiencies. ActiveVOS is an all-in-one, 100% standards-based orchestration and business process management system (BPM) that p...
    Just because the web has been open so far doesn't mean that it will stay that way. Flash and Silverlight, arguably the two market-leading technology toolkits for rich media applications are not open. Make no mistake - Microsoft and Adobe aim to have their proprietary plug-ins, ak...
    Doing network I/O on the user interface (UI) thread is bad. Most developers know that and can tell you why; unfortunately, it’s still done. At this year's JavaOne, one of the keynote JavaFX demos bombed because the network was slow, something that would be forgivable had the en...
    SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
    SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
    Click to Add our RSS Feeds to the Service of Your Choice:
    Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
    myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
    Publish Your Article! Please send it to editorial(at)sys-con.com!

    Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


    SYS-CON FEATURED WHITEPAPERS

    SPONSORED BY INFRAGISTICS
    In every field of design one of the first things students do is learn from the work of others. They ...
    There are many forces that influence technological evolution. After a decade of building enterprise ...
    2008 is going to be an important year for Rich Internet Applications. Most organizations are deliver...
    The OpenAjax Alliance is developing an Ajax industry wishlist for future browsers, using a dedicated...
    Infragistics announced the availability of two Community Technology Preview (CTP) User Interface (UI...
    The YUI development team has released version 2.5.2; you can download the new release from SourceFor...
    ADS BY GOOGLE