代码之家  ›  专栏  ›  技术社区  ›  Nick Tsitlakidis

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

  •  1
  • Nick Tsitlakidis  · 技术社区  · 8 年前

    我正在开发一个使用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();
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Artem Bilan    8 年前

    对,你绝对可以使用 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()"));
    

    您应该在配置中确保类似的功能,以将消息消耗推迟到您需要的时间。