Spring Integration嵌套链与过滤器

时间:2017-09-07 15:22:31

标签: java spring spring-integration

我有一系列处理步骤,我试图处理,但我的测试并不总是通过,而不是总是失败......

我制作了一个简单的例子,它似乎展示了我在实际考试中所看到的内容。

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import java.util.Arrays;
import java.util.List;

import org.springframework.integration.test.support.AbstractRequestResponseScenarioTests;
import org.springframework.integration.test.support.PayloadValidator;
import org.springframework.integration.test.support.RequestResponseScenario;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration
public class FlowTest extends AbstractRequestResponseScenarioTests {

    @Override
    protected List<RequestResponseScenario> defineRequestResponseScenarios() {
        return Arrays.asList(
                new RequestResponseScenario("inputChannel", "outputChannel")
                    .setPayload("a")
                    .setResponseValidator(new PayloadValidator<String>() {
                        @Override
                        protected void validateResponse(String response) {
                            assertEquals("AA", response);
                        }
                    }),
                new RequestResponseScenario("inputChannel", "outputChannel")
                    .setPayload("b")
                    .setResponseValidator(new PayloadValidator<String>() {
                        @Override
                        protected void validateResponse(String response) {
                            assertNotEquals("AA", response);
                        }
                    })
                );
    }
}

和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"
    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.xsd">

    <int:channel id="inputChannel"/>
    <int:channel id="outputChannel"/>
    <int:channel id="errorChannel"/>

    <int:chain input-channel="inputChannel" output-channel="outputChannel">
        <int:gateway request-channel="processOne"/>
        <int:gateway request-channel="processTwo"/>
    </int:chain>

    <int:chain input-channel="processOne">
        <int:service-activator expression="payload + payload"/>
        <int:filter expression="payload == 'aa'" discard-channel="errorChannel"/>
    </int:chain>

    <int:chain input-channel="processTwo">
        <int:service-activator expression="payload.toUpperCase()"/>
    </int:chain>

    <int:transformer input-channel="errorChannel" output-channel="outputChannel" expression="payload + payload"/>
</beans>

似乎消息流正在发生,消息总是出现在responseChannel上,并由PayloadValidator处理。

但是,如果一条消息恰好通过错误处理绕行,那么应用程序上下文就不会关闭,它就会停滞不前。

是否支持这种模式,使用过滤器将消息从链中拉出到备用路径上?

我猜测链正在等待回复消息,并且某些相关性在途中丢失。

我使用的是相当古老的Spring Integration版本: - (

更新

看起来这似乎是使用嵌套链的效果。如果我将过滤器移动到顶级链中,事情就会按预期工作。哪种方式有意义,因为它是等待响应的网关。

我使用嵌套链,因为我需要单独的进程来单独处理重试逻辑。

也许我只需要放弃链条的语法糖?

1 个答案:

答案 0 :(得分:0)

不,一切都是正确的。

只有您真正在等待<int:gateway/>中的回复的问题才会发生,因为您转到discard-channel

您可以考虑使用:

     <xsd:attribute name="reply-timeout" type="xsd:string">
        <xsd:annotation>
            <xsd:documentation>
                <![CDATA[
                Specifies how long this gateway will wait for the reply message
                before returning. By default it will wait indefinitely. 'null' is returned
                if the gateway times out.
                Value is specified in milliseconds; it can be a simple long value or a SpEL
                expression; array variable '#args' is available.
                Also used for receive-only operations as the receive timeout.
                ]]>
            </xsd:documentation>
        </xsd:annotation>
    </xsd:attribute>

<gateway>

 <xsd:attribute name="throw-exception-on-rejection" default="false">
                <xsd:annotation>
                    <xsd:documentation>
                    Throw an exception if the filter rejects the message (default false).
                    </xsd:documentation>
                </xsd:annotation>
                <xsd:simpleType>
                    <xsd:union memberTypes="xsd:boolean xsd:string" />
                </xsd:simpleType>
            </xsd:attribute>

位于<filter>上,因此error-channel上的<gateway>代替discardChannel

这不是一种如何处理请求 - 回复网关的方式,而是你现在如何做到这一点。