当前位置: 首页 > 知识库问答 >
问题:

如何在spring集成中动态调用Service Activator

南宫星波
2023-03-14

目前在我们的项目中,我们正在使用spring框架。由于一些项目需求,我们计划在我们的项目中实现Spring集成框架。

我打算抛出Spring集成示例(Spring集成Rest HTTP路径使用演示)应用程序

下面是应用程序Context-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"/>              

根据我的理解,流程就像当输入通道中有消息时,员工搜索服务将被激活。但是根据我们的项目要求,我们需要在运行时根据一些头值激活服务,比如

  • 如果服务名称=LoginService和方法名称=action,则Service Activator应该激活LoginService并调用action method。
  • 例如,如果我的url是像超文本传输协议://ip地址: 8080/myapp/LoginService(是ServiceName). action(是方法名),那么LoginService应该被激活,并且操作方法应该被调用。

任何建议和帮助将不胜感激,因为SI对我来说是新的。

共有1个答案

戚翼
2023-03-14

有几种方法可以回答这个问题。第一个是使用一个简单的头值

<?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相同)

另一个选项使用SpEL,并假设只有两个服务

<?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>
 类似资料:
  • 如何在我的项目中动态创建以轮询和检索服务器中的文件?

  • null 如何在transform()步骤中添加Jaxb2Marshaller?

  • 如果其他人已经解决了这个问题,那就只是寻找一些信息。我想同时使用Spring集成和Spring批处理。这两个都是SpringBoot应用程序,理想情况下,我希望将它们和各自的配置分开,这样它们都是自己的可执行jar。我在自己的进程空间中执行它们时遇到问题,我相信我希望,除非有人能说服我,否则,每个程序都能像自己的Spring Boot应用程序一样运行,并用自己的配置文件和属性初始化自己。不过,我遇

  • 我想让我的spring集成流通用于通过网关发送的不同类型的请求,我想拥有重载方法,并想为通过网关发送的特定消息调用特定的重载方法。 类似于上面的代码,在validatorService中,我有几个重载方法来满足这个流中的不同请求。但是,如果在有效载荷的同时,我想向一个方法发送另一个参数,该怎么办。那我该怎么做呢。 //网关 因此,如果你在<代码>流中看到。handle(validatorServi

  • 我学习了Spring Web Services入门教程,并编写了一个示例Web应用程序,该应用程序在上动态生成WSDL,endpoint在上服务请求,到目前为止还不错。 现在,我将webapp转换为Spring Boot应用程序:我添加了必要的依赖项,在带有endpoint的包上面的包中创建了一个注释类,并且endpoint实现仍然会回复请求。 但是我不能再从现有的XSD中获得生成的WSDL。 由

  • 基于spring集成的转换器使用来自一个系统的消息,检查、转换并将其发送到另一个系统。 如果目标系统关闭,我们将停止入站适配器,但也希望在本地保留或转发当前“正在运行”的转换消息。为此,只需将消息从正常输出通道动态地重新路由到某个“备份”通道。 在文档中,我只找到了根据消息头路由消息的选项(因此,在流程之前的某个步骤中,一旦targer系统不可用,我将不得不动态添加这些消息),或者基于有效负载类型