To create a Message Channel instance, you can use the 'channel' element:
<channel id="exampleChannel"/>
The default channel type is Point to Point. To create a Publish Subscribe channel, use the "publish-subscribe-channel" element:
<publish-subscribe-channel id="exampleChannel"/>
To create a Datatype Channel that only
accepts messages containing a certain payload type, provide the fully-qualified class name in the
channel element's datatype
attribute:
<channel id="numberChannel" datatype="java.lang.Number"/>
Note that the type check passes for any type that is assignable to the channel's
datatype. In other words, the "numberChannel" above would accept messages whose payload is
java.lang.Integer
or java.lang.Double
. Multiple types can be
provided as a comma-delimited list:
<channel id="stringOrNumberChannel" datatype="java.lang.String,java.lang.Number"/>
When using the "channel" element without any sub-elements, it will create a DirectChannel
instance (a SubscribableChannel
).
However, you can also provide a variety of "queue" sub-elements to create the channel types (as described in Section 3.2, “Message Channel Implementations”). Examples of each are shown below.
As mentioned above, DirectChannel
is the default type.
<channel id="exampleChannel"/>
To create a QueueChannel
, use the "queue" sub-element.
You may specify the channel's capacity:
<channel id="exampleChannel"> <queue capacity="25"/> </channel>
Note | |
---|---|
If you do not provide a value for the 'capacity' attribute on this <queue/> sub-element, the resulting queue will be unbounded. To avoid issues such as OutOfMemoryErrors, it's highly recommended to set an explicit value for a bounded queue. |
To create a PublishSubscribeChannel
, use the "publish-subscribe-channel" element.
When using this element, you can also specify the "task-executor" used for publishing
Messages (if none is specified it simply publishes in the sender's thread):
<publish-subscribe-channel id="exampleChannel" task-executor="someTaskExecutor"/>
If you are providing a Resequencer or Aggregator downstream
from a PublishSubscribeChannel
, then you can set the 'apply-sequence' property
for the channel. That will indicate that the channel should set the sequence-size and sequence-number
Message headers prior to passing the Messages along. For example, if there are 5 subscribers, the
sequence-size would be set to 5, and the Messages would have sequence-number header values ranging
from 1 to 5. This value is 'false' by default.
<publish-subscribe-channel id="exampleChannel" apply-sequence="true"/>
To create a PriorityChannel
, use the "priority-queue" sub-element:
<channel id="exampleChannel"> <priority-queue capacity="20"/> </channel>
By default, the channel will consult the MessagePriority
header of the
message. However, a custom Comparator
reference may be
provided instead. Also, note that the PriorityChannel
(like the other types)
does support the "datatype" attribute. As with the QueueChannel, it also supports a "capacity" attribute.
The following example demonstrates all of these:
<channel id="exampleChannel" datatype="example.Widget"> <priority-queue comparator="widgetComparator" capacity="10"/> </channel>
A RendezvousChannel
is created when the queue sub-element is
a <rendezvous-queue>. It does not provide any additional configuration options.
<channel id="exampleChannel"/> <rendezvous-queue/> </channel>
The ThreadLocalChannel
does not provide any additional configuration options.
<thread-local-channel id="exampleChannel"/>
Message channels may also have interceptors as described in Section 3.3, “Channel Interceptors”. One or
more <interceptor> elements can be added as sub-elements of <channel> (or the more specific element
types). Provide the "ref" attribute to reference any Spring-managed object that implements the
ChannelInterceptor
interface:
<channel id="exampleChannel"> <interceptors> <ref bean="trafficMonitoringInterceptor"/> </interceptors> </channel>
In general, it is a good idea to define the interceptor implementations in a separate location since they usually provide common behavior that can be reused across multiple channels.