3

I'm interested in using JMX to monitor/configure a simple Java Client/Server application. For example, we would capture any network exceptions that occur in a Java program.

Can MBeans be extended in this way? Or are they limited to more concrete get & set functions?

So far, I've looked in Notifications and Monitor MBeans

Thanks

Caffeinated
  • 11,982
  • 40
  • 122
  • 216
  • I am curios why you have so low reputation? and you have answered a lot of questions? – Maytham Fahmi Mar 19 '16 at 22:48
  • 1
    You can capture any exceptions using JVMTI, see [this answer](http://stackoverflow.com/questions/23561555/java-exceptions-counter-on-jvm-hotspot/23567931#23567931). But what are you going to do with them next? – apangin Mar 20 '16 at 19:26
  • @apangin - that linked answer is for C code , correct? The eventual goal is to implement some kind of fail-safe activity or monitoring. Or to at least monitor network exceptions with JMX , not sure what's next though – Caffeinated Mar 20 '16 at 22:01
  • This is JVMTI agent (written in C) for Java code. JMX will not magically intercept the exceptions for you - you'll have to implement MXBeans that provide the desired information yourself. – apangin Mar 20 '16 at 22:37

1 Answers1

2

Well I would say that it's definitely doable. I was using JMX in an Apache Wicket application earlier with custom MBeans. Anyway MBeans is just a wrapper around some logic in your server application. So you can take the data directly from your application.

If you want to take an example how is this done in a working application you might want to checkout this:

https://github.com/apache/wicket/blob/master/wicket-jmx/src/main/java/org/apache/wicket/jmx/wrapper/MarkupSettings.java

The class basically holds a reference to the application and asks for data directly form the server app.

When the server starts up, then it registers all the MBeans through an initializer class: https://github.com/apache/wicket/blob/master/wicket-jmx/src/main/java/org/apache/wicket/jmx/Initializer.java

Then every time when you take a look in your MBean server you will see the latest up-to-date information coming directly from the app.

There are some caveats though. One caveat is that Java in general doesn't provide any good abstraction to capture all Exceptions of a given type coming from any source of the application. You can register your catch-all exception handler but as far as I can remember it doesn't work perfectly.

What I was doing when I had to do something like this, I was using AspectJ to register an all catch place to handle exceptions. I was using compile time weaving to reduce the performance implication but I am not sure how much does it affect the overall performance (if it affects at all).

¯\_(ツ)_/¯

The other caveat is that JMX connections are usually difficult to set up in an enterprise environment. If you have to log-in through two hops just to arrive to the production servers because there are firewalls everywhere than your monitoring connection will definitely fail and you need to keep buying beer to your sysadmin and convince your manager that this is not imposing any security risk. :)

There is one thing though. You say

to monitor/configure a simple Java Client/Server application

You want to configure / monitor the clients as well? I've never done that. I am not sure that's even possible.

Alma Alma
  • 1,641
  • 2
  • 16
  • 19
  • Thanks ! I'll look into AspectJ and the suggestions. I guess I can simply monitor Server alone, since Client is difficult/unrealistic to do – Caffeinated Mar 20 '16 at 22:02