Kafka源码之异步发送模式
异步发送消息
在发送消息的时候设置回调函数:

调⽤KafkaProducer的send⽅法,该⽅法接收要发送的消息批,同时接收回调对象:

doSend的实现:

累加器append的实现:

tryAppend的实现:


Sender的run⽅法调⽤:

sendProducerData的实现:

sendProducerRequests的实现:


上述⽅法如果得到broker的响应,就回调 handleProduceResponse ⽅法:

该⽅法对响应的处理:

completeBatch的实现:


completeBatch的实现:

batch的done⽅法:

触发回调函数的执⾏:

上图中执⾏⽤户设置的callback函数的onCompletion⽅法:

由于上述⽅法都是在Sender线程中调⽤,因此回调的onCompletion⽅法的执⾏也是异步的,跟⽤户的producer.send⽅法不在同⼀个线程。
回调的异步执⾏即是⽣产的异步发送模式。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 WeiJia_Rao!