【问题标题】:How to send response from a Gateway in spring-integration如何在spring-integration中从网关发送响应
【发布时间】:2020-10-13 19:07:37
【问题描述】:

我正在按照下面的示例来实现 spring-integration 网关。

问题:无法在网关上获得从服务激活器发送的响应。

  1. 网关回复通道是“bestQuoteResponseChannel”。
  2. 服务激活器写入同一通道。

所有的自定义方法都被执行并且响应被组合但永远不会被发送出去。知道这里缺少什么吗? 我的配置在流程中也有一个分散聚集。

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

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

    首先对于这种情况,您不需要网关上的reply-channel,因此您不需要服务激活器上的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 类别打开调试级别,以查看您的消息是如何传播的。可能我们也会在日志中看到一些问题。

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

    【讨论】:

    • 感谢您的及时回复。我正在使用聚合器来构建列表。然后聚合器将其提供给具有自定义逻辑的“服务激活器”,以根据某些参数过滤出最好的。如何将响应作为网关响应传递?
    • 好的。这是一点。其他人呢?可能 DEBUG 日志会很有帮助。
    • 如何在 xml 上启用调试以查看所有消息?这是配置吗?
    • 只需将您的日志配置为org.springframework.integration 类别以在DEBUG 级别下登录。例如对于 Log4J 2,它是这样的:&lt;Logger name="org.springframework.integration" level="debug"/&gt;
    • 好吧,看看能不能升级到最新的5.3.1:spring.io/projects/spring-integration#learn!在那个太旧的5.0.1... 之后,确实有一些针对 Scatter-Gather 的修复
    【解决方案2】:

    在向网关发送回复之前,我在 spring-integration 流程中有scatter-gather。这以某种方式阻止它向网关发送响应。

    所做的更改: 我用publish-subscribe channelaggregator 组合替换了scatter-gather 组件。 更改后效果很好。

    https://docs.spring.io/spring-integration/docs/5.3.0.M1/reference/html/scatter-gather.html#scatter-gather-namespace

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2011-05-03
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多