【问题标题】:Add adviceChain to StandardIntegrationFlow to set a CountDownLatch on a message handler将 adviceChain 添加到 StandardIntegrationFlow 以在消息处理程序上设置 CountDownLatch
【发布时间】:2017-09-25 15:42:38
【问题描述】:

我正在尝试通过 adviceChainBeanFactoryPostProcessor 在 Spring Integration 中将 CountDownLatch 添加到 org.springframework.integration.dsl.StandardIntegrationFlow。这样做的原因是为了监视这个集成流中的消息处理程序是否被调用并完成了他的工作。

这里有一个不使用 Spring Integration 的 Java DSL 的解决方案:spring-integration unit test outbound-channel adapter。但遗憾的是,由于 Java DSL 和 StandardIntegrationFlow,它对我不起作用。 Gary Russels 解决方案如下所示:https://gist.github.com/garyrussell/5481779。我的例外是:

Caused by: org.springframework.beans.NotWritablePropertyException: 
Invalid property 'adviceChain' of bean class [org.springframework.integration.dsl.StandardIntegrationFlow]: 
Bean property 'adviceChain' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

集成流程如下所示:

@Bean
public IntegrationFlow myFlow() {
    return IntegrationFlows.from(myChannel())
            .handle(message -> manager.handleMessage(message))
            .get();
}

我需要将CountDownLatch 添加到.handle() 部分,这样我就可以询问latch 处理程序是否已完成工作。

任何想法如何实现这一目标?

【问题讨论】:

    标签: spring unit-testing spring-integration spring-integration-dsl


    【解决方案1】:

    IntegrationFlow 基本上对集成没有任何作用。它是真正的 Spring Integration 端点和它们之间的消息通道的逻辑容器。所以,如果你说的是针对.handle() 的建议,真正正确的是什么,你应该考虑这样做:

    .handle(message -> manager.handleMessage(message), e -> e.advice(...))
    

    advice() 在哪里:

    /**
     * Configure a list of {@link Advice} objects to be applied, in nested order, to the
     * endpoint's handler. The advice objects are applied only to the handler.
     * @param advice the advice chain.
     * @return the endpoint spec.
     */
    public S advice(Advice... advice) {
    

    【讨论】:

    • 这当然是最简单的方法;您可以使用e.advice(advice()),其中advice() bean 是用于生产的空数组,但闩锁建议用于测试。避免.advice(...) 并仅为测试用例添加建议有点棘手;使用BeanPostProcessor 很容易获得简单的MessageHandler,例如问题中的那个;对于AbstractReplyProducingMessageHandler,由于必须在内部应用建议,BPP 必须在初始化之前将建议注入 bean。
    • 出于测试目的考虑使用新添加的测试功能:docs.spring.io/spring-integration/docs/5.0.0.M6/reference/html/…,如果您可以在目标测试中将其替换为mockMessageHandler(),则无需考虑建议-案例。
    • 谢谢你们!我将尝试新的 M6 测试功能。但万一它对我们不起作用@GaryRussell:您将如何使用 BPP 添加建议?我不喜欢在我的生产代码中引入仅测试代码的想法......
    • @ArtemBilan 使用 mockIntegrationContext.substituteMessageHandlerFor() 时,我需要提供消费者的 bean 名称(用模拟替换),但由于我使用的是 Java DSL,我不确定把哪个名字放在这里。我测试了一些,但每次尝试都会导致Bean named 'xyz' is expected to be of type 'org.springframework.integration.endpoint.IntegrationConsumer'。如何替换上面问题中的示例中所示的消息处理程序?
    • 我的示例中的e 有一个id() 选项。这根据您的 MessageHandler 定义了消费者端点的 bean 名称
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多