MessageHandlerChain is an implementation of
MessageHandler that can be configured as a single message endpoint while actually delegating to a chain of other handlers, such as filters, transformers, splitters, and so on.
When several handlers need to be connected in a fixed, linear progression, this can lead to a much simpler configuration.
For example, it is fairly common to provide a transformer before other components.
Similarly, when you provide a filter before some other component in a chain, you essentially create a selective consumer.
In either case, the chain requires only a single
input-channel and a single
output-channel, eliminating the need to define channels for each individual component.
The handler chain simplifies configuration while internally maintaining the same degree of loose coupling between components, and it is trivial to modify the configuration if at some point a non-linear arrangement is required.
Internally, the chain is expanded into a linear setup of the listed endpoints, separated by anonymous channels.
The reply channel header is not taken into account within the chain.
Only after the last handler is invoked is the resulting message forwarded to the reply channel or the chain’s output channel.
Because of this setup, all handlers except the last must implement the
MessageProducer interface (which provides a 'setOutputChannel()' method).
outputChannel on the
MessageHandlerChain is set, the last handler needs only an output channel.
As with other endpoints, the
In most cases, you need not implement
The next section focuses on namespace support for the chain element.
Most Spring Integration endpoints, such as service activators and transformers, are suitable for use within a
<chain> element provides an
If the last element in the chain is capable of producing reply messages (optional), it also supports an
The sub-elements are then filters, transformers, splitters, and service-activators.
The last element may also be a router or an outbound channel adapter.
The following example shows a chain definition:
<int:chain input-channel="input" output-channel="output"> <int:filter ref="someSelector" throw-exception-on-rejection="true"/> <int:header-enricher> <int:header name="thing1" value="thing2"/> </int:header-enricher> <int:service-activator ref="someService" method="someMethod"/> </int:chain>
<header-enricher> element used in the preceding example sets a message header named
thing1 with a value of
thing2 on the message.
A header enricher is a specialization of
Transformer that touches only header values.
You could obtain the same result by implementing a
MessageHandler that did the header modifications and wiring that as a bean, but the header-enricher is a simpler option.
<chain> can be configured as the last “closed-box” consumer of the message flow.
For this solution, you can to put it at the end of the <chain> some <outbound-channel-adapter>, as the following example shows:
<int:chain input-channel="input"> <int-xml:marshalling-transformer marshaller="marshaller" result-type="StringResult" /> <int:service-activator ref="someService" method="someMethod"/> <int:header-enricher> <int:header name="thing1" value="thing2"/> </int:header-enricher> <int:logging-channel-adapter level="INFO" log-full-message="true"/> </int:chain>
Disallowed Attributes and Elements
Certain attributes, such as
For the Spring Integration core components, the XML schema itself enforces some of these constraints. However, for non-core components or your own custom components, these constraints are enforced by the XML namespace parser, not by the XML schema.
These XML namespace parser constraints were added with Spring Integration 2.2.
If you try to use disallowed attributes and elements, the XML namespace parser throws a
Beginning with Spring Integration 3.0, if a chain element is given an
id attribute, the bean name for the element is a combination of the chain’s
id and the
id of the element itself.
id attributes are not registered as beans, but each one is given a
componentName that includes the chain
Consider the following example:
<int:chain id="somethingChain" input-channel="input"> <int:service-activator id="somethingService" ref="someService" method="someMethod"/> <int:object-to-json-transformer/> </int:chain>
In the preceding example:
<chain>root element has an
idof 'somethingChain'. Consequently, the
EventDrivenConsumer, depending on the
input-channeltype) bean takes this value as its bean name.
MessageHandlerChainbean acquires a bean alias ('somethingChain.handler'), which allows direct access to this bean from the
<service-activator>is not a fully fledged messaging endpoint (it is not a
EventDrivenConsumer). It is a
<chain>. In this case, the bean name registered with the
ServiceActivatingHandlertakes the same value but without the '.handler' suffix. It becomes 'somethingChain$child.somethingService'.
<object-to-json-transformer>, does not have an
componentNameis based on its position in the
<chain>. In this case, it is 'somethingChain$child#1'. (The final element of the name is the order within the chain, beginning with '#0'). Note, this transformer is not registered as a bean within the application context, so it does not get a
beanName. However its
componentNamehas a value that is useful for logging and other purposes.
id attribute for
<chain> elements lets them be eligible for JMX export, and they are trackable in the message history.
You can access them from the
BeanFactory by using the appropriate bean name, as discussed earlier.
It is useful to provide an explicit
Sometimes, you need to make a nested call to another chain from within a chain and then come back and continue execution within the original chain. To accomplish this, you can use a messaging gateway by including a <gateway> element, as the following example shows:
<int:chain id="main-chain" input-channel="in" output-channel="out"> <int:header-enricher> <int:header name="name" value="Many" /> </int:header-enricher> <int:service-activator> <bean class="org.foo.SampleService" /> </int:service-activator> <int:gateway request-channel="inputA"/> </int:chain> <int:chain id="nested-chain-a" input-channel="inputA"> <int:header-enricher> <int:header name="name" value="Moe" /> </int:header-enricher> <int:gateway request-channel="inputB"/> <int:service-activator> <bean class="org.foo.SampleService" /> </int:service-activator> </int:chain> <int:chain id="nested-chain-b" input-channel="inputB"> <int:header-enricher> <int:header name="name" value="Jack" /> </int:header-enricher> <int:service-activator> <bean class="org.foo.SampleService" /> </int:service-activator> </int:chain>
In the preceding example,
nested-chain-a is called at the end of
main-chain processing by the 'gateway' element configured there.
nested-chain-a, a call to a
nested-chain-b is made after header enrichment.
Then the flow comes back to finish execution in
Finally, the flow returns to
When the nested version of a
<gateway> element is defined in the chain, it does not require the
Instead, it takes the message in its current state and places it on the channel defined in the
When the downstream flow initiated by that gateway completes, a
Message is returned to the gateway and continues its journey within the current chain.