Spring Integration DSL - Wiretap不会异步运行

时间:2017-05-30 11:23:17

标签: java spring asynchronous spring-integration dsl

当Spring Integration文档声明(http://docs.spring.io/spring-integration/reference/html/messaging-channels-section.html#channel-wiretap)时,如果我们想要将wiretap作为异步运行,我们需要将消息发送到异步Channel(Pollable Channel或Executor Channel)

我尝试实现类似下面的流程,但它没有按预期运行

return IntegrationFlows
     .from("inputChannel")
     .wireTap(customChannel())
     .handle((p, h) -> {
          System.out.println("After calling wiretap");
          return p;
     })
     .get();
=========================

@Bean
public MessageChannel customChannel() {

    return MessageChannels.executor(new TaskExecutor() {
        @Override
        public void execute(Runnable task) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {

            }
            System.out.println("End customChannel");
        }
    }).get();
}

我希望它会写“调用窃听后”,然后睡5秒,然后写“结束customChannel”

但控制台是:

End customChannel
After calling wiretap

请帮我提一下这个案子的建议!

1 个答案:

答案 0 :(得分:0)

最后,我找到了异步执行的方式,如下面的

return IntegrationFlows
 .from("inputChannel")
 .wireTap("CustomChannel")
 .handle((p, h) -> {
      System.out.println("After calling wiretap");
      return p;
 })
 .get();

=========================

@Bean (value = "CustomChannel")
public MessageChannel customChannel() {
    // execute 5 concurrent threads.
    // if additional tasks are submitted when all threads are active, they will wait in the queue.
    return new ExecutorChannel(Executors.newFixedThreadPool(5));
}

@Bean
@ServiceActivator(inputChannel = "CustomChannel")
public MessageHandler exportDataServiceActivator() {
    return new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            // TODO Handle logic we want here

        }
    };
}