相关疑难解决方法(0)

如何使用Spring Kafka的Acknowledgement.acknowledge()方法进行手动提交

我使用Spring卡夫卡第一次,我无法使用手动这里提到犯在我的消费者的代码Acknowledgement.acknowledge()方法https://docs.spring.io/spring-kafka/reference/html/_reference. html#commititting-offsets.我的是春季启动应用程序.如果我没有使用手动提交过程,那么我的代码工作正常.但是当我使用Acknowledgement.acknowledge()进行手动提交时,它会显示与bean相关的错误.此外,如果我没有正确使用手动提交,请建议我正确的方法.

错误信息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field ack in Receiver required a bean of type 'org.springframework.kafka.support.Acknowledgment' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.kafka.support.Acknowledgment' in your configuration.
Run Code Online (Sandbox Code Playgroud)

我搜索了这个错误,我发现我需要添加@Component,但这已经存在于我的消费者代码中.

我的消费者代码如下所示:Receiver.java

import java.util.concurrent.CountDownLatch;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

@Component
public class Receiver {

    @Autowired
    public Acknowledgment ack;

    private CountDownLatch latch = new CountDownLatch(1);

    @KafkaListener(topics = "${kafka.topic.TestTopic}")
    public void receive(ConsumerRecord<?, ?> consumerRecord){
            System.out.println(consumerRecord.value());
            latch.countDown();
            ack.acknowledge();
    }
} …
Run Code Online (Sandbox Code Playgroud)

java spring-boot kafka-consumer-api spring-kafka

3
推荐指数
3
解决办法
1万
查看次数