java - 新版本的 Kafka 生产者是否仍然有 "producer.type"?

标签 java apache-kafka producer

旧版本的文档说这是基本属性之一。

新版本的文档根本没有提到它。

较新版本的 Kafka 生产者仍然有 Producer.type 吗?

或者,新的生产者总是异步,我应该调用future.get()使其同步

最佳答案

新的生产者总是异步的,你应该调用 future.get() 来使其同步。当像添加 future.get() 这样简单的事情为您提供基本相同的功能时,不值得创建两个 api 方法。

来自此处的 send() 文档

https://kafka.apache.org/0110/javadoc/index.html?org/apache/kafka/clients/producer/KafkaProducer.html

Since the send call is asynchronous it returns a Future for the RecordMetadata that will be assigned to this record. Invoking get() on this future will block until the associated request completes and then return the metadata for the record or throw any exception that occurred while sending the record.

If you want to simulate a simple blocking call you can call the get() method immediately:

byte[] key = "key".getBytes();
byte[] value = "value".getBytes();  
ProducerRecord<byte[],byte[]> record = new ProducerRecord<byte[],byte[]>("my-topic", key, value);
producer.send(record).get();

关于java - 新版本的 Kafka 生产者是否仍然有 "producer.type"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45873907/

相关文章:

java - 需要 Spring Kafka KafkaTemplate.flush() 吗?

java - Kafka 流关联消息

java - Kafka 生产者异常 NoClassDefFoundError

producer - kafka生产者是否能够在列表中发送多条消息?

java - Hive-JDBC : No row found exception while fetching data from hive table through Hive-JDBC connection

JavaFX,将anchorpane元素与另一个文件中的FXML交换

apache-kafka - 如何关闭正在运行的 Confluent Cloud 集群?

java - 序列化和归档对象

java - 使用 Java 3.0 驱动程序检查 MongoDB 身份验证

RabbitMQ - 按优先级使用多个队列