Spring Rich Clients with JMX and 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:
| Attachment | Size |
|---|---|
| figure-1.png | 75.51 KB |
| figure-3.png | 79.4 KB |
| figure-4.png | 51.23 KB |
| figure-5.png | 64.41 KB |
| figure-6.png | 98.37 KB |
| figure-2.png | 65.65 KB |
| figure-7.png | 105.27 KB |
| figure-8.png | 68.62 KB |
| figure-9.png | 87.33 KB |
- Login or register to post comments
- 4004 reads
- Flag as offensive
- Email this Story
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)







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