Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
Enterprise Integration Zone is brought to you in partnership with:

I'm an Enterprise Software Architect working predominantly on integration solutions in the financial software arena. Core competencies include Java, Message Driven Architectures, Spring Integration and data storage technologies. Any spare time I have is used developing my Border Collie into a competition trial dog. Matt is a DZone MVB and is not an employee of DZone and has posted 5 posts at DZone. You can read more from them at their website. View Full User Profile

Spring Integration - Input Channel Definition

05.11.2012
Email
Views: 2273
  • submit to reddit
The Enterprise Integration Zone is presented by DZone and FuseSource. Check out the EI Zone for real world integration scenarios to help you learn which technology will give you the most elegant solution.  For open source systems based on Apache Camel, ActiveMQ, or ServiceMix, look into FuseSource's training and technology.  
In a pipes and filters architecture, pipes are connectors or channels. Although at first sight trivial, channels are fairly rich semantically - they allow typing, synchronous and asynchronous input, direct and multicast notifications, send and wait (rendezvous) as well as queued input and wrapping by adapters.

To define or not to define?

Explicit specification or definition of the Spring Integration input-channel is not mandatory for SI constructs to operate. The framework will create input channels automatically if they are not explicitly defined in configuration, but what are the pros and cons of this aproach?

Take the following chain as an example:
<int:chain input-channel="processing-channel"
           output-channel="claim-check-out-channel">
        <int:service-activator expression="new String('different string')"/>
</int:chain>

The input-channel named "processing-channel" will get created automatically as it's not explicitly defined here. In a trivial configuration such as this one there's very little difference between including the channel explicitly or using the frameworks implicit creation. In larger configurations the extra lines taken by a dozen or more channel definitions may start to make the context appear a little cluttered. In this case it's then possible to consider decomposing the configuration but sometimes you just can't decompose those flows into distinct contexts, all of the constructs naturally belong together in a single context.

Channel Typing

One of the features of Spring Integration channels is that they can be strongly type matched with payload object types. It's worth considering adopting a convention for specifying the payload type on the channel because not only does this provide strong payload typing but improved better confidence that whomever is reading the flow after its built can readily see which type of objects are traversing the flows.

Of course channels can't always be strongly typed but in many cases they can. Here's an example of one that can:

<int:channel id="processing-channel" datatype="java.util.UUID"/>
<int:chain input-channel="processing-channel"
           output-channel="claim-check-out-channel">
    <int:service-activator expression="new String('different string')"/>
</int:chain>

You can see that on the first line of this Spring Integration configuration the contract for operation specifies that a java.util.UUID type object is expected on the input channel. In this case the payload contained a claim-check identifier and is to be replaced with an arbitrary string for the example.

In the case where a channel is strongly typed and the contract broken an exception will be thrown, here's an example of what you'll see:
org.springframework.integration.MessageDeliveryException: 
Channel 'processing-channel' expected one of the following datataypes 
[interface java.util.Map], 
but received [class java.util.UUID]
 at org.springframework.integration.channel.AbstractMessageChannel.convertPayloadIfNecessary(AbstractMessageChannel.java:187)

In this example I changed the datatype to java.util.Map and ran a test with a payload that's actually a java.util.UUID. That was the start of the stack trace that was generated when the exception was thrown.

It's possible to provide a collection of message types for channel specification. Any messages entering the channel must conform to at least one of the specified types otherwise a MessageDeliveryException is generated. The following example shows a change that will prevent the exception above. The configuration now allows for two types of message payload, java.util.Map and java.util.UUID to enter the channel.

 
<int:channel id="processing-channel" datatype="java.util.UUID, java.util.Map"/>
    <int:chain input-channel="processing-channel"
               output-channel="claim-check-out-channel">
        <int:service-activator expression="new String('different string')"/>
</int:chain>
Published at DZone with permission of Matt Vickery, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Enterprise Integration is a huge problem space for developers, and with so many different technologies to choose from, finding the most elegant solution can be tricky.  The Enterprise Integration Zone is a place for enterprise developers of all backgrounds to share design patterns and technology solutions that make integration easier for various scenarios.  FuseSource proudly supports the EI Zone and provides its own gamut of training, services, and tools based on Apache Camel, ActiveMQ, CXF, and ServiceMix.