User Defined Exceptions with JMX
- by Daniel
I have exposed methods for remote management in my application server using JMX by creating an MXBean interface, and a class to implement it. Included in this interface are operations for setting attributes on my server, and for getting the current value of attributes. For example, take the following methods:
public interface WordManagerMXBean {
public void addWord(String word);
public WordsObject getWords();
public void removeWord(String word);
}
The WordsObject is a custom, serializable class used to retrieve data about the state of the server. Then I also have a WordManager class that implements the above interface. I then create a JMX agent to manage my resource:
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName wordManagerName = new ObjectName("com.example:type=WordManager");
mbs.registerMBean(wordManager, wordManagerName);
I have created a client that invokes these methods, and this works as expected. However, I would like to extend this current configuration by adding user defined exceptions that can be sent back to my client. So I would like to change my interface to something like this:
public interface WordManagerMXBean {
public void addWord(String word) throws WordAlreadyExistsException;
public WordsObject getWords();
public void removeWord(String word);
}
My WordAlreadyExistsException looks like this:
public class WordAlreadyExistsException extends Exception implements Serializable {
private static final long serialVersionUID = -9095552123119275304L;
public WordAlreadyExistsException() {
super();
}
}
When I call the addWord() method in my client, I would like to get back a WordAlreadyExistsException if the word already exists. However, when I do this, I get an error like this:
java.rmi.UnmarshalException: Error unmarshaling return; nested exception is:
java.lang.ClassNotFoundException: com.example.WordAlreadyExistsException
The WordAlreadyExistsException, the WordsObject and the WordManagerMXBean interface are all in a single jar file that is available to both the client and the server. If I call the getWords() method, the client has no difficulty handling the WordsObject. However, if a user defined exception, like the one above, is thrown, then the client gives the error shown above. Is it possible to configure JMX to handle this exception correctly in the client?
Following some searching, I noticed that there is an MBeanException class that is used to wrap exceptions. I'm not sure if this wrapping is performed by the agent automatically, or if I'm supposed to do the wrapping myself. I tried both, but in either case I get the same error on the client.
I have also tried this with both checked and unchecked exceptions, again the same error occurs.
One solution to this is to simply pass back the error string inside a generic error, as all of the standard java exceptions work. But I'd prefer to get back the actual exception for processing by the client.
Is it possible to handle user defined exceptions in JMX? If so, any ideas how?