完成任何/所有服务激活器后的输出和错误通道

时间:2018-12-20 16:17:37

标签: spring-integration spring-integration-dsl

我想在服务任何/所有服务激活器执行完毕后执行一些操作。流程定义如下:

@Bean
public IntegrationFlow myFlow(MessageSource<Object> someMessageSource) {
    return IntegrationFlows.from(someMessageSource,
            c -> c.poller(Pollers.fixedRate(1, SECONDS)
                    .maxMessagesPerPoll(10)))
            .channel(mySourceChannel())
            .get();
}

@Bean
public MessageChannel mySourceChannel() {
    return new ExecutorChannel(executor());
}

两个服务激活器定义为

public static class MyFooActivator {  
    @ServiceActivator(inputChannel = "mySourceChannel")
    public void doSomething(FooTask event) {
      //handle foo task
    }
}

public static class MyBarActivator { 
    @ServiceActivator(inputChannel = "mySourceChannel")
    public void doSomething(BarTask event) {
        // handle bar task
    }
}

如您所见,服务激活器未定义为集成流程的一部分,而是通过注释定义的。目标是在1)服务激活程序执行成功时执行一些代码。2)服务激活程序执行失败时。

如果流程已知/定义了服务执行器,则可以通过定义另一个流程并添加输出通道和错误来实现。不确定如何在我的情况下实现相同效果。

1 个答案:

答案 0 :(得分:2)

首先,您需要了解mySourceChannel的订阅者,当第一个消息发送到第一个服务激活器和下一个服务激活器时,您将在它们之间进行循环平衡。一到二。以此类推。 因此,除了这种配置之外,您应该确定这就是您所要的。

要对服务激活器的执行结果进行一些分析,您需要使用ExpressionEvaluatingRequestHandlerAdvice。它具有successChannelfailureChannel选项,并在需要时提供适当的表达式。

您可以将对其bean的引用注入adviceChain的{​​{1}}属性中。像这样:

@ServiceActivator

更多信息,请参见参考手册:https://docs.spring.io/spring-integration/reference/html/messaging-endpoints-chapter.html#expression-advice