我正在尝试将我的一些代码从 XML 迁移到 java dsl 样式(java8 之前)。
这是我创建的 java 配置,我无法弄清楚如何设置轮询器。这些示例仅讨论全局轮询器,但在我的情况下,我需要在适配器中设置轮询器。
@Bean
public MessageHandler kafkaMessageHandler() {
KafkaProducerMessageHandler<String, String> handler =
new KafkaProducerMessageHandler<>(kafkaTemplate());
handler.setMessageKeyExpression(new LiteralExpression("kafka-integration"));
handler.setTopicExpression(new LiteralExpression("headers.kafka_topic"));
return handler;
}
@Bean
public KafkaTemplate<String, String> kafkaTemplate() {
return new KafkaTemplate<>(new DefaultKafkaProducerFactory<String, String>(producerConfigs()));
}
@Bean
public Map<String, Object> producerConfigs() {
Map<String, Object> properties = new HashMap<>();
properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
// introduce a delay on the send to allow more messages to accumulate
properties.put(ProducerConfig.LINGER_MS_CONFIG, 1);
return properties;
}
我拥有的 XML 等价物如下:
<int-kafka:outbound-channel-adapter
id="kafkaOutboundChannelAdapter"
kafka-producer-context-ref="kafkaProducerContext"
channel="kafkaChannel" >
<int:poller fixed-rate="1000" max-messages-per-poll="10000}"/>
</int-kafka:outbound-channel-adapter>