Spring integration Spring集成-集成流模板

Spring integration Spring集成-集成流模板,spring-integration,spring-integration-dsl,Spring Integration,Spring Integration Dsl,我已经成功地在DSL中实现了一些集成流,在PROD中一切都运行良好。 对于几乎所有的流(+-10),我只有一个消息源和一个处理程序,加上一些额外的特性,这些特性对于每个流都是相同的: 创建发送电子邮件的错误流 增加断路器 添加httpinbound以强制运行 我想知道是否有一些优雅的方法来分解它,比如抽象配置类或模板(可能是flowIntegrationAdatper?) 鉴于此抽象级别,在每个流配置类中,我只想提供/覆盖两种方法: 消息来源 处理者 @配置 @Configurati

我已经成功地在DSL中实现了一些集成流,在PROD中一切都运行良好。 对于几乎所有的流(+-10),我只有一个消息源和一个处理程序,加上一些额外的特性,这些特性对于每个流都是相同的:

  • 创建发送电子邮件的错误流
  • 增加断路器
  • 添加httpinbound以强制运行
我想知道是否有一些优雅的方法来分解它,比如抽象配置类或模板(可能是flowIntegrationAdatper?)

鉴于此抽象级别,在每个流配置类中,我只想提供/覆盖两种方法:

  • 消息来源
  • 处理者

@配置
@ConfigurationProperties(“app.flows.sample”)
公共类SampleFlowConfiguration{
公共静态最终字符串流\u NAME=“SampleFlow”;
公共静态最终字符串POLLER=“POLLER”;
私人最终服务;
私人最终任务执行人工厂任务执行人工厂;
私人最终ErrorFlowFactory ErrorFlowFactory;
公共采样流配置(服务,
TaskExecutorFactory TaskExecutorFactory,
ErrorFlowFactory ErrorFlowFactory){
服务=服务;
this.taskExecutorFactory=taskExecutorFactory;
this.errorFlowFactory=errorFlowFactory;
}
@豆子
公共集成流sampleFlow(){
返回积分流
.from(objecttotreassource(),sampleProducer())
.enrichHeaders(h->h.header(MessageHeaders.ERROR\u通道,sampleErrorChannel()))
.channel(MessageChannels.executor(sampleConsumerTaskExecutor())
.handle(handler())
.get();
}
@豆子
public MessageSource objecttotreassource(){
return service.getObjectToTreat();
}
@豆子
公共消费者样本生产者(){
返回c->c.poller(Pollers.cron(“**”)
.maxMessagesPerPoll(10)
.errorChannel(sampleErrorChannel())
.taskExecutor(samplePollerTaskExecutor())
.AutoStart(错误)
.id(轮询器);
}
@豆子
public MessageHandler objectHandler(){
返回新的AbstractReplyProducingMessageHandler(){
@凌驾
受保护对象HandlerRequestMessage(消息消息){
handle(message.getPayload());
返回消息;
}
};
}
@豆子
公共执行器samplePollerTaskExecutor(){
return taskExecutorFactory.getTaskExecutor(10,“sampleProducerExec”);
}
@豆子
公共执行器sampleConsumerTaskExecutor(){
return taskExecutorFactory.getTaskExecutor(10,“sampleConsumerExec”);
}
@豆子
公共DirectChannel sampleErrorChannel(){
返回MessageChannels.direct().get();
}
@豆子
公共集成流示例ExpirationErrorFlow(){
返回errorFlowFactory.getDefaultErrorFlow(
sampleErrorChannel(),
m->POLLER,
mailTransformer());
}
@豆子
公用信息过滤变压器异常mailTransformer(){
FlowErrorMessageBuilder FlowErrorMessageBuilder=messageException->
“处理样本时出错”;
将新的MessagingExceptionToMiltTransformer返回(FLOW_NAME,flowErrorMessageBuilder,true);
}
}

感谢集成流程
是一个工厂,它确实可以通过一些通用方法使用:

@Bean
public IntegrationFlow sampleFlow() {
    return myFlowBuilder(objectToTreatsSource(), handler());
}

private IntegrationFlow myFlowBuilder(MessageSource<?> messageSource, MessageHandler  handler) {
 return IntegrationFlows
            .from(messageSource, sampleProducer())
            .enrichHeaders(h -> h.header(MessageHeaders.ERROR_CHANNEL, sampleErrorChannel()))
            .channel(MessageChannels.executor(sampleConsumerTaskExecutor()))
            .handle(handler)
            .get();
@Bean
公共集成流sampleFlow(){
返回myFlowBuilder(ObjectToTreasSource(),handler());
}
私有集成流myFlowBuilder(MessageSource MessageSource、MessageHandler){
返回积分流
.from(messageSource,sampleProducer())
.enrichHeaders(h->h.header(MessageHeaders.ERROR\u通道,sampleErrorChannel()))
.channel(MessageChannels.executor(sampleConsumerTaskExecutor())
.handle(处理程序)
.get();
}

另一方面,您可以将这些流的公共部分提取到一个独立的流中,并使用
gateway()
向该流执行请求并等待答复


请记住:
MessageChannel
抽象是框架中的头等公民之一,因此您总是可以从某个地方发送到某个频道。

谢谢,我会尝试一下,如果需要,会回来找您。
@Bean
public IntegrationFlow sampleFlow() {
    return myFlowBuilder(objectToTreatsSource(), handler());
}

private IntegrationFlow myFlowBuilder(MessageSource<?> messageSource, MessageHandler  handler) {
 return IntegrationFlows
            .from(messageSource, sampleProducer())
            .enrichHeaders(h -> h.header(MessageHeaders.ERROR_CHANNEL, sampleErrorChannel()))
            .channel(MessageChannels.executor(sampleConsumerTaskExecutor()))
            .handle(handler)
            .get();