首页 > 代码库 > kafka生产者
kafka生产者
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发送失败时能够抛出异常。
kafka生产者
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。