5

パブリッシュチャンネルのRabbitMQ接続仕様のリストから一連のコンシューマに移動する正しい方法は何ですか?あるSpringインテグレーションを使用した動的コンフィグレーション

、次のように私はウサギの設定を持っていると言う:私は@NestedConfigurationProperty List<RabbitConfiguration>にこれらをマーシャリング

rabbits: 
    - hostname: blahblah 
     vhost: blahlbah 
     username: blahlbah 
     password: blahlbalh 
     exchange: blahblah 
    - hostname: blahblah1 
     vhost: blahlbah1 
     username: blahlbah1 
     password: blahlbalh1 
     exchange: blahblah1 
    ... 

@Bean 
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) 
public AmqpTemplate amqpTemplate(RabbitConfiguration rabbitConfiguration) { 
    ... 
} 

私は、基本的にIntegrationFlowにそれをマッピングすることができます:

@Bean 
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) 
public IntegrationFlow integrationFlow(AmqpTemplate amqpTemplate) { 
    return IntegrationFlows.from(inboundPubSubChannel()).handle(Amqp.outboundAdapter(amqpTemplate)).get(); 
} 

は何、しかし、である私は、私にそれらList<RabbitConfiguration>の1からAmqpTemplateを取得しますので、のような@Beanメソッドを書くことができList<RabbitConfiguration>のためにこれを行う正しい方法で、結果としてList<IntegrationFlow>がSpringによって処理されますか?

@Bean 
public List<IntegrationFlow> integrationFlows(List<RabbitConfiguration> rabbitConfigurations) { 
    return rabbitConfigurations.stream() 
     .map(this::amqpTemplate) 
     .map(this::integrationFlow) 
     .collect(toList()) 
} 

答えて

0

あなたがしようとしていることは、設定プロパティに基づいてIntegrationFlow型のSpring Beanを動的に作成することだと思います。どのように "魔法"か "透明"であるかに応じて、多くの選択肢があります。完全な魔法を望むなら、BeanFactoryPostProcessorを実装し、Springコンテキストで登録する必要があります。このような 何か動作するはずです:

public class IntegrationFlowPostProcessor implements BeanFactoryPostProcessor{ 

    List<RabbitConfiguration> rabbitConfigurations; 

    public IntegrationFlowPostProcessor(List<RabbitConfiguration> rabbitConfigurations) { 
    this.rabbitConfigurations = rabbitConfigurations; 
    } 

    @Override 
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 
    rabbitConfigurations.stream() 
     .forEach(rabbitConfig -> { 
      IntegrationFlow intFlow = integrationFlow(amqpTemplate(rabbitConfig)); 
      beanFactory.registerSingleton(rabbitConfig.getHostanme(), intFlow); 
     }); 
    } 

    private AmqpTemplate amqpTemplate(RabbitConfiguration rabbitConfiguration) { 
    //Implement here 
    return null; 
    } 

    private IntegrationFlow integrationFlow(AmqpTemplate amqpTemplate) { 
    //Implement here 
    return null; 
    } 
} 

を次にご使用の構成クラスでポストプロセッサを登録する必要があります:

@Bean public IntegrationFlowPostProcessor ifpp(List<RabbitConfiguration> config { 
    return new IntegrationFlowPostProcessor(config); 
} 

次に、あなたにホスト名で各統合の流れを注入することができるだろう@Qualifierを使用する他のBean、またはコレクションとして、List<IntegrationFlow>とし、すべての統合フローがSpringコンテキストに存在するとします。

関連する問題