首先我们先来看一下步骤四,判断一条消息的大小,就是这里
/**
* 步骤四:
* 确认一下消息的大小是否超过了最大值
* kafkaproducer初始化的时候,指定了一个参数,代表的是producer这最大能发送的是一条消息能有多大
* 默认最大是1M,生产上会去修改
*
*/
ensureValidRecordSize(serializedSize);
跟进去
/**
* Validate that the record size isn't too large
*/
private void ensureValidRecordSize(int size) {
//如果一条消息的大小超过了1M,就会报错
if (size > this.maxRequestSize)
//自定义异常
throw new RecordTooLargeException("The message is " + size +
" bytes when serialized which is larger than the maximum request size you have configured with the " +
ProducerConfig.MAX_REQUEST_SIZE_CONFIG +
" configuration.");
//如果一条消息的大小超过了32M(缓存的大小)也会报错
if (size > this.totalMemorySize)
throw new RecordTooLargeException("The message is " + size +
" bytes when serialized which is larger than the total memory buffer you have configured with the " +
ProducerConfig.BUFFER_MEMORY_CONFIG +
" configuration.");
}
这里比较简单
步骤5,6也比较简单容易理解,有兴趣可以看一下