Chapter 315. Spring Integration Component
Available as of Camel version 1.4
The spring-integration: component provides a bridge for Camel components to talk to spring integration endpoints.
Maven users will need to add the following dependency to their pom.xml
for this component:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-integration</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
315.1. URI format
spring-integration:defaultChannelName[?options]
Where defaultChannelName represents the default channel name which is used by the Spring Integration Spring context. It will equal to the inputChannel
name for the Spring Integration consumer and the outputChannel
name for the Spring Integration provider.
You can append query options to the URI in the following format, ?option=value&option=value&…
315.2. Options
The Spring Integration component has no options.
The Spring Integration endpoint is configured using URI syntax:
spring-integration:defaultChannel
with the following path and query parameters:
315.2.1. Path Parameters (1 parameters):
Name | Description | Default | Type |
---|---|---|---|
defaultChannel | Required The default channel name which is used by the Spring Integration Spring context. It will equal to the inputChannel name for the Spring Integration consumer and the outputChannel name for the Spring Integration provider. | String |
315.2.2. Query Parameters (7 parameters):
Name | Description | Default | Type |
---|---|---|---|
inOut (common) | The exchange pattern that the Spring integration endpoint should use. If inOut=true then a reply channel is expected, either from the Spring Integration Message header or configured on the endpoint. | false | boolean |
bridgeErrorHandler (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean |
inputChannel (consumer) | The Spring integration input channel name that this endpoint wants to consume from Spring integration. | String | |
exceptionHandler (consumer) | To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this options is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. | ExceptionHandler | |
exchangePattern (consumer) | Sets the exchange pattern when the consumer creates an exchange. | ExchangePattern | |
outputChannel (producer) | The Spring integration output channel name that is used to send messages to Spring integration. | String | |
synchronous (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean |
315.3. Usage
The Spring integration component is a bridge that connects Camel endpoints with Spring integration endpoints through the Spring integration’s input channels and output channels. Using this component, we can send Camel messages to Spring Integration endpoints or receive messages from Spring integration endpoints in a Camel routing context.
315.4. Examples
315.4.1. Using the Spring integration endpoint
You can set up a Spring integration endpoint using a URI, as follows:
Or directly using a Spring integration channel name:
315.4.2. The Source and Target adapter
Spring integration also provides the Spring integration’s source and target adapters, which can route messages from a Spring integration channel to a Camel endpoint or from a Camel endpoint to a Spring integration channel.
This example uses the following namespaces:
You can bind your source or target to a Camel endpoint as follows:
315.5. See Also
- Configuring Camel
- Component
- Endpoint
- Getting Started
315.6. Spring Java Config
Spring started life using XML Config to wire beans together. However some folks don’t like using XML and would rather use Java code which led to the creation of Guice along with the Spring JavaConfig project.
You can use either the XML or Java config approaches with Camel; its your choice really on which you prefer.
315.6.1. Using Spring Java Config
To use Spring Java Config in your Camel project the easiest thing to do is add the following to your pom.xml
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-javaconfig</artifactId> <version>${camel-version}</version> </dependency>
This will then add the dependencies on the Spring JavaConfig library along with some helper classes for configuring Camel inside Spring.
Note that this library is totally optional; you could just wire Camel together yourself with Java Config.
315.6.2. Configuration
The most common case of using JavaConfig with Camel would be to create configuration with defined list of routes to be used by router.
@Configuration public class MyRouteConfiguration extends CamelConfiguration { @Autowire private MyRouteBuilder myRouteBuilder; @Autowire private MyAnotherRouteBuilder myAnotherRouteBuilder; @Override public List<RouteBuilder> routes() { return Arrays.asList(myRouteBuilder, myAnotherRouteBuilder); } }
Starting from Camel 2.13.0 you can skip the routes() definition, and fall back to the RouteBuilder instances located in the Spring context.
@Configuration @ComponentScan("com.example.routes") public class MyRouteConfiguration extends CamelConfiguration { }
315.6.3. Testing
Since Camel 2.11.0 you can use the CamelSpringJUnit4ClassRunner
with CamelSpringDelegatingTestContextLoader
. This is the recommended way to test Java Config and Camel integration.
If you wish to create a collection of RouteBuilder instances then derive from the CamelConfiguration helper class and implement the routes() method. Keep in mind that (starting from the Camel 2.13.0) if you don’t override routes() method, then CamelConfiguration will use all RouteBuilder instances available in the Spring context.
The following example using Java Config demonstrates how to test Java Config integration with Camel 2.10 and lower. Keep in mind that JavaConfigContextLoader
is deprecated and could be removed in the future versions of Camel on the behalf of the CamelSpringDelegatingTestContextLoader
.
The @ContextConfiguration annotation tells the Spring Testing framework to load the ContextConfig class as the configuration to use. This class derives from SingleRouteCamelConfiguration which is a helper Spring Java Config class which will configure the CamelContext for us and then register the RouteBuilder we create.