首页 > 解决方案 > 如何将部分源数据属性发送到不同的目标

问题描述

我正在建立一个源和两个目标之间的集成,这里源数据对象有 10 个属性,其中一个目标需要大约 6 个属性,另一个目标只需要 4 个属性,感谢任何关于我如何使用 spring 实现的帮助

标签: springspring-bootspring-integration

解决方案


您可以配置源以将消息发送到PublishSubscribeChannel.

然后配置两个Transformer订阅这个 pub-sub 频道。其中一个转换器将消息转换为 6 个属性版本,而另一个转换为 4 个属性版本。然后两个转换器将转换后的消息发送到一个单独的通道。两个目标系统将查找发送到这些分离通道的消息并处理它们。

在注解配置方面,如下所示:(假设源发出的消息是Foo

    @Bean
    public MessageChannel pubSubChannel() {
        return new PublishSubscribeChannel();
    }

    @Bean
    public MessageChannel outputChannelWith4Attributes() {
        return new DirectChannel();
    }

    @Bean
    public MessageChannel outputChannelWith6Attributes() {
        return new DirectChannel();
    }


    @Component 
    public class MyTransformer {

        @Transformer(inputChannel = "pubSubChannel", outputChannel = "outputChannelWith4Attributes")
        public Foo transformTo4Attribute(Foo foo) {
            //do the transformation logic here
            return result;
        }

        @Transformer(inputChannel = "pubSubChannel", outputChannel = "outputChannelWith6Attributes")
        public Foo transformTo6Attribute(Foo foo) {
            //do the transformation logic here
            return result;
        }
    }

并配置源以将带有有效负载的消息发送Foo到。还pubSubChannel配置目标以处理来自outputChannelWith4Attributes和的消息outputChannelWith6Attributes


推荐阅读