Thursday, January 28, 2010

JMX Portable Extension for CDI

CDI (Contexts and Dependency Injection) is a new standard that makes application development easier by providing type safety dependency injection, scope binding (request, session, application, etc.) and contextual lifecycle management of Java components. It also  exposes a powerful SPI (Service Provider Interface) to extend its functionality through portable extensions.

JMX (Java Management Extensions) provides a consistent, well defined interface to monitor and manage your applications by exposing objects (MBeans) through an MBeanServer. Currently, there are two ways to expose an object as an MBean: you can either provide an interface of your class, in which case, you won’t be able to add textual descriptions for your exposed fields and methods; or you can implement javax.management.DynamicMBean, which is cumbersome and difficult to maintain.

JSR-255, the JMX specification 2.0, is an initiative that will make the writing of MBeans easier through annotations. However, by the time of this writing, the JSR is inactive as stated here.

I’ve decided to write my first own portable extension that will allow you to expose any object automatically or manually through JMX by just adding some simple annotations. You can download the complete code from here.

Let’s take a look at the following example:

When the bean is instantiated by CDI, a dynamic MBean is created and registered on an MBeanServer to expose the attributes and methods annotated with @ManagedAttribute and @ManagedOperation respectively. You can then use any JMX Agent Viewer to monitor and manage your code.

jmx-view 
Figure 1. JBoss AS JMX Console. A view of an MBean.

You don’t have to worry anymore about obtaining an MBeanServer, registering the MBean and unregistering it. This is all done automatically for you. It’s a simple way of instrumenting your code without writing complex and uneven administration interfaces!

Creation and registration of the MBean is done when the bean is instantiated by CDI, not on startup. If you want the MBean to be registered on startup, you will have to annotate your class with the @Singleton and @Startup annotations.

If you don’t want the object to be automatically registered on instantiation but still want to register it yourself later in the code, you can set the autoRegister property of the @MBean to false. To register your object programmatically, you use the MBeanFactory service to create a DynamicMBean instance, and then register it to an MBeanServer. For example:

You can even expose objects that are not annotated with @MBean. In this case, all fields and methods from the class are exposed. Any @ManagedAttribute or @ManagedOperation annotations will be ignored.

CDI Integration

Writing the portable extension was pretty straightforward. First, I created a class org.gescobar.management.cdi.ManagementExtension that extends javax.enterprise.inject.spi.Extension with a method to observe the ProcessInjectionTarget event:

Basically, what we are doing here is replacing the InjectionTarget with a wrapper class that will register and unregister the MBean if the @MBean annotation is present and autoRegister is true

To register the portable extension, I created a file called javax.enterprise.inject.spi.Extension with only one line:

Testing

I used TestNG and Arquillian to write and execute the tests inside a remote JBoss AS 6.0.0.M1 container. One of the tests looks like this:

Arquillian is a JBoss project for running tests on different containers. By the time of this writing, it hasn’t been release but you can download the code from SVN and build it using Maven 2.

What’s next?

It would be nice to support the following:

  • Automatic and manual registration of MBeans for JBoss Seam.
  • Manual registration of MBeans in any J2SE environment.
  • @ManagedAttribute annotation on methods that return information.
  • Name for operation parameters.
  • Notifications using annotations.
  • Message keys from resource bundles for descriptions.

You can check the project on Google Code here. Please, feel free to download it and give it a try!

38 comments:

Matt Corey said...

A simple, but very nice extension... I would definitely encourage you to go for the Notifications support -- if there's a way to streamline the current implementation mechanism for that, it would be a great improvement...

Nice work!

M

Pete said...

Very nice!

Good to see you are using Arquillian. We are trying to get an alpha release out, but are blocked on some good docs - hopefully we'll get this done this week coming, so soon you can depend on it ;-)

exitcondition said...

Great stuff German!

cvasilak said...

excellent!

Keep up the good work!

Dissertation Service said...

thank you very much for this article.
dissertation'>http://www.dissertationswriting.co.uk">dissertation services

Dissertation help said...

Dissertation is a broad document and is very essential to your ultimate scores. If you are at graduate or even at undergraduate stage, you absolutely need proficient help for your dissertation. Through Dissertation Help you find a panel of fastidious and skilled writers who examine and realize all phase of the topic and write a commendable dissertation.

Dissertation Writing said...

JMX Portable Extension for CDI <-- that's what i was looking for
Writing a Dissertation

Jérôme said...

That's the missing link !

Is there a V1.0.0 release planned ?

Anonymous said...

casamentovestido
festavestido
promjurk
partijjurk
bruiloftjurk
bailevestido
noviavestido
partevestido
iltamekko
morsianmekko
puoluemekko
jewelrybigshot
480jc

Ben Kirby said...

Brilliant extension, very useful and clear.

I've been having a problem with the classloader used when invoking the registered bean via a JMX Console in a browser. The autoRegister code (and the example given for registering a bean yourself, after startup) both use mBeanServer.registerMBean(Object object, ObjectName name) - the only public registration option available. This causes the classloader on the bean to be set to null, which means that, when invoking, you only get the classloader of jmx-console.war, which limits the classes available for use by your method.

The solution for this involves substituting mBeanServer.registerMBean(Object object, ObjectName name) for a different method of registration - see http://community.jboss.org/message/616465 for details.

Ben Kirby said...

You'll also currently need to make the fix specified by Jeremy Norris at http://community.jboss.org/thread/148750 before you can invoke any exposed methods with parameters.

Manoj Bharadwaj said...

German, thanks for sharing.

I started exploring this to use it in JBoss7. I was able to use it with one modification.

updated locateMBeanServer method in MBeanServerLocator.java to following:
private MBeanServer locateMBeanServer() {
MBeanServer mBeanServer = null;

ArrayList al = MBeanServerFactory.findMBeanServer(null);
if (al.isEmpty())
mBeanServer = MBeanServerFactory.createMBeanServer();
else
mBeanServer = (MBeanServer) al.get(0);
return mBeanServer;
}

This also removed the dependency on jboss:jboss-jmx:4.0.2 as this is old and updating it to org.jboss.as:jboss-as-jmx broke the earlier code and the above change enabled me to register the MBean.

Template Design said...

This is really good blog.i would like to bookmarked your site.

emily parrr said...

This causes the classloader on the bean to be set to null, which means that, when invoking, argumentative essay structure

Angela said...

Good work.I like your awesome post..!

direct lenders for payday loans

Zain said...

There are certainly loads of details like that to take into consideration. That is a great point to convey up. I supply the ideas above as general inspiration but clearly there are questions like the one you convey up the place a very powerful thing shall be working in honest good faith. I don't know if greatest practices have emerged round issues like that, but I'm positive that your job is clearly identified as a fair game. Anyway, in my language,there aren't much good supply like this.
増毛

Mike said...

Wow, what a nice blog you have, nice english skills. Have bookmarked your site for further reference.
ritalin
garment sourcing
Wanduhren
Wandbilder

keylogger said...

It was sure my pleasure to make one comment on this blog. Have bookmarked this site for further reference. Will also share this information with friends and team workers

earnbangla said...

I just started reading your post and just after few seconds, I found it very interesting. I read your mostly all the posts. Nice posts, thank you so much for sharing the posts with all.
ptc

custom dissertation said...

I have searched JMX Portable Extension for CDI on many search engines.You have written it very well.Thanks.

watch simpsons said...

I do appreciate your post.Thank you for sharing, very useful, will continue to focus on your article!

Anonymous said...

nice one

Miami Chrysler Dealer

Judith Walton said...

You have done a Great job, keep doing more work like this! Thanks...

logo design costs
buy logo online

x431 diagun manual said...

Cheers favorable on the proficient information. Admittedly unprejudiced wen upward! My spouse and i frequently accomplish certainly not expanding in those however, stew over anyone does a new animatedly buddy-buddy despoile along with I’m unquestionable a lot of people suavity your a lesser amount of at just about any rate. lyj

Anonymous said...

I've integrated this in JBoss AS 7.1.1 but when connecting via JConsole I can not display or modify my attributes. The property is marked as "writeable=true" but in the JConsole the value is displayed red with value "Unavailable". What might be the problem here? How can I fix this?

Shani Raza Panjwani said...

good


best cheap all in one printers
best cheap web hosting
best cheap external hard drives

Child Care Center Insurance said...

Wonderful being going to your blog once again, it's been weeks to me.just wanted to say good blog and this article really helped me.

mary tharpe said...

cdi portable extension jmx is a powerful software. very good sahring!
new software download site

Thomas Anderson said...

all new software download free with crack version.

annette cooper said...

JMX portable extension is really nice and very helpful.
crack download manager free download

herin woods said...

JMX portable extension for CDI is a very informative articele. thanks for good sharing.
softwares idea

CRACK SOFTWARE DOWNLOAD said...

I was just searching for it.... Thanks for share it....
PORTABLE | FREE DOWNLOAD

emmajoseph said...

I found 1 bug in this code....
Crack Software Download | Free Download Software

Paul Allen said...

JMX portable extension for CDI is really a good sharing . thanks alot for useful information.
new software download for windows 8

Bruce Wolf said...

this is informative article. thankful for useful information about JMX portable extension for CDI.
Download Free Software

Thomas Anderson said...

That's a really informative article!! Thanks for useful information about JMX portable extension for CDI.
Portable Software

Scarlet Emma said...

I must say this blog indicates the sincerety with other bloggers, It's an amazing blog.I always love to read these kind of blogs.Amazing discussion on software technology. Go ahead and update us through these type of posts.
Free Software Download Full Version

Anna said...

Great and Useful Article.

J2EE Training

Java EE course

Java EE training

J2EE training in chennai

Java J2EE Training Institutes in Chennai

Java J2EE Training in Chennai

Java EE training

Java Interview Questions

Post a Comment