繁体   English   中英

如何在 spring 集成中推迟消息的消耗

[英]How to postpone consuming of messages in spring integration

我正在使用 Spring Integration 5.0.1 和 Spring Boot 2.0.0.RC1 开发应用程序

目前,应用程序响应ApplicationReadyEvent并运行一些可能需要一段时间才能完成的初始化代码。 这不使用任何 spring 集成组件。

我还有一些非常基本的集成流程,使用 java dsl 编写并在配置中声明为 bean。

有没有办法推迟流何时开始消费消息? 我希望能够在初始化完成后手动启动它们。

似乎配置ControlBus将是解决方案,但我不知道如何将类似的东西与其他流连接起来。

以下是流如何使用消息的示例:

IntegrationFlows.from(sourceGateway)
                .transform(Transformers.fromJson(IncomingTask.class, jsonObjectMapper))

                .handle(IncomingTask.class, (incomingTask, headers) -> {

                //stuff with the task here.

                })

                .get();

是的,你绝对可以在这件事上使用ControlBus 使用 Java DSL,它看起来像:

@Bean
public IntegrationFlow controlBus() {
    return IntegrationFlowDefinition::controlBus;
}

要使用它,您需要:

@Autowired
@Qualifier("controlBus.input")
private MessageChannel controlBusChannel;

现在我们需要知道您的目标IntegraionFlow是如何启动的。 什么消费消息。 例如我有这个:

@Bean
public IntegrationFlow fileFlow1() {
    return IntegrationFlows.from("fileFlow1Input")
            .handle(Files.outboundAdapter(tmpDir.getRoot()),
                        c -> c.id("fileWriting").autoStartup(false))
                .get();
    }

注意c.id("fileWriting").autoStartup(false) id用于端点 bean,可以通过发送到控制总线的命令访问它。 autoStartup(false)意味着它不会立即使用消息,而只会在我们调用start() 我这样做:

this.controlBusChannel.send(new GenericMessage<>("@fileWriting.start()"));

您应该在配置中确保将消息消耗推迟到您需要的时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM