【发布时间】: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