Error handling with Http Outbound Gateway in Spring Integration
Error handling with Http Outbound Gateway in Spring Integration
I'm trying to understand how errors should be handled in Spring Integration. I found documentation about errorChannel
, I tried to use it but the exception is not caught.
errorChannel
Here is my HTTP outbound gateway:
<int-http:outbound-gateway id="orderRequestHttpGateway"
request-channel="orders-channel"
url="${url}"
http-method="POST"
expected-response-type="java.lang.String"
reply-timeout='5000'
reply-channel='pushed-channel'
charset="UTF-8">
</int-http:outbound-gateway>
I don't understand where I should catch exceptions thrown by this component (like a 500 HTTP error)
The route is started by a poller
. I tried adding an error-channel
property:
poller
error-channel
<int:inbound-channel-adapter channel="orders-trigger-channel" expression="''">
<int:poller fixed-delay="${poller}" error-channel="errorChannel"/>
</int:inbound-channel-adapter>
I tried with a custom error channel. I also tried to override the existing errorChannel:
<int:channel id="errorChannel"/>
<int:outbound-channel-adapter id="errorChannelHandler"
ref="errorManager"
method="foo"
channel="errorChannel"/>
So far I keep getting MessageHandlingException
and I can't catch them to deal with them properly.
MessageHandlingException
1 Answer
1
You catch exception only in the caller thread or in the place where you have a try...catch
- there is nothing different in Spring Integration from pure Java: we catch exception whenever we have a try...catch
.
try...catch
try...catch
According your description, the error-channel="errorChannel"
on the <poller>
is the way to go and if you don't have any other thread shifting downstream, such a MessageHandlingException
is really thrown to the poller and wrapped into the ErrorMessage
to be sent to that errorChannel
configured.
error-channel="errorChannel"
<poller>
MessageHandlingException
ErrorMessage
errorChannel
It is a MessageHandlingException
because we deal with the Messaging
in Spring Integration and such an exception carries some context and important information about the flow and failed message. Your 500 HTTP error
is just a cause
in that message. So, when you catch and handle an ErrorMessage
you should take a look into its stack trace for the information to parse.
MessageHandlingException
Messaging
500 HTTP error
cause
ErrorMessage
On the other hand you can narrow a scope for the error handling via ExpressionEvaluatingRequestHandlerAdvice
or RequestHandlerRetryAdvice
: https://docs.spring.io/spring-integration/docs/5.0.6.RELEASE/reference/html/messaging-endpoints-chapter.html#message-handler-advice-chain
ExpressionEvaluatingRequestHandlerAdvice
RequestHandlerRetryAdvice
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.