【问题标题】:Spring Integration Flow: filter based on headers without SpEl expressionSpring Integration Flow:基于没有 SpEl 表达式的标头进行过滤
【发布时间】:2019-07-20 13:28:15
【问题描述】:

我有以下 Spring 集成流程:

@Bean
public IntegrationFlow checkoutEventFlow(){
    return IntegrationFlows.from(EventASink.INPUT)
        .filter("headers['type'] == 'TYPE_A'") //1
        .transform(Transformers.fromJson(EventA.class)) //2
        .<EventA, EventB> transform(eventA ->
            new EventB(
                eventA.getSomeField(),
                eventB.getOtherField()))
        .handle(Http.outboundGateway(uri).httpMethod(HttpMethod.POST))
        .get();
}

1) 我想在不使用 SpEl 表达式的情况下根据其标头过滤消息(查看 //1),可以吗?

2) 没有//2 是否有另一种将JSON 转换为POJO 的机制?我喜欢用 POJO 编写 @StreamListener 的方式,并且转换是在幕后完成的。

提前致谢。

【问题讨论】:

    标签: spring spring-integration spring-cloud-stream


    【解决方案1】:

    如果filter() 中没有 SpEL,您可以改为使用 Java lambda:

    .filter(Message.class, m -> m.getHeaders().get("type") == "TYPE_A")
    

    Spring Cloud Stream 是自以为是的框架,其中 JSON 作为默认内容类型,用于在流中传输并进出目标消息传递系统的数据。

    Spring Integration 是一个可让您构建集成应用程序的库。在那里,我们对某些默认内容类型转换没有任何意见。没有任何开箱即用的猜测,因为 JSON,您要将传入的 byte[] 转换为一些 POJO。尽管为了尊重一些与 Spring Cloud Stream 一样可见的可能性,我们在 POJO 方法调用程序中有一个钩子,用于从 JSON 转换为预期的 POJO。但这仅适用于自定义 POJO 方法,当它们也标有 @serviceActivator 时。从这里我们不能假设您对 .transform() lambda 的期望。您需要有一些带有方法的服务并在以下位置使用它:

    /**
     * Populate a {@link ServiceActivatingHandler} for the
     * {@link org.springframework.integration.handler.MethodInvokingMessageProcessor}
     * to invoke the {@code method} for provided {@code bean} at runtime.
     * In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
     * @param service the service object to use.
     * @param methodName the method to invoke.
     * @return the current {@link IntegrationFlowDefinition}.
     */
    public B handle(Object service, String methodName) {
    

    这样,它的工作方式与您在 Spring Cloud Stream 中看到的@StreamListener 相同。

    【讨论】:

      猜你喜欢
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多