在运行时将动态值传递给kafka使用者主题以用于消息驱动的通道适配器

时间:2017-02-01 02:46:34

标签: java spring apache-kafka spring-integration

要求是在运行时下设置主题属性,而不重新启动服务器。如何在此处实现。 目前我们正在从属性文件中读取值,但这里需要重新启动服务器以反映已完成的更改。

例如: sample.properties(在部署目录中)

topic.list = TOPIC1,标题2

并且希望将来使用topic3而不重启服务器。

注意:发现主题最终变量。

尝试从文件系统路径(部署目录之外)读取密钥(topic.list),但没有运气。

任何建议。

<int-kafka:message-driven-channel-adapter

               id="inAdapter"
               channel="fromKafka"
               connection-factory="connectionFactory"
               key-decoder="kafkaKeyDecoder"
               payload-decoder="kafkaDecoder"                              
               topics="${topic.list}"
               offset-manager="offsetManager"/>

1 个答案:

答案 0 :(得分:1)

您可以使用Java DSL根据需要为其他主题动态添加适配器......

@Autowired
private IntegrationFlowContext flowContext;

public void addAnotherListenerForTopics(String... topics) {
    IntegrationFlow flow =
        IntegrationFlows.from(Kafka.messageDrivenChannelAdapter(consumerFactory(), topics))
            .channel("fromKafka")
            .get();
    this.flowContext.registration(flow).register();
}

bean.addAnotherListenerForTopics("added.new");

POM:

<dependency>
  <groupId>org.springframework.integration</groupId>
  <artifactId>spring-integration-java-dsl</artifactId>
  <version>1.2.1.RELEASE</version>
</dependency>

请注意,如果您使用代理分区分配,则新容器需要不同的组ID,以避免撤消现有分配。

相关问题