Namespace Support
Spring Integration offers a number of configuration options. Which option you choose depends upon your particular
needs and at what level you prefer to work. As with the Spring framework in general, it is also possible to mix
and match the various techniques according to the particular problem at hand. For example, you may choose the
XSD-based namespace for the majority of configuration combined with a handful of objects that are configured with
annotations. As much as possible, the two provide consistent naming. XML elements defined by the XSD schema will
match the names of annotations, and the attributes of those XML elements will match the names of annotation
properties. Direct usage of the API is of course always an option, but we expect that most users will choose one
of the higher-level options, or a combination of the namespace-based and annotation-driven configuration.
Spring Integration components can be configured with XML elements that map directly to the terminology and
concepts of enterprise integration. In many cases, the element names match those of the
Enterprise Integration Patterns.
To enable Spring Integration's core namespace support within your Spring configuration files, add the following
namespace reference and schema mapping in your top-level 'beans' element:
xmlns:integration="http://www.springframework.org/schema/integration"http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
You can choose any name after "xmlns:"; integration is used here for clarity, but you might
prefer a shorter abbreviation. Of course if you are using an XML-editor or IDE support, then the availability of
auto-completion may convince you to keep the longer name for clarity. Alternatively, you can create configuration
files that use the Spring Integration schema as the primary namespace:
<beans:beans xmlns="http://www.springframework.org/schema/integration"xmlns:beans="http://www.springframework.org/schema/beans"]]>
When using this alternative, no prefix is necessary for the Spring Integration elements. On the other hand, if
you want to define a generic Spring "bean" within the same configuration file, then a prefix would be required
for the bean element (<beans:bean ... />). Since it is generally a good idea to modularize the
configuration files themselves based on responsibility and/or architectural layer, you may find it appropriate to
use the latter approach in the integration-focused configuration files, since generic beans are seldom necessary
within those same files. For purposes of this documentation, we will assume the "integration" namespace is
primary.
Many other namespaces are provided within the Spring Integration distribution. In fact, each adapter type (JMS,
File, etc.) that provides namespace support defines its elements within a separate schema. In order to use these
elements, simply add the necessary namespaces with an "xmlns" entry and the corresponding "schemaLocation" mapping.
For example, the following root element shows several of these namespace declarations:
...
]]>
The reference manual provides specific examples of the various elements in their corresponding chapters. Here, the
main thing to recognize is the consistency of the naming for each namespace URI and schema location.
Configuring the Message Bus
The Message Bus plays a central role, but its configuration is quite simple since it is primarily concerned
with managing internal details based on the configuration of channels and endpoints. The bus is aware of its
host application context, and therefore is also capable of auto-detecting the channels and endpoints.
The Message Bus can be configured with a single empty element:
<message-bus/>
The Message Bus provides default error handling for its components in the form of a configurable error channel,
and it will first check for a channel bean named 'errorChannel' within the context:
]]>
When exceptions occur in a scheduled poller task's execution, those exceptions will be wrapped in
ErrorMessages and sent to the 'errorChannel' by default. To enable global error
handling, simply register a handler on that channel. For example, you can configure Spring Integration's
ErrorMessageExceptionTypeRouter as the handler of an endpoint that is subscribed to the
'errorChannel'. That router can then spread the error messages across multiple channels based on
Exception type. However, since most of the errors will already have been wrapped in
MessageDeliveryException or MessageHandlingException,
the ErrorMessageExceptionTypeRouter is typically a better option.
The 'message-bus' element accepts several more optional attributes. First, you can control whether the
MessageBus will be started automatically (the default) or will require explicit startup
by invoking its start() method (MessageBus implements
Spring's Lifecycle interface):
]]>
Another configurable property is the reference to a TaskScheduler implementation.
If not provided, a default will be created. The scheduler is responsible for managing the pollers.
]]>
When the endpoints are concurrency-enabled with their own 'taskExecutor' reference, the invocation of the handling
methods will happen within that executor's thread pool and not the main scheduler pool. However, when no
task-executor is provided for an endpoint's poller, then it will be invoked in the dispatcher's thread
(with the exception of subscribable channels where the subscribers may be invoked directly).
Annotations
In addition to the XML namespace support for configuring Message Endpoints, it is also possible to use
annotations. First, Spring Integration provides the class-level @MessageEndpoint
as a stereotype annotation meaning that is itself annotated with Spring's @Component
annotation and therefore is recognized automatically as a bean definition when using Spring component-scanning.
Even more importantly are the various Method-level annotations that indicate the annotated method is capable of
handling a message. The following example demonstrates both:
@MessageEndpoint
public class FooService {
@ServiceActivator
public void processMessage(Message message) {
...
}
}
Exactly what it means for the method to "handle" the Message depends on the particular annotation. The following
are available with Spring Integration, and the behavior of each is described in its own chapter or section within
this reference: @Transformer, @Router, @Splitter, @Aggregator, @ServiceActivator, and @ChannelAdapter.
The @MessageEndpoint is not required. If you want to configure a POJO reference from the "ref" attribute
of a <service-activator/> element, it is sufficient to provide the method-level annotations.
In most cases, the annotated handler method should not require the Message type as its
parameter. Instead, the method parameter type can match the message's payload type.
public class FooService {
@Handler
public void bar(Foo foo) {
...
}
}
When the method parameter should be mapped from a value in the MessageHeader, another
option is to use the parameter-level @Header annotation. In general, methods
annotated with the Spring Integration annotations can either accept the Message itself, the
message payload, or a header value (with @Header) as the parameter. In fact, the method can accept a combination,
such as:
public class FooService {
@ServiceActivator
public void bar(String payload, @Header("x") int valueX, @Header("y") int valueY) {
...
}
}
There is also a @Headers annotation that provides all of the Message headers as a Map:
public class FooService {
@ServiceActivator
public void bar(String payload, @Headers Map<String, Object> headerMap) {
...
}
}
For several of these annotations, when a Message-handling method returns a non-null value, the endpoint will
attempt to send a reply. This is consistent across both configuration options (namespace and annotations) in that
the such an endpoint's output channel will be used if available, and the message header's REPLY_CHANNEL value
will be the fallback.