JavaCompute node exception handling
The evaluate()
method throws an MbException
.
If your code throws other classes of checked exceptions, they must be caught and rethrown as
MbException
. Typically, you use the
MbUserException
class, as shown in
the example code. MbUserException
is a subclass of
MbException
, and has a public constructor. IBM® Integration Bus utility functions can throw other subclasses of
MbException that have private constructors.
For information about what happens after the evaluate()
method is
processed, see Default error handling.
About this task
Exceptions can occur during message
flow processing (during the evaluate()
method) and
during the onInitialize()
method. If you have implemented
the onInitialize()
method, and you detect an unrecoverable
error in the node configuration, an exception of class MBException
is
issued and the flow fails to initialize. Either your flow fails to
deploy, and error message BIP4157 is issued, or the
flow does not start and does not appear in the list of running flows.
Do
not run code in an onInitialize()
method that depends
on another external resource that might not be there because the integration node will not retry
calls to that method. If you need to initialize an external connection,
for example, initialize it during the first call to the evaluate()
method.
If the initialization does not work, you can throw an exception that will be handled
like any other flow exception.
The following example code shows
a simple implementation of a JavaCompute node that routes
a CSV message to the out
or alt
terminals, based
on a header field that contains a positive or negative number. If
the text in the field is not numeric, a NumberFormatException
is thrown.
The example code shows how this exception can be caught and thrown
as a MbUserException
so that the handling of MbException
built
into IBM Integration Bus from JavaCompute nodes can be used.
public void evaluate(MbMessageAssembly assembly) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
MbOutputTerminal alt = getOutputTerminal("alternate");
MbMessage message = assembly.getMessage();
int routeTo = 0;
try {
// ----------------------------------------------------------
// Add user code below
String routingValue = (String)message.evaluateXPath("string(/csv/header/route_to)");
routeTo = Integer.parseInt(routingValue);
// End of user code
// ----------------------------------------------------------
} catch (java.lang.NumberFormatException e ) {
// Example Exception handling
MbUserException mbue = new MbUserException(this, "evaluate()", "","", e.toString(), null);
throw mbue;
}
// The following should only be changed
// if not propagating message to the 'out' terminal
if ( routeTo > 0 )
out.propagate(assembly);
else
alt.propagate(assembly);
}