首页 > 解决方案 > How to send response from a Gateway in spring-integration

问题描述

I'm following the sample below to implement spring-integration gateway.

Issue: Not able to get response on the Gateway which is sent from a service-activator.

  1. Gateway reply-channel is "bestQuoteResponseChannel".
  2. Service-activator writes to the same channel.

All the custom methods are executed and response is composed but never gets sent out. Any idea what's missing here? My configuration also has a scatter-gather in the flow.

<!--Gateway:-->
<channel id="bestQuoteResponseChannel"/>
<gateway id="loanBrokerGateway"
        default-request-channel="loanRequestsChannel"
        service-interface="org.springframework.integration.samples.loanbroker.LoanBrokerGateway">
    <method name="getBestLoanQuote" reply-channel="bestQuoteResponseChannel">
        <header name="RESPONSE_TYPE" value="BEST"/>
    </method>
</gateway>

<chain input-channel="loanRequestsChannel">
    <header-enricher>
        <header name="creditScore" expression="@creditBureau.getCreditReport(payload).score"/>
    </header-enricher>
    <recipient-list-router apply-sequence="true">
        <recipient selector-expression="headers.creditScore > 800" channel="exclusiveBankChannel"/>
        <recipient selector-expression="headers.creditScore > 750" channel="premiereBankChannel"/>
        <recipient selector-expression="headers.creditScore > 700" channel="qualityBankChannel"/>
        <recipient selector-expression="headers.creditScore > 650" channel="friendlyBankChannel"/>
        <recipient channel="easyBankChannel"/>
    </recipient-list-router>
</chain>

<!-- Messages are sent to the banks via bank channels and will be received and processed by an aggregator -->

<aggregator input-channel="loanQuotesChannel" output-channel="input" method="aggregateQuotes">
    <beans:bean class="org.springframework.integration.samples.loanbroker.LoanQuoteAggregator"/>
</aggregator>

<service-activator ref="findBestQuoteService" method="findBestQuote" input-channel="input" output-channel="bestQuoteResponseChannel"/>

标签: springspring-integration

解决方案


首先,对于这种情况,您不需要reply-channel网关上的 a ,因此您不需要output-channel在服务激活器上使用它。当调用网关方法时,所有回复逻辑都应该放在replyChannel框架始终填充的标头上。

很高兴看到你findBestQuoteService.findBestQuote做了什么,因为LoanQuoteAggregator它已经为我们做了一切:

/**
 * Aggregates LoanQuote Messages to return a single reply Message.
 *
 * @param quotes list of loan quotes received from upstream lenders
 * @param responseType header that indicates the response type
 * @return the best {@link LoanQuote} if the 'RESPONSE_TYPE' header value is 'BEST' else all quotes
 */
public Object aggregateQuotes(List<LoanQuote> quotes,
        @Header(value="RESPONSE_TYPE", required=false) String responseType) {
    Collections.sort(quotes);
    return ("BEST".equals(responseType)) ? quotes.get(0) : quotes;
}

那么,此后您的定制服务有什么意义呢?

还可以考虑为该org.springframework.integration类别打开 DEBUG 级别,以查看您的消息是如何传播的。可能我们也会在日志中看到一些问题。

我看不出您的逻辑如何丢失replyChannel标题,但我们需要更多信息进行调查。


推荐阅读