Channel Interceptors
Mechanism by which to intercept message and do some value addition can be achieved by the usage of appropriate channel interceptors. They operate on the messages and can intercept at various stages as summarized below:
- Before/Pre Sending
- After/Post Sending
- Before/Pre Receiving
- After/Post Receiving
The ChannelInterceptor interface provides methods for each of those operations as shown in Listing 22-21 below.
Listing 22-21. ChannelInterceptor Interface
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.springframework.integration.channel; public interface ChannelInterceptor { Message<?> preSend(Message<?> message, MessageChannel channel); void postSend(Message<?> message, MessageChannel channel, boolean sent); boolean preReceive(MessageChannel channel); Message<?> postReceive(Message<?> message, MessageChannel channel); } |
Once you implement the ChannelInterceptor interface according to your application require, you need to register the interceptor with the channel as shown in Listing 22-22 below.
Listing 22-22. Registering interceptor with channel
1 |
yourChannel.addInterceptor(yourChannelInterceptor); |
While it is usually not required to implement all the methods in ChannelInterceptor interface, Spring Integration provides class namely ChannelInterceptorAdapter which can be extended and appropriate methods written as shown in Listing 22-23 below.
Listing 22-23. Extending ChannelInterceporAdaptor class in place of implementing ChannelInterceptor interface
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.springframework.integration.channel.interceptor; public class CountingChannelInterceptor extends ChannelInterceptorAdapter { @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { //..Do the necessary operation } } |
Spring Integration also provides an implementation of the Wire Tap pattern as explained in section,”Other EAI Components”. It is a channel interceptor, which sends the Message to another channel without altering the existing flow and can be very useful for debugging and monitoring purposes.
Router Implementations
As discussed earlier Spring Integration provides out-of-the-box following router implementations:
- org.springframework.integration.router.PayloadTypeRouter
- org.springframework.integration.router.HeaderValueRouter
- org.springframework.integration.router.RecepientListRouter
- org.springframework.integration.router.ErrorMessageExceptionTypeRouter
PayloadTypeRouter
Based on the type of the payload the PayloadTypeRouter determines the routing of messages to different channels. The router attached to the channel will determine the type of the payload and appropriately distributes it to different channels for processing. The following Listing 22-24 below shows configuring a payload type router in which if the message is of type A it is routed to channel A and if it is of type B, routed to channel B respectively.
Listing 22-24. Sample of PayloadTypeRouter
1 2 3 4 5 6 7 |
<int:payload-type-router input-channel="inputChannel"> <int:mapping type="com.mybook.sample.integration.TypeA" channel="typeAChannel" /> <int:mapping type="com.mybook.sample.integration.TypeB" channel="typeBChannel" /> </int:payload-type-router> |
HeaderValueRouter
Based on the message header properties the HeaderValueRouter determines the routing of messages to different channels. When a HeaderValueRouter is configured, it is initialized with the name of the header to be evaluated for routing. The value of the header could be one of following:
- Arbitrary value
- Channel name
Listing 22-25 and Listing 22-26 below shows two ways of configuring header value router using Spring Integration namespace. The first one is configuring the router where there is mapping of header values to appropriate channels and the second one is configuring this router where mapping of header values to appropriate channel names is not required since header values themselves represent channel names.
Listing 22-25. Configuring the router where there is mapping of header values to appropriate channels
1 2 3 4 5 6 7 |
<int:header-value-router input-channel="inputChannel" header-name="headerName"> <int:mapping value="firstHeaderValue" channel="channelOne" /> <int:mapping value="secondHeaderValue" channel="channelTwo" /> </int:header-value-router> |
Listing 22-26. Configuring the router where mapping of header values to appropriate channel names is not required since header values themselves represent channel names
1 |
<int:header-value-router input-channel="inputChannel" header-name="headerName"/> |
Page Visitors: 13000

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Hi,
Thanks for this blog and information sharing!
I have one question from Spring Integration. Just thought of checking with you.
I have SimplewebserviceOutBoundGateway. I need to send a document / attachment to the webservice. How will i achieve that?
This is my current gateway configuration:
Please let me know, if you can throw some light in this area.
Thanks in Advance!
Hi,
Thank you for your information sharing.
I have one doubt,could you please suggest me the correct frameworks to address that issue.
My requirement is I would like to get and process files using spring and camel.
Thanks,
Madhu