码迷,mamicode.com
首页 > 其他好文 > 详细

kafka生产者

时间:2017-07-30 11:30:28      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:kafka   异步   tom   async   span   table   serialize   调用   资源   

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生产者

标签:kafka   异步   tom   async   span   table   serialize   调用   资源   

原文地址:http://www.cnblogs.com/moss-yang/p/7253354.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!