Spring Integration

Spring Integration Component

The spring-integration: component provides a bridge for Apache Camel components to talk to spring integration endpoints.

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&...

Options

Table 70 lists the optional spring-integration uri options:

Table 70. URI options

Name Description Example
inputChannel The Spring integration input channel name that this endpoint wants to consume from, where the specified channel name is defined in the Spring context. inputChannel=requestChannel
outputChannel The Spring integration output channel name that is used to send messages to the Spring integration context. outputChannel=replyChannel
inOut

The exchange pattern that the Spring integration endpoint should use.

The default is inOnly for the consumer and outOnly for the provider.

inOut=true
consumer.delay

Delay in milliseconds between each poll.

The default is 500.

consumer.delay=60000
consumer.initialDelay

Milliseconds before polling starts.

The default is 1000.

consumer.initialDelay=10000
consumer.userFixedDelay

Specify true to use fixed delay between polls, otherwise fixed rate is used. See the Java[ScheduledExecutorService|java.lang Class Character class.

The default is false.

consumer.userFixedDelay=false

Usage

The Spring integration component is a bridge that connects Apache 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.

Using the Spring integration endpoint

You can set up a Spring integration endpoint using a URI, as follows:

<beans:beans xmlns="http://www.springframework.org/schema/integration"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans.xsd
			http://www.springframework.org/schema/integration
			http://www.springframework.org/schema/integration/spring-integration.xsd
			http://camel.apache.org/schema/spring
			http://camel.apache.org/schema/spring/camel-spring.xsd">
	
	<channel id="inputChannel"/>
   	<channel id="outputChannel"/>
   	<channel id="onewayChannel"/>

	<service-activator input-channel="inputChannel"	          
	          ref="helloService"
	          method="sayHello"/>
	          	      
	<service-activator input-channel="onewayChannel"	          
	          ref="helloService"
	          method="greet"/>
	          
	<beans:bean id="helloService" class="org.apache.camel.component.spring.integration.HelloWorldService"/>
    
    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
      <route>
        <from uri="direct:twowayMessage"/>
        <!-- Using the &as the separator of & -->
        <to uri="spring-integration:inputChannel?inOut=true&nputChannel=outputChannel"/>
      </route>
      <route>      
        <from uri="direct:onewayMessage"/>
        <to uri="spring-integration:onewayChannel?inOut=false"/>
      </route>
    </camelContext>
<channel id="requestChannel"/>
<channel id="responseChannel"/>

<beans:bean id="myProcessor" class="org.apache.camel.component.spring.integration.MyProcessor"/>

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <route>
    <!-- Using the &as the separator of & -->
    <from uri="spring-integration://requestChannel?outputChannel=responseChannel&nOut=true"/>
    <process ref="myProcessor"/>
  </route>
</camelContext>

Or directly using a Spring integration channel name:

<beans:beans xmlns="http://www.springframework.org/schema/integration"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans.xsd
			http://www.springframework.org/schema/integration
			http://www.springframework.org/schema/integration/spring-integration.xsd
			http://camel.apache.org/schema/spring
			http://camel.apache.org/schema/spring/camel-spring.xsd">
	<channel id="outputChannel"/>

    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
      <route>
        <!-- camel will create a spring integration endpoint automatically -->
        <from uri="outputChannel"/>
        <to uri="mock:result"/>
      </route>
    </camelContext>

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 an Apache Camel endpoint or from an Apache Camel endpoint to a Spring integration channel.

This example uses the following namespaces:

<beans:beans xmlns="http://www.springframework.org/schema/integration"
       xmlns:beans="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel-si="http://camel.apache.org/schema/spring/integration"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/integration
	   http://www.springframework.org/schema/integration/spring-integration.xsd
       http://camel.apache.org/schema/spring/integration
       http://camel.apache.org/schema/spring/integration/camel-spring-integration.xsd
       http://camel.apache.org/schema/spring
       http://camel.apache.org/schema/spring/camel-spring.xsd
    ">

You can bind your source or target to an Apache Camel endpoint as follows:

<!-- Create the camel context here -->
<camelContext id="camelTargetContext" xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="direct:EndpointA" />
        <to uri="mock:result" />
    </route>
    <route>
        <from uri="direct:EndpointC"/>
        <process ref="myProcessor"/>
      </route>
</camelContext>

<!-- We can bind the camelTarget to the camel context's endpoint by specifying the camelEndpointUri attribute -->
<camel-si:camelTarget id="camelTargetA" camelEndpointUri="direct:EndpointA" expectReply="false">
    <camel-si:camelContextRef>camelTargetContext</camel-si:camelContextRef>
</camel-si:camelTarget>

<camel-si:camelTarget id="camelTargetB" camelEndpointUri="direct:EndpointC" replyChannel="channelC" expectReply="true">
    <camel-si:camelContextRef>camelTargetContext</camel-si:camelContextRef>
</camel-si:camelTarget>

<camel-si:camelTarget id="camelTargetD" camelEndpointUri="direct:EndpointC" expectReply="true">
    <camel-si:camelContextRef>camelTargetContext</camel-si:camelContextRef>
</camel-si:camelTarget>

<beans:bean id="myProcessor" class="org.apache.camel.component.spring.integration.MyProcessor"/>
<!-- spring integration channels -->
<channel id="channelA"/>
<channel id="channelB"/>
<channel id="channelC"/>
      
<!-- spring integration service activator -->
<service-activator input-channel="channelB" output-channel="channelC" ref="helloService" method="sayHello"/>
      
<!-- custom bean -->
<beans:bean id="helloService" class="org.apache.camel.component.spring.integration.HelloWorldService"/>
      
<camelContext id="camelSourceContext" xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="direct:OneWay"/>
        <to uri="direct:EndpointB" />
    </route>
    <route>
    	   <from uri="direct:TwoWay"/>
        <to uri="direct:EndpointC" />
    </route>
</camelContext>

<!-- camelSource will redirect the message coming for direct:EndpointB to the spring requestChannel channelA -->

<camel-si:camelSource id="camelSourceA" camelEndpointUri="direct:EndpointB"
                         requestChannel="channelA" expectReply="false">
    <camel-si:camelContextRef>camelSourceContext</camel-si:camelContextRef>
</camel-si:camelSource>

<!-- camelSource will redirect the message coming for direct:EndpointC to the spring requestChannel channelB
 then it will pull the response from channelC and put the response message back to direct:EndpointC -->

<camel-si:camelSource id="camelSourceB" camelEndpointUri="direct:EndpointC"
    requestChannel="channelB" replyChannel="channelC" expectReply="true">
    <camel-si:camelContextRef>camelSourceContext</camel-si:camelContextRef>
</camel-si:camelSource>