如何在JavaSpring中使用KafkaTemplate而不使用@KafkaListener来使用消息表单Kafka?RabbitMQ类似物
我想在卡夫卡中声明主题,在其中发送消息,并从这个主题接收消息。据我所知,最简单的方法是使用Kafkatemplate方法send(),这也会创建一个主题。但是我如何在没有@KafkaListener的情况下,使用某种方法接收来自本主题的消息。 最近,我为拉比MQ做了同样的alghritm。具体实现如下:如何在JavaSpring中使用KafkaTemplate而不使用@KafkaListener来使用消息表单Kafka?RabbitMQ类似物,java,spring,apache-kafka,rabbitmq,Java,Spring,Apache Kafka,Rabbitmq,我想在卡夫卡中声明主题,在其中发送消息,并从这个主题接收消息。据我所知,最简单的方法是使用Kafkatemplate方法send(),这也会创建一个主题。但是我如何在没有@KafkaListener的情况下,使用某种方法接收来自本主题的消息。 最近,我为拉比MQ做了同样的alghritm。具体实现如下: @Bean public AmqpAdmin amqpAdmin(final ConnectionFactory connectionFactory) { ret
@Bean
public AmqpAdmin amqpAdmin(final ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
@Bean
public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
}
然后
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private AmqpAdmin amqpAdmin;
private void sendMessage(final String queue, final String message) {
createQueueIfNotExists(queue);
this.rabbitTemplate.convertAndSend(queue, message);
}
private void createQueueIfNotExists(final String queue) {
this.amqpAdmin.declareQueue(new Queue(queue));
}
protected String receiveMessage(final String queue) {
Message message = this.rabbitTemplate.receive(queue);
Strung result = messeage.getBody();
return result;
}
我可以对KafkaTemplate执行同样的操作吗?您可以使用Apache官方Kafka客户端创建使用者 1.使用属性创建卡夫卡消费者 2.分配或订阅主题 3.使用轮询方法轮询消息 创造消费者
private静态使用者createConsumer(){
最终属性道具=新属性();
props.put(ConsumerConfig.BOOTSTRAP\u SERVERS\u CONFIG,
引导服务器);
props.put(ConsumerConfig.GROUP\u ID\u CONFIG,
"卡夫卡夫(例如消费者);;
props.put(ConsumerConfig.KEY\u反序列化程序\u类\u配置,
LongDeserializer.class.getName());
props.put(ConsumerConfig.VALUE\u反序列化程序\u类\u配置,
StringDeserializer.class.getName());
//使用道具创建消费者。
最终消费者=
新卡夫卡消费者(道具);
//订阅该主题。
consumer.subscribe(Collections.singletonList(主题));
退货消费者;
}
阅读信息
static void runConsumer()引发InterruptedException{
最终消费者=createConsumer();
最终积分放弃=100;积分不可恢复=0;
while(true){
//轮询自上次偏移以来的记录。
最终用户记录用户记录=
消费者调查(1000);
if(consumerRecords.count()=0){
NorecordScont++;
如果(NORECORDSCONNT>放弃)中断;
否则继续;
}
//打印在for循环中接收的消息
消费者记录。forEach(记录->{
System.out.printf(“消费者记录:(%d,%s,%d,%d)\n”,
record.key(),record.value(),
record.partition(),record.offset());
});
//在异步模式下提交消息的偏移量
consumer.commitAsync();
}
consumer.close();
系统输出打印项次(“完成”);
}
您必须创建一个卡夫卡消费者来消费来自卡夫卡的消息。@Umeshwaran,是的,我找到了这个类,但实际上我不知道如何使用它接收来自主题的消息。你知道吗?是的,请看我的答案。过去6个月我一直在使用卡夫卡。还请注意,Kafka不支持消息的优先级处理。因此,如果需要优先级处理,您可能希望查看其他MQ,或者您必须针对Kafkaw编写一个解决方案不使用KafkaListener或Spring Cloud Streams/Spring Integration的用例是什么?@OneCricketeer for testingExact consumerrecord-这是一条消息,对吗?ConsumerRecords就像一个消息列表。您必须遍历消费者记录和消费消息。
private static Consumer<Long, String> createConsumer() {
final Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
BOOTSTRAP_SERVERS);
props.put(ConsumerConfig.GROUP_ID_CONFIG,
"KafkaExampleConsumer");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
LongDeserializer.class.getName());
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
StringDeserializer.class.getName());
// Create the consumer using props.
final Consumer<Long, String> consumer =
new KafkaConsumer<>(props);
// Subscribe to the topic.
consumer.subscribe(Collections.singletonList(TOPIC));
return consumer;
}
static void runConsumer() throws InterruptedException {
final Consumer<Long, String> consumer = createConsumer();
final int giveUp = 100; int noRecordsCount = 0;
while (true) {
//polling the records since the last offset.
final ConsumerRecords<Long, String> consumerRecords =
consumer.poll(1000);
if (consumerRecords.count()==0) {
noRecordsCount++;
if (noRecordsCount > giveUp) break;
else continue;
}
//Printing the messages received in a for loop
consumerRecords.forEach(record -> {
System.out.printf("Consumer Record:(%d, %s, %d, %d)\n",
record.key(), record.value(),
record.partition(), record.offset());
});
//committing the offset of messages in Async mode
consumer.commitAsync();
}
consumer.close();
System.out.println("DONE");
}