来自JMS Request / Reply with ListenableFuture的DeferredResult

时间:2015-10-15 19:33:41

标签: spring spring-mvc spring-integration

我一直在寻找机会来改善我们向外部客户提供的Rest Api。在这个练习中,我发现我几乎没有利用我们的集成团队采用后端与JMS请求/回复集成的决定,而不是传统的阻塞SOAP请求/回复。

目前与消息代理的所有交互都是使用jmsOutboundGateway完成的,因为请求线程必须等待完成。为了扩展RestAPI,我想使用Spring MVC控制器中的DeferredResult发送JMS回复。控制器与消息代理的交互如下所示:

                Controller --> GatewayProxy --> JMSOutboundGateway

我正在寻找机会使用ListenableFuture作为GatewayProxy的返回类型,但我无法找到使用spring集成实现它的正确方法。

以下是我从控制器调用的集​​成流程:

<int:gateway 
        service-interface="ae.emaratech.ngx.service.PermitSearchService" 
        default-request-channel="permit_search_input_channel" 
        default-reply-timeout="${broker.jms.gateway.min.consumers}"/>


    <int:channel id="permit_search_input_channel" />

    <int:chain input-channel="permit_search_input_channel">


    <int:header-enricher>
        <int:header name="person_number" expression="payload"/>
    </int:header-enricher>


    <int:transformer expression="#formatString(@api_messages['FIND_PERM_BY_PERSNO_MSG'],headers)"/>

    <int:header-filter header-names="JMS_*,jms_*,priority" pattern-match="true" />


    <int:header-enricher>
        <int:header name="jms_type" type="java.lang.String" value="1" overwrite="true"/>
    </int:header-enricher>


    <jms:outbound-gateway 
        request-destination="permitsInboundQueue"
        reply-destination="permitsOutboundQueue" 
        receive-timeout="${broker.jms.gateway.timeout}"
        correlation-key="Correlation_ID" 
        connection-factory="brokerConnectionFactory">

        <jms:reply-listener concurrent-consumers="${broker.jms.gateway.min.consumers}"  max-concurrent-consumers="${broker.jms.gateway.max.consumers}"/>

    </jms:outbound-gateway>


    <int-xml:xpath-filter throw-exception-on-rejection="true">
        <int-xml:xpath-expression expression="not(boolean(/*/ErrorDetails))"/>
    </int-xml:xpath-filter>             

    <int-xml:xslt-transformer
        xsl-resource="classpath:/META-INF/spring/integration/permit-to-json.xsl"
        result-type="StringResult" >
        </int-xml:xslt-transformer>

    <int:transformer expression="payload.toString()"/>


</int:chain> 

1 个答案:

答案 0 :(得分:2)

不确定您遇到了什么问题,但功能如下:

ListenableFuture<String> result = this.asyncGateway.async("foo");
result.addCallback(new ListenableFutureCallback<String>() {

    @Override
    public void onSuccess(String result) {
        ...
    }

    @Override
    public void onFailure(Throwable t) {
        ...
    }
});

自版本4.1起可用。