动态创建一系列有序的IntegrationFlow

时间:2015-11-26 16:15:12

标签: spring-integration

我创建了一系列使用Java DSL显式使用Spring Integration的进程。每个过程都做不同的事情,但它们有一些相同的处理逻辑

示例:

get
process
deduplicate
emit

我想基本上创建一系列后期处理集成流程,可以通过配置/配置文件启用/禁用。

示例:

get
preprocess flow 1 (if enabled)
...
preprocess flow n (if enabled)
process
postprocess flow 1 (if enabled)
...
postprocess flow n (if enabled)
emit

我很确定这在SI中还没有存在,但我想我会问。我唯一能想到的就是创建一个可以动态创建直接消息通道的bean,并且在配置期间,我可以给每个集成流程用来从"中获取#34;和"频道"消息频道。

示例:

@Configuration
public class BaseIntegrationConfiguration {
    @Bean
    public MessageChannel preProcessMessageChannel() {
        return MessageChannels.direct().get();
    }

    @Bean
    public MessageChannel processMessageChannel() {
        return MessageChannels.direct().get();
    }

    @Bean
    public MessageChannel postProcessMessageChannel() {
        return MessageChannels.direct().get();
    }

    @Bean
    public MessageChannel emitMessageChannel() {
        return MessageChannels.direct().get();
    }

    @Bean
    public IntegrationFlow getDataFlow(MessageChannel preProcessMessageChannel) {
        return IntegrationFlows
                .from(/* some inbound channel adapter */)
                // do other flow stuff
                .channel(preProcessMessageChannel)
                .get();
    }

    @Bean
    public IntegrationFlowChainMessageChannelGenerator preProcessFlowGenerator(
            MessageChannel preProcessMessageChannel, 
            MessageChannel processMessageChannel) {
        IntegrationFlowChainMessageChannelGenerator generator = new IntegrationFlowChainMessageChannelGenerator ();
        generator.startWith(preProcessMessageChannel);
        generator.endWith(processMessageChannel);

        return generator;
    }

    @Bean
    public IntegrationFlow processFlow(
            MessageChannel processMessageChannel, 
            MessageChannel postProcessMessageChannel) {
        return IntegrationFlows
                .from(processMessageChannel)
                // do other flow stuff
                .channel(postProcessMessageChannel)
                .get();
    }

    @Bean
    public IntegrationFlowChainMessageChannelGenerator postProcessFlowGenerator(
            MessageChannel postProcessMessageChannel, 
            MessageChannel emitMessageChannel) {
        IntegrationFlowChainMessageChannelGenerator generator = new IntegrationFlowChainMessageChannelGenerator ();
        generator.startWith(postProcessMessageChannel);
        generator.endWith(emitMessageChannel);

        return generator;
    }
}

@Configuration
@Order(1)
@Profile("PreProcessFlowOne")
public class PreProcessOneIntegrationConfiguration {
    @Bean
    public IntegrationFlow preProcessFlowOne(IntegrationFlowChainMessageChannelGenerator preProcessFlowGenerator) {
        return IntegrationFlows
                .from(preProcessFlowGenerator.getSourceChannel())
                // flow specific behavior here
                .channel(preProcessFlowGenerator.getDestinationChannel())
                .get();
    }
}

@Configuration
@Order(2)
@Profile("PreProcessFlowTwo")
public class PreProcessTwoIntegrationConfiguration {
    @Bean
    public IntegrationFlow preProcessFlowTwo(IntegrationFlowChainMessageChannelGenerator preProcessFlowGenerator) {
        return IntegrationFlows
                .from(preProcessFlowGenerator.getSourceChannel())
                // flow specific behavior here
                .channel(preProcessFlowGenerator.getDestinationChannel())
                .get();
    }
}

@Configuration
@Order(1)
@Profile("PostProcessFlowOne")
public class PostProcessOneIntegrationConfiguration {
    @Bean
    public IntegrationFlow postProcessFlowOne(IntegrationFlowChainMessageChannelGenerator postProcessFlowGenerator) {
        return IntegrationFlows
                .from(postProcessFlowGenerator.getSourceChannel())
                // flow specific behavior here
                .channel(postProcessFlowGenerator.getDestinationChannel())
                .get();
    }
}

@Configuration
@Order(2)
@Profile("PostProcessFlowTwo")
public class PostProcessTwoIntegrationConfiguration {
    @Bean
    public IntegrationFlow postProcessFlowTwo(IntegrationFlowChainMessageChannelGenerator postProcessFlowGenerator) {
        return IntegrationFlows
                .from(postProcessFlowGenerator.getSourceChannel())
                // flow specific behavior here
                .channel(postProcessFlowGenerator.getDestinationChannel())
                .get();
    }
}

这里的想法是" getDestinationChannel"每次都会创建一个新的频道,并将最后生成的频道的输出桥接到配置的" endWith"并且每次调用" getSourceChannel"返回最后创建的目标通道,如果没有,则返回" startWith"信道。

当我写下并思考这个问题时,我开始认为可能有更好的方法,但我认为我会把它放在那里进行一些输入。

谢谢。

1 个答案:

答案 0 :(得分:2)

目前DSL尚未直接支持,但routing slip可能会满足您的需求。

如果您的getdedup等是个别流量,您可以在初始流程开始时将路由单初始化为包含或不包含预处理步骤的输入通道在主流的渠道之间的列表中。

虽然DSL中还没有一流的支持,但您可以使用标题扩展来设置路由单。标题名称为IntegrationMessageHeaderAccessor.ROUTING_SLIP

修改

实际上,不要自己维护标题;向下滚动有关路由单的参考手册章节,了解如何使用Java配置HeaderEnricher