Skip to content

Commit b7e928b

Browse files
artembilangaryrussell
authored andcommitted
GH-3024 Move Error Handling Docs to the top level
Fixes #3024 To avoid confusing about `errorChannel` header behavior and make it more clear how to handle errors in Spring Integration flows, it would be better to present an `Error Handling` chapter on the top level. So, now it is a first chapter of the `Appendices` section
1 parent a835a63 commit b7e928b

File tree

8 files changed

+80
-79
lines changed

8 files changed

+80
-79
lines changed

src/reference/asciidoc/changes-4.3-5.0.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ See <<./endpoint.adoc#content-type-conversion,Content Type Conversion>> for more
4545

4646
We added `ErrorMessagePublisher` and the `ErrorMessageStrategy` for creating `ErrorMessage` instances.
4747

48-
See <<./configuration.adoc#namespace-errorhandler,Error Handling>> for more information.
48+
See <<./error-handling.adoc#error-handling,Error Handling>> for more information.
4949

5050
===== JDBC Metadata Store
5151

src/reference/asciidoc/channel.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ If namespace support is enabled, two special channels are defined within the app
951951
The 'nullChannel' acts like `/dev/null`, logging any message sent to it at the `DEBUG` level and returning immediately.
952952
Any time you face channel resolution errors for a reply that you do not care about, you can set the affected component's `output-channel` attribute to 'nullChannel' (the name, 'nullChannel', is reserved within the application context).
953953
The 'errorChannel' is used internally for sending error messages and may be overridden with a custom configuration.
954-
This is discussed in greater detail in <<./configuration.adoc#namespace-errorhandler,Error Handling>>.
954+
This is discussed in greater detail in <<./error-handling.adoc#error-handling,Error Handling>>.
955955

956956

957957
See also <<./dsl.adoc#java-dsl-channels,Message Channels>> in the Java DSL chapter for more information about message channel and interceptors.

src/reference/asciidoc/configuration.adoc

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -149,81 +149,6 @@ To do so, define a bean with the appropriate JNDI name for your environment, as
149149

150150
The next section describes what happens if exceptions occur within the asynchronous invocations.
151151

152-
[[namespace-errorhandler]]
153-
=== Error Handling
154-
155-
As described in the <<./overview.adoc#overview,overview>> at the very beginning of this manual, one of the main motivations behind a message-oriented framework such as Spring Integration is to promote loose coupling between components.
156-
The message channel plays an important role, in that producers and consumers do not have to know about each other.
157-
However, the advantages also have some drawbacks.
158-
Some things become more complicated in a loosely coupled environment, and one example is error handling.
159-
160-
When sending a message to a channel, the component that ultimately handles that message may or may not be operating within the same thread as the sender.
161-
If using a simple default `DirectChannel` (when the `<channel>` element that has no `<queue>` child element and no 'task-executor' attribute),
162-
the message handling occurs in the same thread that sends the initial message.
163-
In that case, if an `Exception` is thrown, it can be caught by the sender (or it may propagate past the sender if it is an uncaught `RuntimeException`).
164-
So far, everything is fine.
165-
This is the same behavior as an exception-throwing operation in a normal call stack.
166-
167-
A message flow that runs on a caller thread might be invoked through a messaging gateway (see <<./gateway.adoc#gateway,Messaging Gateways>>) or a `MessagingTemplate` (see <<./channel.adoc#channel-template,`MessagingTemplate`>>).
168-
In either case, the default behavior is to throw any exceptions to the caller.
169-
For the messaging gateway, see <<./gateway.adoc#gateway-error-handling,Error Handling>> for details about how the exception is thrown and how to configure the gateway to route the errors to an error channel instead.
170-
When using a `MessagingTemplate` or sending to a `MessageChannel` directly, exceptions are always thrown to the caller.
171-
172-
When adding asynchronous processing, things become rather more complicated.
173-
For instance, if the 'channel' element does provide a 'queue' child element, the component that handles the message operates in a different thread than the sender.
174-
The same is true when an `ExecutorChannel` is used.
175-
The sender may have dropped the `Message` into the channel and moved on to other things.
176-
There is no way for the `Exception` to be thrown directly back to that sender by using standard `Exception` throwing techniques.
177-
Instead, handling errors for asynchronous processes requires that the error-handling mechanism also be asynchronous.
178-
179-
Spring Integration supports error handling for its components by publishing errors to a message channel.
180-
Specifically, the `Exception` becomes the payload of a Spring Integration `ErrorMessage`.
181-
That `Message` is then sent to a message channel that is resolved in a way that is similar to the 'replyChannel' resolution.
182-
First, if the request `Message` being handled at the time the `Exception` occurred contains an 'errorChannel' header (the header name is defined in the `MessageHeaders.ERROR_CHANNEL` constant), the `ErrorMessage` is sent to that channel.
183-
Otherwise, the error handler sends to a "`global`" channel whose bean name is `errorChannel` (this is also defined as a constant: `IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME`).
184-
185-
A default `errorChannel` bean is created internally by the Framework.
186-
However, you can define your own if you want to control the settings.
187-
The following example shows how to define an error channel backed by a queue with a capacity of 500:
188-
189-
====
190-
[source,xml]
191-
----
192-
<int:channel id="errorChannel">
193-
<int:queue capacity="500"/>
194-
</int:channel>
195-
----
196-
====
197-
198-
NOTE: The default error channel is a `PublishSubscribeChannel`.
199-
200-
The most important thing to understand here is that the messaging-based error handling applies only to exceptions that are thrown by a Spring Integration task that is executing within a `TaskExecutor`.
201-
This does not apply to exceptions thrown by a handler that operates within the same thread as the sender (for example, through a `DirectChannel` as described earlier in this section).
202-
203-
NOTE: When exceptions occur in a scheduled poller task's execution, those exceptions are wrapped in `ErrorMessage` instances and sent to the 'errorChannel' as well.
204-
205-
To enable global error handling, register a handler on that channel.
206-
For example, you can configure Spring Integration's `ErrorMessageExceptionTypeRouter` as the handler of an endpoint that is subscribed to the 'errorChannel'.
207-
That router can then spread the error messages across multiple channels, based on the `Exception` type.
208-
209-
Starting with version 4.3.10, Spring Integration provides the `ErrorMessagePublisher` and the `ErrorMessageStrategy`.
210-
You can use them as a general mechanism for publishing `ErrorMessage` instances.
211-
You can call or extend them in any error handling scenarios.
212-
The `ErrorMessageSendingRecoverer` extends this class as a `RecoveryCallback` implementation that can be used with retry, such as the
213-
<<./handler-advice.adoc#retry-advice,`RequestHandlerRetryAdvice`>>.
214-
The `ErrorMessageStrategy` is used to build an `ErrorMessage` based on the provided exception and an `AttributeAccessor` context.
215-
It can be injected into any `MessageProducerSupport` or `MessagingGatewaySupport`.
216-
The `requestMessage` is stored under `ErrorMessageUtils.INPUT_MESSAGE_CONTEXT_KEY` in the `AttributeAccessor` context.
217-
The `ErrorMessageStrategy` can use that `requestMessage` as the `originalMessage` property of the `ErrorMessage` it creates.
218-
The `DefaultErrorMessageStrategy` does exactly that.
219-
220-
Starting with version 5.2, all the `MessageHandlingException` instances thrown by the framework components, includes a component `BeanDefinition` resource and source to determine a configuration point form the exception.
221-
In case of XML configuration, a resource is an XML file path and source an XML tag with its `id` attribute.
222-
With Java & Annotation configuration, a resource is a `@Configuration` class and source is a `@Bean` method.
223-
In most case the target integration flow solution is based on the out-of-the-box components and their configuration options.
224-
When an exception happens at runtime, there is no any end-user code involved in stack trace because an execution is against beans, not their configuration.
225-
Including a resource and source of the bean definition helps to determine possible configuration mistakes and provides better developer experience.
226-
227152
[[global-properties]]
228153
=== Global Properties
229154

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[[error-handling]]
2+
== Error Handling
3+
4+
As described in the <<./overview.adoc#overview,overview>> at the very beginning of this manual, one of the main motivations behind a message-oriented framework such as Spring Integration is to promote loose coupling between components.
5+
The message channel plays an important role, in that producers and consumers do not have to know about each other.
6+
However, the advantages also have some drawbacks.
7+
Some things become more complicated in a loosely coupled environment, and one example is error handling.
8+
9+
When sending a message to a channel, the component that ultimately handles that message may or may not be operating within the same thread as the sender.
10+
If using a simple default `DirectChannel` (when the `<channel>` element that has no `<queue>` child element and no 'task-executor' attribute), the message handling occurs in the same thread that sends the initial message.
11+
In that case, if an `Exception` is thrown, it can be caught by the sender (or it may propagate past the sender if it is an uncaught `RuntimeException`).
12+
This is the same behavior as an exception-throwing operation in a normal Java call stack.
13+
14+
A message flow that runs on a caller thread might be invoked through a messaging gateway (see <<./gateway.adoc#gateway,Messaging Gateways>>) or a `MessagingTemplate` (see <<./channel.adoc#channel-template,`MessagingTemplate`>>).
15+
In either case, the default behavior is to throw any exceptions to the caller.
16+
For the messaging gateway, see <<./gateway.adoc#gateway-error-handling,Error Handling>> for details about how the exception is thrown and how to configure the gateway to route the errors to an error channel instead.
17+
When using a `MessagingTemplate` or sending to a `MessageChannel` directly, exceptions are always thrown to the caller.
18+
19+
When adding asynchronous processing, things become rather more complicated.
20+
For instance, if the 'channel' element does provide a 'queue' child element (`QueueChannel` in Java & Annotations Configuration), the component that handles the message operates in a different thread than the sender.
21+
The same is true when an `ExecutorChannel` is used.
22+
The sender may have dropped the `Message` into the channel and moved on to other things.
23+
There is no way for the `Exception` to be thrown directly back to that sender by using standard `Exception` throwing techniques.
24+
Instead, handling errors for asynchronous processes requires that the error-handling mechanism also be asynchronous.
25+
26+
Spring Integration supports error handling for its components by publishing errors to a message channel.
27+
Specifically, the `Exception` becomes the payload of a Spring Integration `ErrorMessage`.
28+
That `Message` is then sent to a message channel that is resolved in a way that is similar to the 'replyChannel' resolution.
29+
First, if the request `Message` being handled at the time the `Exception` occurred contains an 'errorChannel' header (the header name is defined in the `MessageHeaders.ERROR_CHANNEL` constant), the `ErrorMessage` is sent to that channel.
30+
Otherwise, the error handler sends to a "`global`" channel whose bean name is `errorChannel` (this is also defined as a constant: `IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME`).
31+
32+
A default `errorChannel` bean is created internally by the Framework.
33+
However, you can define your own if you want to control the settings.
34+
The following example shows how to define an error channel in XML configuration backed by a queue with a capacity of `500`:
35+
36+
====
37+
[source,xml]
38+
----
39+
<int:channel id="errorChannel">
40+
<int:queue capacity="500"/>
41+
</int:channel>
42+
----
43+
====
44+
45+
NOTE: The default error channel is a `PublishSubscribeChannel`.
46+
47+
The most important thing to understand here is that the messaging-based error handling applies only to exceptions that are thrown by a Spring Integration task that is executing within a `TaskExecutor`.
48+
This does not apply to exceptions thrown by a handler that operates within the same thread as the sender (for example, through a `DirectChannel` as described earlier in this section).
49+
50+
NOTE: When exceptions occur in a scheduled poller task's execution, those exceptions are wrapped in `ErrorMessage` instances and sent to the 'errorChannel' as well.
51+
52+
To enable global error handling, register a handler on that channel.
53+
For example, you can configure Spring Integration's `ErrorMessageExceptionTypeRouter` as the handler of an endpoint that is subscribed to the 'errorChannel'.
54+
That router can then spread the error messages across multiple channels, based on the `Exception` type.
55+
56+
Starting with version 4.3.10, Spring Integration provides the `ErrorMessagePublisher` and the `ErrorMessageStrategy`.
57+
You can use them as a general mechanism for publishing `ErrorMessage` instances.
58+
You can call or extend them in any error handling scenarios.
59+
The `ErrorMessageSendingRecoverer` extends this class as a `RecoveryCallback` implementation that can be used with retry, such as the
60+
<<./handler-advice.adoc#retry-advice,`RequestHandlerRetryAdvice`>>.
61+
The `ErrorMessageStrategy` is used to build an `ErrorMessage` based on the provided exception and an `AttributeAccessor` context.
62+
It can be injected into any `MessageProducerSupport` or `MessagingGatewaySupport`.
63+
The `requestMessage` is stored under `ErrorMessageUtils.INPUT_MESSAGE_CONTEXT_KEY` in the `AttributeAccessor` context.
64+
The `ErrorMessageStrategy` can use that `requestMessage` as the `originalMessage` property of the `ErrorMessage` it creates.
65+
The `DefaultErrorMessageStrategy` does exactly that.
66+
67+
Starting with version 5.2, all the `MessageHandlingException` instances thrown by the framework components, includes a component `BeanDefinition` resource and source to determine a configuration point form the exception.
68+
In case of XML configuration, a resource is an XML file path and source an XML tag with its `id` attribute.
69+
With Java & Annotation configuration, a resource is a `@Configuration` class and source is a `@Bean` method.
70+
In most case the target integration flow solution is based on the out-of-the-box components and their configuration options.
71+
When an exception happens at runtime, there is no any end-user code involved in stack trace because an execution is against beans, not their configuration.
72+
Including a resource and source of the bean definition helps to determine possible configuration mistakes and provides better developer experience.

src/reference/asciidoc/index-single.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ The appendices cover advanced topics and additional resources.
8989

9090
The last appendix covers the history of Spring Integration.
9191

92+
[appendix]
93+
include::./error-handling.adoc[]
94+
9295
[appendix]
9396
include::./spel.adoc[]
9497

src/reference/asciidoc/index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ as single searchable link:index-single.html[html] and link:../pdf/spring-integra
5454
**Appendices** ::
5555

5656
[horizontal]
57+
<<./error-handling.adoc#error-handling,Error Handling>> ::
5758
<<./spel.adoc#spel,Spring Expression Language (SpEL)>> ::
5859
<<./message-publishing.adoc#message-publishing,Message Publishing>> ::
5960
<<./transactions.adoc#transactions,Transaction Support>> ::

src/reference/asciidoc/scatter-gather.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ In other cases something like a "`compensation message`" should be considered fo
163163

164164
Every async sub-flow should be configured with a `errorChannel` header for the proper error message sending from the `MessagePublishingErrorHandler`.
165165
Otherwise, an error will be sent to the global `errorChannel` with the common error handling logic.
166-
See <<./configuration.adoc#namespace-errorhandler,Error Handling>> for more information about async error processing.
166+
See <<./error-handling.adoc#error-handling,Error Handling>> for more information about async error processing.
167167

168168
Synchronous flows may use an `ExpressionEvaluatingRequestHandlerAdvice` for ignoring the exception or returning a compensation message.
169169
When an exception is thrown from one of the sub-flows to the `ScatterGatherHandler`, it is just re-thrown to upstream.

src/reference/asciidoc/whats-new.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ The `Function<MessageGroup, Map<String, Object>>` strategy has been introduced f
8484
See <<./aggregator.adoc#aggregator-api,Aggregator Programming Model>> for more information.
8585

8686
All the `MessageHandlingException` s thrown in the framework, includes now a bean resource and source for back tracking a configuration part in case no end-user code involved.
87-
See <<./configuration.adoc#namespace-errorhandler,Error Handling>> for more information.
87+
See <<./error-handling.adoc#error-handling,Error Handling>> for more information.
8888

8989
[[x5.2-amqp]]
9090
==== AMQP Changes

0 commit comments

Comments
 (0)