Simple Spring Integration

David Winterfeldt

2009


This example uses Spring Integration to process a book order and appropriately route the message depending on if it's a pickup from the store or it should be delivered. Spring Integration is an implementation of Enterprise Integration Patterns.

From the Order gateway, the BookOrder is sent to the 'processOrder' channel. An OrderRouter routes the order either to the 'pickup' or 'delivery' channels based on the order's OrderType annotation. If the order is a pickup, the 'pickup' channel is bridged to the 'store' channel which is configured to have the StoreEndpoint do the final processing for this part of the flow. If the order is a delivery, the DeliveryTransformer converts the BookOrder into an OnlineBookOrder that contains the delivery address. The address is just hard coded in the example, but could have looked up an address in a real application. The online order is sent to the 'post' channel, which is configured to have the PostEndpoint handle the end of this part of the flow.

Order Message Flow
            
           processOrder
                |
            OrderRouter
             /     \
        pickup     delivery
          |           |
        store    DeliveryTranformer
                      |
                     post
            
        

1. Spring Configuration

This configuration uses a combination of XML and classes with annotations to configure the message flow. The annotation-config element enables annotation-based configuration for Spring Integration. The context:component-scan loads the annotated part of the configuration. The gateway element creates the Order gateway, which is the beginning of the flow. Different channels are created, a bridge between two channels using the bridge element, and the outbound-channel-adapter is used to configure an endpoint for the 'store' and 'post' channels.

OrderTest-context.xml
                
<?xml version="1.0" encoding="UTF-8"?>
<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"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:stream="http://www.springframework.org/schema/integration/stream"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
                                 http://www.springframework.org/schema/beans/spring-beans.xsd
                                 http://www.springframework.org/schema/context
                                 http://www.springframework.org/schema/context/spring-context.xsd
                                 http://www.springframework.org/schema/integration
                                 http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
                                 http://www.springframework.org/schema/integration/stream
                                 http://www.springframework.org/schema/integration/stream/spring-integration-stream-1.0.xsd">

    <!-- 
           processOrder
                |
            OrderRouter
             /     \
        pickup     delivery
          |           |
        store    DeliveryTranformer
                      |
                     post
    -->
    
    <annotation-config/>

    <context:component-scan base-package="org.springbyexample.integration.book.annotation"/>

    <gateway id="order" service-interface="org.springbyexample.integration.book.Order"/>
    
    <channel id="processOrder"/>
    
    <channel id="delivery"/>
    <channel id="pickup"/>
    
    <bridge input-channel="pickup" output-channel="store" />
    
    <channel id="store"/>
    <channel id="post"/>
    
    <outbound-channel-adapter channel="store" ref="storeEndpoint" method="processMessage" />
    <outbound-channel-adapter channel="post" ref="postEndpoint" method="processMessage" />
    
</beans:beans>