在Spring Integration DSL服务激活器中丢弃消息

时间:2020-02-11 22:39:32

标签: spring spring-boot spring-integration spring-integration-dsl

有没有一种方法可以丢弃使用Spring Integration DSL .handle()方法定义的Spring Integration Service Activator中的消息?

更具体地说,假设下面的IntegrationFlow定义...

public IntegrationFlow sampleFlow() {
    return IntegrationFlows
             .from("someChannel")
             .handle(someServiceClass::someMethod)
             .get();
}

是否有一种方法可以根据someMethod定义中的某些评估丢弃一条消息?理想情况下,我想执行一些日志记录然后丢弃。

我发现从null返回someMethod似乎有效地结束了流程,但是我不确定这是否是最合适/最正确的解决方案。如果这是推荐的解决方案,那很好。

1 个答案:

答案 0 :(得分:2)

是的。停止从服务方法返回null的处理实际上是一种非正式的模式。我们甚至以非官方的方式对其进行了记录:https://docs.spring.io/spring-integration/docs/5.2.3.RELEASE/reference/html/messaging-endpoints.html#service-activator-namespace

服务激活器是不需要生成回复消息的那些组件之一。如果您的方法返回null或返回类型为void,则服务激活器将在方法调用后退出,而不会发出任何信号。

您还可以使用filter()来实现“丢弃并记录”的目标。