DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Spring Integration - Input Channel Definition

Spring Integration - Input Channel Definition

Matt Vickery user avatar by
Matt Vickery
·
May. 12, 12 · Integration Zone · Interview
Like (0)
Save
Tweet
15.06K Views

Join the DZone community and get the full member experience.

Join For Free
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>
Spring Integration Integration Spring Framework

Published at DZone with permission of Matt Vickery, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Open Source Security Risks
  • Top Soft Skills to Identify a Great Software Engineer
  • Ultra-Fast Microservices: When Microstream Meets Wildfly
  • Choosing Between GraphQL Vs REST

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo