Geertjan is a DZone Zone Leader and has posted 457 posts at DZone. You can read more from them at their website. View Full User Profile

Spring Rich Clients with JMX and Java VisualVM

07.18.2008
| 15850 views |
  • submit to reddit
Let's put Java VisualVM to some practical use. We'll take the simple Spring Rich Client demo that is distributed with the Spring Rich Client distro, spruce it up with JMX-aware tags and annotations, and then monitor one of its beans from within Java VisualVM.

 

Note: When it comes to Spring and JMX, chapter 20: JMX from the online Spring documentation cannot be recommended highly enough. Everything that follows below assumes you've read and understood that chapter.

 

We begin by configuring our instance of the Spring MBeanExporter class, where we declare our "Contact" domain object as a JMX Bean and inject some initial values for two of the domain attributes:

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="beans">
<map>
<entry key="bean:name=customerBean" value-ref="customerBean"/>
</map>
</property>
</bean>

<bean id="customerBean" class="domain.Contact">
<property name="firstName" value="Harry"/>
<property name="lastName" value="Potter"/>
</bean>

 

Next, we simply need to annotate our "Contact" domain object:

...
...
...
@ManagedResource(objectName = "app.SimpleApp:type=SpringDemo", description = "Simple Example")
public class Contact {
...
...
...

 

Then run the application while Java VisualVM is running, with its MBeans plugin installed. Open the tab for the application and then look in the MBeans tab. You should see the following:

 

We've exposed all the attributes of our domain object. Let's not do that. First read section 20.3 of the abovementioned reference chapter. Then, to limit the attributes exposed to the JMX server, let's give ourselves some control over our MBean:

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="namingStrategy" ref="namingStrategy"/>
<property name="assembler" ref="assembler"/>
<property name="beans">
<map>
<entry key="bean:name=customerBean" value-ref="customerBean"/>
</map>
</property>
</bean>

<bean id="customerBean" class="domain.Contact">
<property name="firstName" value="Harry"/>
<property name="lastName" value="Potter"/>
</bean>

<bean id="attributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>

<bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
<property name="attributeSource" ref="attributeSource"/>
</bean>

<bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
<property name="attributeSource" ref="attributeSource"/>
</bean>

 

Now we can be specific about which attributes to expose. In the "Contact" domain object, annotate the two methods that get the first name and last name:

@ManagedAttribute()
public String getFirstName() {
return firstName;
}


@ManagedAttribute()
public String getLastName() {
return lastName;
}

 

Note: Section 20.3.2 of the aforementioned reference document gives some interesting info here: "You will also notice that both the age and name properties are annotated with the ManagedAttribute attribute, but in the case of the age property, only the getter is marked. This will cause both of these properties to be included in the management interface as attributes, but the age attribute will be read-only." In other words, if you also annote the setters, then the attributes will be writable in Java VisualVM. Also read the document about Notifications (section 20.7), which could also be implemented in the context of Spring Rich Clients.

Run the application again and Java VisualVM will be more discriminating about the data it exposes:

Had you also annotated one or both of the setters, the font of the attribute would have been blue in Java VisualVM and you would have been able to edit its value:

However, VisualVM hasn't really proved its value thus far. We could simply have used JConsole instead. So, let's now create a VisualVM plugin specifically for our Spring Rich Client application. Once we've installed the plugin into VisualVM, we'll have created functionality specifically for our application so that we'll be able to monitor it visually in an easy and intuitive way from then onwards:

In fact, we won't even need the MBeans plugin anymore. Below, you see that there is no MBeans tab, although the JMX attributes are exposed in the explorer view:

As you can see, we'll let Java VisualVM recognize our Spring demo application as being different to all other applications. A special icon and display name will appear in the explorer view whenever the Spring demo starts up. Also, its attributes will be exposed in the explorer view so that we can immediately see them.

The next section will explain how simple it is to create a plugin that provides the above functionality. But how useful is it really? Well, you could extend it even further and, instead of the attribute name and type, also show the current value, which could be particularly useful in visualizing the application that you're monitoring:

 

AttachmentSize
figure-1.png75.51 KB
figure-3.png79.4 KB
figure-4.png51.23 KB
figure-5.png64.41 KB
figure-6.png98.37 KB
figure-2.png65.65 KB
figure-7.png105.27 KB
figure-8.png68.62 KB
figure-9.png87.33 KB
Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Andrew McVeigh replied on Mon, 2008/07/21 - 4:13am

these articles have been very, very interesting for me. it's the first time i've seen the spring rcp described in a clear manner.

i think the overhead of describing the widget structures via XML seems high. however the flexibility is impressive. it's a shame they are re-jigging the whole framework just as it seems to have reached a level of maturity. i lost interest in it about 2 years ago when no activity seemed to be occurring.

thanks for the series geertjan.

cheers,
Andrew

Geertjan Wielenga replied on Mon, 2008/07/21 - 2:48pm

Andrew, thanks a lot, I really appreciate your comments on this! Get ready for more parts in this series, over the coming weeks.

Jeremiah Holder replied on Wed, 2008/09/17 - 5:48pm

Hi,

 I'm new to Java developent and am drinking from the firehose to get up to speed.  I discoverd the VisualVM tool and wished to use it for performance monitoring of our applications.  However, one key feature that I do not see is the ability to capture the data and export it to .csv, html, or xml.  It appears I can only save the data in the proprietary file type.

Do you have any suggestions for how to customize the API's to allow export of data?

Thanks for any help you might offer.

Regards,

Jeremiah 

Liezel Jane Jandayan replied on Mon, 2011/08/22 - 9:19pm

Programmers can also capture data about the JVM software and save the data to the local system, and then view the data later or share it with others. VisualVM is built on the NetBeans Platform, its architecture is modular and easy to extend with plugins.-Yochanan Berkowitz

Passion Lab replied on Sun, 2012/09/02 - 4:02pm

This will cause both of these properties to be included in the management interface as attributes, but the age attribute will be read-only." In other words, if you also annote the setters, then the attributes will be writable in Java VisualVM. Also read the "best term papers http://www.research-paper.me/" about Notifications (section 20.7), which could also be implemented in the context of Spring Rich Clients.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.