【问题标题】:Spring Integration - Service Activator in Java DSLSpring Integration - Java DSL 中的服务激活器
【发布时间】:2021-06-26 06:49:06
【问题描述】:

我在一个简单的 POJO 中配置了一个服务激活器,我想将它转换为 Java DSL。

现在,我的 Java DSL 看起来像这样,

public IntegrationFlow inputFlow() {
    return IntegrationFlows.from(inputChannel())
            .log(LoggingHandler.Level.DEBUG, "com.dash.messages")
            .transform(Transformers.fromJson(MessageWrapper.class, customObjectMapper()))
            .channel(theOtherChannel()))
            .get();
  }

有一个 POJO 里面有一个服务激活器,

public class MessageProcessor {

  private static final Logger logger = LoggerFactory.getLogger(MessageProcessor.class);

  ....

  @ServiceActivator
  public void handle(MessageWrapper message, @Headers Map<String, Object> headers) {
    logger.debug("Message received: " + message);
    
    // Send message to another system
    ....
  }

}

在XML中,对应的配置如下图,

<int:service-activator input-channel="theOtherChannel"
        ref="MessageProcessor" output-channel="nullChannel" />
  1. 如何在 Java DSL 中调用 ServiceActivator 方法?我正在考虑使用.handle(),但参数应该是什么?
  2. 在使用 Java DSL 时是否有 null 通道的概念?如果是,我们如何指定?

【问题讨论】:

    标签: spring-integration spring-integration-dsl


    【解决方案1】:

    这个这个handle()变种:

    /**
     * Populate a {@link ServiceActivatingHandler} for the
     * {@link org.springframework.integration.handler.MethodInvokingMessageProcessor}
     * to invoke the discovered {@code method} for provided {@code service} at runtime.
     * @param service the service object to use.
     * @return the current {@link BaseIntegrationFlowDefinition}.
     */
    public B handle(Object service) {
    

    因此,您只需要将 MessageProcessor 自动连接到具有 IntegrationFlow 定义的类中,或者直接连接到 inputFlow() bean 定义中。那么你就这样做:

    .channel(theOtherChannel()))
    .handle(messageProcessor)
    .get();
    

    nullChannel 中不需要返回类型为 void 的服务激活器。无论如何,当遇到voidnull 回复时,流程就会停止。

    在文档中查看更多信息:https://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl-handle

    【讨论】:

    • 做到了。谢谢!