使用Spring Integration Java DSL创建Java消费者的Java代码
答案 0 :(得分:1)
使用以下代码创建消息驱动的适配器,以连接以使用来自kafka的消息
@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;
@Value("${spring.kafka.topic}")
private String springIntegrationKafkaTopic;
@Bean
public IntegrationFlow kafkaReader() throws Exception {
return IntegrationFlows
.from(Kafka.messageDrivenChannelAdapter(listener(),ListenerMode.record))
.channel("queureader")
.get();
}
@Bean
public KafkaMessageListenerContainer listener() {
return new KafkaMessageListenerContainer(consumerFactory(), new ContainerProperties(this.springIntegrationKafkaTopic));
}
@Bean
public ConsumerFactory consumerFactory() {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, this.bootstrapServers);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.GROUP_ID_CONFIG, "kafkaListener");
return new DefaultKafkaConsumerFactory(props);
}
@ServiceActivator(inputChannel = "queureader")
public void Print(Message<?> msg) {
System.out.println(msg.getPayload().toString());
}
在application.properties中
您可以提及ConsumerConfig.GROUP_ID_CONFIG的任何值。