A channel adapter is a message endpoint that enables connecting a single sender or receiver to a message channel. Spring Integration provides a number of adapters to support various transports, such as JMS, file, HTTP, web services, mail, and more. Upcoming chapters of this reference guide discuss each adapter. However, this chapter focuses on the simple but flexible method-invoking channel adapter support. There are both inbound and outbound adapters, and each may be configured with XML elements provided in the core namespace. These provide an easy way to extend Spring Integration, as long as you have a method that can be invoked as either a source or a destination.
inbound-channel-adapter element can invoke any method on a Spring-managed object and send a non-null return value to a
MessageChannel after converting the method’s output to a
When the adapter’s subscription is activated, a poller tries to receive messages from the source.
The poller is scheduled with the
TaskScheduler according to the provided configuration.
To configure the polling interval or cron expression for an individual channel adapter, you can provide a 'poller' element with one of the scheduling attributes, such as 'fixed-rate' or 'cron'.
The following example defines two
<int:inbound-channel-adapter ref="source1" method="method1" channel="channel1"> <int:poller fixed-rate="5000"/> </int:inbound-channel-adapter> <int:inbound-channel-adapter ref="source2" method="method2" channel="channel2"> <int:poller cron="30 * 9-17 * * MON-FRI"/> </int:channel-adapter>
See also Channel Adapter Expressions and Scripts.
|If no poller is provided, then a single default poller must be registered within the context. See Endpoint Namespace Support for more detail.|
Important: Poller Configuration
In the the first configuration, the polling task is invoked once per poll, and, during each task (poll), the method (which results in the production of the message) is invoked once, based on the
Note that there is no
However, in the
However, if you are sure that your method can return null and you need to poll for as many sources as available per each poll, you should explicitly set
outbound-channel-adapter element can also connect a
MessageChannel to any POJO consumer method that should be invoked with the payload of messages sent to that channel.
The following example shows how to define an outbound channel adapter:
<int:outbound-channel-adapter channel="channel1" ref="target" method="handle"/> <beans:bean id="target" class="org.MyPojo"/>
If the channel being adapted is a
PollableChannel, you must provide a poller sub-element, as the following example shows:
<int:outbound-channel-adapter channel="channel2" ref="target" method="handle"> <int:poller fixed-rate="3000" /> </int:outbound-channel-adapter> <beans:bean id="target" class="org.MyPojo"/>
You should use a
ref attribute if the POJO consumer implementation can be reused in other
However, if the consumer implementation is referenced by only a single definition of the
<outbound-channel-adapter>, you can define it as an inner bean, as the following example shows:
<int:outbound-channel-adapter channel="channel" method="handle"> <beans:bean class="org.Foo"/> </int:outbound-channel-adapter>
Using both the
Any channel adapter can be created without a
channel reference, in which case it implicitly creates an instance of
The created channel’s name matches the
id attribute of the
channel is not provided,
id is required.
Like many other Spring Integration components, the
<outbound-channel-adapter> also provide support for SpEL expression evaluation.
To use SpEL, provide the expression string in the 'expression' attribute instead of providing the 'ref' and 'method' attributes that are used for method-invocation on a bean.
When an expression is evaluated, it follows the same contract as method-invocation where: the expression for an
<inbound-channel-adapter> generates a message any time the evaluation result is a non-null value, while the expression for an
<outbound-channel-adapter> must be the equivalent of a void-returning method invocation.
Starting with Spring Integration 3.0, an
<int:inbound-channel-adapter/> can also be configured with a SpEL
<expression/> (or even with a
<script/>) sub-element, for when more sophistication is required than can be achieved with the simple 'expression' attribute.
If you provide a script as a
Resource by using the
location attribute, you can also set
refresh-check-delay, which allows the resource to be periodically refreshed.
If you want the script to be checked on each poll, you would need to coordinate this setting with the poller’s trigger, as the following example shows:
<int:inbound-channel-adapter ref="source1" method="method1" channel="channel1"> <int:poller max-messages-per-poll="1" fixed-delay="5000"/> <script:script lang="ruby" location="Foo.rb" refresh-check-delay="5000"/> </int:inbound-channel-adapter>
See also the
cacheSeconds property on the
ReloadableResourceBundleExpressionSource when using the
For more information regarding expressions, see Spring Expression Language (SpEL).
For scripts, see Groovy support and Scripting Support.