djechlin
djechlin

Reputation: 60788

throw exception through mbean operation

What is the proper exception handling strategy for MBean operations? Some of these operations can fail (e.g. drop a user who isn't connected) and an exception should be reported, but I don't know how my exceptions can percolate up to the JMX layer. My best idea is C-style return codes.

Upvotes: 2

Views: 2611

Answers (1)

Nicholas
Nicholas

Reputation: 16056

That's the intent of javax.management.MBeanException, which is declared as thrown from MBeanServerConnection.invoke. To quote from the JavaDoc:

Represents "user defined" exceptions thrown by MBean methods in the agent. It "wraps" the actual "user defined" exception thrown.

So rather than return cryptic integer codes, (or BigDecimal if the bigger the better ;) ) just trap the "business exception", create a new MBeanException using the business exception and throw it. Provided your business exception is serializable and present in the classpath of the caller, the caller should be able to unwrap the JMX exception and get to the business exception.

If either of those assumptions is not true, just create a message based on the business exception class name and message and create an MBeanException from a new Exception (so you get a stack trace) and your new error message.

Upvotes: 5

Related Questions