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 must specify the channel's capacity:
<channel id="exampleChannel"> <queue capacity="25"/> </channel>
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"/>
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>
The RendezvousChannel
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"> <interceptor ref="trafficMonitoringInterceptor"/> </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.