1

Currently in our project we are using spring framework. We are planning to implement Spring Integration framework in our project because of some project requirement.

I was going throw Spring Integration Sample(Spring Integration Rest HTTP Path Usage Demo) appications

below is applicationContext-http-int.xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd      
    http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd   
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:oxm="http://www.springframework.org/schema/oxm" 
xmlns:int-http="http://www.springframework.org/schema/integration/http">

<int:annotation-config/>

<!-- handler mapping implementation that is aware of inbound Spring Integration 
        http inbound gateway's and inbound adapter's with "path" attributes -->
<bean class="org.springframework.integration.http.inbound.UriPathHandlerMapping"/>

<!-- Inbound/Outbound Channels -->
<int:channel id="employeeSearchRequest" />
<int:channel id="employeeSearchResponse" />


<int-http:inbound-gateway id="inboundEmployeeSearchRequestGateway"      
    supported-methods="GET, POST" 
    request-channel="employeeSearchRequest"
    reply-channel="employeeSearchResponse"      
    mapped-response-headers="Return-Status, Return-Status-Msg, HTTP_RESPONSE_HEADERS"       
    view-name="/employee" 
    path="/services/employee/{id}/search"
    reply-timeout="50000">

    <int-http:header name="employeeId" expression="#pathVariables.id"/>

</int-http:inbound-gateway>


<!-- Note: The default parameter name for favorParameter is 'format'. For instance, when this flag is true, a request for /services/employee/{id}/search?format=json will result 
        in an MappingJacksonJsonView being resolved, while the Accept header can be the browser-defined text/html,application/xhtml+xml  -->

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1" /> 
    <property name="defaultContentType" value="application/xml"/>
    <property name="favorParameter" value="true"/>  
    <property name="ignoreAcceptHeader" value="true" />     
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />             
        </map>
    </property>
    <property name="defaultViews">
        <list>
            <bean
                class="org.springframework.integration.samples.rest.json.view.ExtendedMappingJacksonJsonView" >
                <property name="objectMapper" ref="jaxbJacksonObjectMapper"/>
            </bean> 
            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <constructor-arg ref="marshaller"/>                 
            </bean>             
        </list>
    </property>             
</bean>

<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.integration.samples.rest.domain" />

<int:service-activator id="employeeServiceActivator" 
                input-channel="employeeSearchRequest"
                output-channel="employeeSearchResponse" 
                ref="employeeSearchService" 
                method="getEmployee" 
                requires-reply="true"  
                send-timeout="60000"/>

<bean id="jaxbJacksonObjectMapper" class="org.springframework.integration.samples.rest.json.JaxbJacksonObjectMapper"/>              

As per my understanding flow is like when there is message in input channel employeeSearchService will be activated. But as per our project requirement we need activate service at runtime based on some header value like

  • if service name = LoginService and method name = action than Service Activator should activate LoginService and call action method. based on url pattern
  • For example if my url is like http://ipaddress:8080/myapp/LoginService(is ServiceName).action(is method name)than LoginService should be activated and action method should be called.

Any suggestion and help will be appreciate as SI is new for me.

1 Answer 1

2

there are a couple of ways to answer this one. the first is to use a simple header value

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/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-2.2.xsd">

    <!-- input channel where the message starts -->
    <int:channel id="input.channel"/>

    <!-- routes to the different services based on the header value -->
    <int:header-value-router input-channel="input.channel" header-name="serviceName">
        <int:mapping value="a" channel="service.a.channel"/>
        <int:mapping value="b" channel="service.b.channel"/>
    </int:header-value-router>

    <!-- when serviceName header == 'a' -->
    <int:channel id="service.a.channel"/>

    <int:service-activator input-channel="service.a.channel" ref="serviceA"/>

    <!-- when serviceName == 'b' -->
    <int:channel id="service.b.channel"/>

    <int:service-activator input-channel="service.b.channel" ref="serviceB"/>
</beans>

this example allows you to expand according to the different services you may require and multiple options.

(input.channel would be the same as your employeeSearchRequest)

the other option uses SpEL and assumes there's only two services

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/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-2.2.xsd">

    <int:channel id="input.channel"/>

    <int:service-activator input-channel="input.channel"
        expression="headers['serviceName'] == 'a' ? @serviceA.process(payload) : @serviceB.process(payload)"/>

</beans>
3
  • This is right for 2 services, but what if I have plenty of services but have only one single service activator and one input channel and I need to dynamically call service based on servicename set in header?
    – Ketan
    Apr 26, 2013 at 6:13
  • 1
    have a look at this on spring forums; forum.springsource.org/… May 30, 2013 at 14:17
  • thnx...luckily I also had gone through that link month ago and implemented solution
    – Ketan
    Jun 3, 2013 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.