刚刚在研究rocketmq生成文件的源码。零时记录一下MapedFileQueue中属性AllocateMapedFileService allocateMapedFileService的初始化链。

首先这个服务实在DefaultMessageStore中初始化完成并启动的,然后DefaultMessageStore把AllocateMapedFileService对象传给了CommitLog(绿色箭头)

DefaultMessageStore-CommitLog-MapedFileQueue.allocateMapedFileService初始化链-LMLPHP

然后CommitLog初始化MapedFileQueue的时候又穿给了它。

DefaultMessageStore-CommitLog-MapedFileQueue.allocateMapedFileService初始化链-LMLPHP

最终MapedFileQueue有了两种创建文件的方式,一种就是这个服务线程:AllocateMapedFileService

DefaultMessageStore-CommitLog-MapedFileQueue.allocateMapedFileService初始化链-LMLPHP

使用起来也很方便:

DefaultMessageStore-CommitLog-MapedFileQueue.allocateMapedFileService初始化链-LMLPHP

只要调用它的这个方法就可以了:

allocateMapedFileService.putRequestAndReturnMapedFile(nextFilePath,
nextNextFilePath, this.mapedFileSize);

这个方法会把这三个参数分装,然后放入它的优先级队列中PriorityBlockingQueue,这个服务线程就会不断的从这个优先级队列中拿需要创建文件的参数来创建文件来。

05-26 05:56