1、kafka生产者是线程安全的,她允许多个线程共享一个kafka实例
2、kafka管理一个简单的后台线程,所有的IO操作以及与每个broker的tcp连接通信,如果没有正确的关闭生产者可能会造成资源泄露。
kafka总共有以下的这些生产者实例
KafkaProducer(java.util.Map<java.lang.String,java.lang.Object> configs) A producer is instantiated by providing a set of key-value pairs as configuration. |
|
KafkaProducer(java.util.Map<java.lang.String,java.lang.Object> configs, Serializer<K> keySerializer, Serializer<V> valueSerializer) A producer is instantiated by providing a set of key-value pairs as configuration, a key and a value Serializer . |
|
KafkaProducer(java.util.Properties properties) A producer is instantiated by providing a set of key-value pairs as configuration. |
|
KafkaProducer(java.util.Properties properties, Serializer<K> keySerializer, Serializer<V> valueSerializer) A producer is instantiated by providing a set of key-value pairs as configuration, a key and a value Serializer . |
以及相应的方法
void |
close() Close this producer. |
java.util.Map<MetricName,? extends Metric> |
metrics() Return a map of metrics maintained by the producer |
java.util.List<PartitionInfo> |
partitionsFor(java.lang.String topic) Get a list of partitions for the given topic for custom partition assignment. |
java.util.concurrent.Future<RecordMetadata> |
send(ProducerRecord<K,V> record) Asynchronously send a record to a topic. |
java.util.concurrent.Future<RecordMetadata> |
send(ProducerRecord<K,V> record, Callback callback) Asynchronously send a record to a topic and invoke the provided callback when the send has been acknowledged. |
主要介绍send方法
public java.util.concurrent.Future<RecordMetadata> send(ProducerRecord<K,V> record, Callback callback)send方法是异步的,消息到缓冲区后接着发送消息,不会确认数据是否已经存入kafka,调用callback函数有效的了解当send发送失败时能够抛出异常。
时间: 2024-11-05 13:47:05