弹簧整合终点

时间:2015-06-10 21:38:09

标签: spring-integration

第一篇文章。我是Spring Integration的新手,有以下几种情况: 我有一个服务," xyzSearchService.getXYZ",它是作为http入站请求的结果而调用的。 它做了一些逻辑然后调用" routingChannel"反过来调用谷歌地理编码服务。

我希望谷歌的回复返回xyzSearchService.getXYZ,但我不知道如何在" outboundGateway"中配置replyChannel。现在我将它转到同一服务中的另一个方法,我可以看到结果,但我希望它返回到调用它的服务/方法。不知道如何配置我的最终出站通道适配器?

   <int-http:inbound-gateway id="inboundXYZSearchRequestGateway"
                              supported-methods="GET, POST"
                              request-channel="xyzSearchRequest"
                              reply-channel="xyzSearchResponse"
                              mapped-response-headers="Return-Status, Return-Status-Msg, HTTP_RESPONSE_HEADERS"
                              view-name="/xyz"
                              path="/services/xyz/zip/{zipcode}/search"
                              reply-timeout="50000">

        <int-http:header name="zipcode" expression="#pathVariables.zipcode"/>
    </int-http:inbound-gateway>

    <int:service-activator id="xyzServiceActivator"
                           input-channel="xyzSearchRequest"
                           output-channel="xyzSearchResponse"
                           ref="xyzSearchService"
                           method="getXYZ"
                           requires-reply="true"
                           send-timeout="60000"/>

//the service activator method does some logic and invokes the "routingChannel"

    <int:chain input-channel="routingChannel">
        <int:router  
                    expression="payload.serviceType"  
                    default-output-channel="channel_default"  
                    resolution-required="false">
        </int:router>
    </int:chain>

    <int-http:outbound-gateway  id="outboundGateway"
                                url="http://maps.googleapis.com/maps/api/geocode/json?address={zipCode}"
                                http-method="GET"
                                request-factory="requestFactory"
                                request-channel="restchannel"
                                reply-channel="channel2"
                                expected-response-type="java.lang.String">
        <int-http:uri-variable name="zipCode" `enter code `enter code here`here`expression="payload.data['zipcode']"/>
    </int-http:outbound-gateway>


    <int:channel id="channel2"/>
    <int:outbound-channel-adapter channel="channel2" ref="xyzSearchService" method="routeUnit" />

修改

您是否建议我创建一个入站网关,如下所示,并从我的服务方法中调用它?

<int-http:inbound-gateway id="inboundNewZipRequestGateway"
                          supported-methods="GET, POST"
                          request-channel="zipRoutingChannel"
                          reply-channel="zipSearchResponse"
                          mapped-response-headers="Return-Status, Return-Status-Msg, HTTP_RESPONSE_HEADERS"
                          view-name="/zip"
                          path="/services/zip/zipcode/{zipcode}/search"
                          reply-timeout="50000">
    <int-http:header name="zipcode" expression="#pathVariables.zipcode"/>
</int-http:inbound-gateway>

编辑:

我已按如下方式添加了网关:

<int:gateway id="toHttp" service-interface="com.....domain.MyGW"
 default-request-channel="routingChannel"
 default-reply-timeout="55550" ></int:gateway>

autowire应该如下所示?:

@Autowired
private MyGW toHttp;

@Autowired
private MyGW gateway;

我使用了前者:

Message<?> reply = toHttp.callHttp(inMessage);

我不认为它正在接收routingChannel。

1 个答案:

答案 0 :(得分:0)

正确的解决方案是不直接发送到路由通道,而是使用Messaging Gateway并将<gateway/>发送到路由通道。

由于您可能会或可能不会返回结果(取决于路由),您应该设置default-reply-timeout=0,并且在这种情况下您将从网关方法返回null。

或者,使用MessagingTemplate及其sendAndReceive()方法之一发送到路由通道 - 如果您没有得到回复,则还需要回复超时为零。

编辑:

不,我在暗示......

<gateway id="toHttp" service-interface="foo.MyGW
     default-request-channel="routingChannel"
     default-reply-timeout="0" />

public interface MyGW {
    public Message<?> callHttp(Message<?> message);
}

而且,在xyzServiceActivator

@Autowired
private MyGW gateway;

...

    Message<?> reply = myGW.callHttp(m);
...

或者...

@Autowired
private MessageChannel routerChannel;


...
    MessagingTemplate template = new MessagingTemplate(routerChannel);
    template.setReceiveTimeout(0);
...
    Message<?> reply = template.sendAndReceive(m);

    Object reply = template.convertSendAndReceive(somePayload);

(后者让框架负责转换到/来自消息)。