工作过程
1、从阻塞队列里面消费PullRequest,PullRequest调用netty client做出Response
2、PullRequest做了本机缓存优化
3、后台线程处理拉取消息
4、长轮询优化:使用延时Task处理PullRequest,每隔5秒拉取消息,当处理大批量消息,延迟任务变更为10秒。
和推模式区别:
推模式做了封装,Broker启动时会将PullRequest封装起来,通过callback设计模式让consumer自行拉取消息,表面看起来时推消息,实际还是consumer自行拉取。
拉模式是由consumer主动去拉取消息,自行从阻塞队列里面拉取PullRequest
启动
public synchronized void start() throws MQClientException {
switch (this.serviceState) {
case CREATE_JUST:
this.serviceState = ServiceState.START_FAILED;
this.checkConfig();
if (this.defaultLitePullConsumer.getMessageModel() == MessageModel.CLUSTERING) {
this.defaultLitePullConsumer.changeInstanceNameToPID();
}
initMQClientFactory();
initRebalanceImpl();
initPullAPIWrapper();
initOffsetStore();
mQClientFactory.start();
startScheduleTask();
this.serviceState = ServiceState.RUNNING;
log.info("the consumer [{}] start OK", this.defaultLitePullConsumer.getConsumerGroup());
operateAfterRunning();
break;
case RUNNING:
case START_FAILED:
case SHUTDOWN_ALREADY:
throw new MQClientException("The PullConsumer service state not OK, maybe started once, "
+ this.serviceState
+ FAQUrl.suggestTodo(FAQUrl.CLIENT_SERVICE_NOT_OK),
null);
default:
break;
}
}