【问题标题】:Connecting Spring batch to Spring integration workflow将 Spring 批处理连接到 Spring 集成工作流
【发布时间】:2020-02-04 16:08:50
【问题描述】:



我正在尝试将 Spring Batch 和 Spring Integration 结合在一个项目中。所以我的想法是使用StepBuilderFactory 的Spring Batch 使用返回String 的自定义方法读取文件,而.processor 使用MessageBuilder 创建一个新的Message<String> 并将该消息发送到通道...
因此,Spring Integration 从这里连接到该渠道并开始工作其工作流程..

知道如何做到这一点吗?因为除了将String 读取到.processor 之外,我没有得到任何结果,但我无法从那里进行 Spring 集成。 我读到了remotechunkingmanagerstepbuilderfactory,但它不适合我的目的,因为它会自动设置特定类型

 @Bean
    public TaskletStep managerStep() throws Exception {

        return managerStepBuilderFactory.get("managerStep")
                .<String, String>chunk(5)
                .reader(readFile())
                .processor(new MessageProcess())//write String into Message and send it to Channel
                .writer(doSomething())//not important
                .build();
    }

public class MessageProcess implements ItemProcessor<String, String> {

    @Override
    public String process(String readString) throws Exception {
        Message<String> message = MessageBuilder.withPayload(item).build();
        channel().send(message); //sending message to channel
        return "item";

    }
}

    @Bean
    public IntegrationFlow workflow() {
        return IntegrationFlows
                .from(channel())
                .handle(checkMessage()) //checkMessage reads payload from Message<?> message
                .get();
    }

    @Bean
    public DirectChannel channel() {
        return new DirectChannel();
    }

【问题讨论】:

  • 您的MessageProcess 遇到什么问题?看起来没有什么问题。虽然我会将MessageProcess 设为@Bean。还要确保在 @Configuration 类的某处有 @EnableIntegration 注释。
  • 我同意 Artem。除此之外,为什么处理器做两件事而作者什么都不做?我将使用处理器将字符串转换为消息,并使用编写器将消息发送到通道。
  • 感谢 cmets。你是对的。代码原样工作正常。我只是忘记了@Artem Bilan 建议的一些注释。如果您发布它,我会接受它作为答案

标签: java spring spring-integration spring-batch spring-integration-dsl


【解决方案1】:

要使 Spring Integration 与注解配置或 Java DSL 一起使用,您需要确保在某些 @Configuration 类上指定 @EnableIntegration。或者考虑使用 Spring Boot。

查看更多信息文档:https://docs.spring.io/spring-integration/docs/5.2.3.RELEASE/reference/html/overview.html#configuration-enable-integration

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-13
    • 2022-12-05
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多