2013-04-22 78 views
1

目前在我們的項目中我們使用的是spring框架。由於某些項目需求,我們計劃在項目中實施Spring Integration框架。如何在春季動態調用服務激活器集成

我正想扔Spring集成樣品(Spring集成REST HTTP路徑使用演示)appications

下面

是的applicationContext-HTTP-int.xml文件

<?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"/>    

按我的理解流程就像是當輸入通道中有消息時,employeeSearchService將被激活。 但按我們的項目需求,我們需要激活基於像

  • 一些頭值在運行時服務,如果服務名稱= login服務和方法名稱=超過 服務激活動作應該激活login服務和調用操作方法。 基於url模式
  • 例如 如果我的url類似於http:// ipaddress:8080/myapp/LoginService(是ServiceName).action(是方法名稱),那麼應該激活LoginService並且應該調用Action方法。

任何建議和幫助將不勝感激,因爲SI對我來說是新的。

回答

1

有幾種方法可以回答這個問題。首先是使用簡單的標題值

<?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> 

這個例子允許你根據你可能需要的不同服務和多個選項進行擴展。

(input.channel將與您的employeeSearchRequest)

其他選項使用規劃環境地政司,並假定這裏只有兩個服務

<?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> 
+0

這是對2個服務,但如果我有足夠的的服務,但只有一個單一的服務激活器和一個輸入通道,我需要動態調用基於服務名稱**設置在頭中的服務? – Ketan 2013-04-26 06:13:35

+1

在春季論壇上看看這個; http://forum.springsource.org/showthread.php?115669-Dynamic-Methods-for-Service-Activator&highlight=dynamic+method – 2013-05-30 14:17:11

+0

thnx ...幸運的是,我也經歷了一個月前的鏈接,並實施瞭解決方案 – Ketan 2013-06-03 06:26:31