我是春天整合kafka的新手,我理解kafka-oubound-channel适配器。但有没有办法可以创建主题,而无需在上下文中设置xml?
ie:基于我对变换器的消息,我想将消息发布到为此消息类型创建的kafka主题。
更新
以下是我最终做的事情。欢迎任何更好的解决方案。
<int:channel id="inputForSolrPublish"></int:channel>
<int:service-activator input-channel="inputForSolrPublish"
ref="solrMasterListRouter" >
- &gt;
private void postMessageToMasterSpecifcTopics(final List<String> topicNames,
final String brokerList,
final Message<?> message) throws Exception {
for (String topicName : topicNames) {
createProducerContext(topicName,
brokerList).send(topicName,
message.getHeaders()
.get(KafkaHeaders.MESSAGE_KEY),
message);
}
}
private KafkaProducerContext<String, String> createProducerContext(final String topicName,
final String brokerList) throws Exception {
KafkaProducerContext<String, String> kafkaProducerContext = new KafkaProducerContext<String, String>();
AvroReflectDatumBackedKafkaEncoder<String> kafkaReflectionEncoder = new AvroReflectDatumBackedKafkaEncoder<>(String.class);
AvroSpecificDatumBackedKafkaEncoder<String> kafkaSpecificEncoder = new AvroSpecificDatumBackedKafkaEncoder<>(String.class);
// Encoder<String> encoder = new
// org.springframework.integration.kafka.serializer.common.StringEncoder<String>();
ProducerMetadata<String, String> producerMetadata = new ProducerMetadata<String, String>(topicName);
producerMetadata.setValueClassType(String.class);
producerMetadata.setKeyClassType(String.class);
producerMetadata.setValueEncoder(kafkaSpecificEncoder);
producerMetadata.setKeyEncoder(kafkaReflectionEncoder);
producerMetadata.setAsync(true);
Properties props = buildProducerConfigProperties();
ProducerFactoryBean<String, String> producer = new ProducerFactoryBean<String, String>(producerMetadata,
brokerList,
props);
ProducerConfiguration<String, String> config = new ProducerConfiguration<String, String>(producerMetadata,
producer.getObject());
kafkaProducerContext.setProducerConfigurations(Collections.singletonMap(topicName,
config));
return kafkaProducerContext;
}
private Properties buildProducerConfigProperties() {
Properties props = new Properties();
props.put("topic.metadata.refresh.interval.ms",
"3600000");
props.put("message.send.max.retries",
"5");
props.put("tsend.buffer.bytes",
"5242880");
return props;
}
答案 0 :(得分:2)
是的,您可以在运行时执行此操作。请参阅TopicUtils.ensureTopicCreated
。
您可以将<service-activator>
作为另一个订阅者(第一个)添加到<publish-subscribe-channel>
以发送消息。像这样:
<publish-subscribe-channel id="sendMessageToKafkaChannel"/>
<service-activator input-channel="sendMessageToKafkaChannel" output-channel="nullChannel" order="1"
ref="creatTopicService" method="creatTopic"/>
<int-kafka:outbound-channel-adapter channel="sendMessageToKafkaChannel" order="2"/>
在creatTopic
中接受整个消息并从消息中或在注入阶段提取所有必需参数,例如注入ZookeeperConnect
以提取第一个getZkConnect()
zkAddress
参数的ensureTopicCreated
。
但是你应该明白,在Kafka没有现有主题的情况下你不能拥有<int-kafka:message-driven-channel-adapter>
。所以,我不确定你将来如何处理那些动态创建的主题中的消息。虽然<int-kafka:inbound-channel-adapter>
可能适用于该情况......