Spring Book – Chapter 20 – Spring Integration

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

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

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

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

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

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

Page Visitors: 13349

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 20 – Spring Integration

  1. 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!

  2. 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

Leave a Reply

Your email address will not be published. Required fields are marked *