rocketmq源码解析发送消息处理器②

说在前面

发小消息处理器

 

源码解析

返回方法,在发送消息之后创建topic配置,org.apache.rocketmq.broker.topic.TopicConfigManager#createTopicInSendMessageBackMethod介绍过了。

返回方法,存储批量消息,org.apache.rocketmq.store.DefaultMessageStore#putMessages介绍过了。

返回方法,解析存储结果,org.apache.rocketmq.broker.processor.SendMessageProcessor#handlePutMessageResult

 private RemotingCommand handlePutMessageResult(PutMessageResult putMessageResult, RemotingCommand response,                                                   RemotingCommand request, MessageExt msg,                                                   SendMessageResponseHeader responseHeader, SendMessageContext sendMessageContext, ChannelHandlerContext ctx,                                                   int queueIdInt) {        if (putMessageResult == null) {            response.setCode(ResponseCode.SYSTEM_ERROR);            response.setRemark("store putMessage return null");            return response;        }        boolean sendOK = false;
        switch (putMessageResult.getPutMessageStatus()) {            // Success            case PUT_OK:                sendOK = true;                response.setCode(ResponseCode.SUCCESS);                break;            case FLUSH_DISK_TIMEOUT:                response.setCode(ResponseCode.FLUSH_DISK_TIMEOUT);                sendOK = true;                break;            case FLUSH_SLAVE_TIMEOUT:                response.setCode(ResponseCode.FLUSH_SLAVE_TIMEOUT);                sendOK = true;                break;            case SLAVE_NOT_AVAILABLE:                response.setCode(ResponseCode.SLAVE_NOT_AVAILABLE);                sendOK = true;                break;
            // Failed            case CREATE_MAPEDFILE_FAILED:                response.setCode(ResponseCode.SYSTEM_ERROR);                response.setRemark("create mapped file failed, server is busy or broken.");                break;            case MESSAGE_ILLEGAL:            case PROPERTIES_SIZE_EXCEEDED:                response.setCode(ResponseCode.MESSAGE_ILLEGAL);                response.setRemark(                    "the message is illegal, maybe msg body or properties length not matched. msg body length limit 128k, msg properties length limit 32k.");                break;            case SERVICE_NOT_AVAILABLE:                response.setCode(ResponseCode.SERVICE_NOT_AVAILABLE);                response.setRemark(                    "service not available now, maybe disk full, " + diskUtil() + ", maybe your broker machine memory too small.");                break;            case OS_PAGECACHE_BUSY:                response.setCode(ResponseCode.SYSTEM_ERROR);                response.setRemark("[PC_SYNCHRONIZED]broker busy, start flow control for a while");                break;            case UNKNOWN_ERROR:                response.setCode(ResponseCode.SYSTEM_ERROR);                response.setRemark("UNKNOWN_ERROR");                break;            default:                response.setCode(ResponseCode.SYSTEM_ERROR);                response.setRemark("UNKNOWN_ERROR DEFAULT");                break;        }
        String owner = request.getExtFields().get(BrokerStatsManager.COMMERCIAL_OWNER);        if (sendOK) {
            this.brokerController.getBrokerStatsManager().incTopicPutNums(msg.getTopic(), putMessageResult.getAppendMessageResult().getMsgNum(), 1);            this.brokerController.getBrokerStatsManager().incTopicPutSize(msg.getTopic(),                putMessageResult.getAppendMessageResult().getWroteBytes());            this.brokerController.getBrokerStatsManager().incBrokerPutNums(putMessageResult.getAppendMessageResult().getMsgNum());
            response.setRemark(null);
            responseHeader.setMsgId(putMessageResult.getAppendMessageResult().getMsgId());            responseHeader.setQueueId(queueIdInt);            responseHeader.setQueueOffset(putMessageResult.getAppendMessageResult().getLogicsOffset());
//            响应=》            doResponse(ctx, request, response);
            if (hasSendMessageHook()) {                sendMessageContext.setMsgId(responseHeader.getMsgId());                sendMessageContext.setQueueId(responseHeader.getQueueId());                sendMessageContext.setQueueOffset(responseHeader.getQueueOffset());
                int commercialBaseCount = brokerController.getBrokerConfig().getCommercialBaseCount();                int wroteSize = putMessageResult.getAppendMessageResult().getWroteBytes();                int incValue = (int)Math.ceil(wroteSize / BrokerStatsManager.SIZE_PER_COUNT) * commercialBaseCount;
                sendMessageContext.setCommercialSendStats(BrokerStatsManager.StatsType.SEND_SUCCESS);                sendMessageContext.setCommercialSendTimes(incValue);                sendMessageContext.setCommercialSendSize(wroteSize);                sendMessageContext.setCommercialOwner(owner);            }            return null;        } else {            if (hasSendMessageHook()) {                int wroteSize = request.getBody().length;                int incValue = (int)Math.ceil(wroteSize / BrokerStatsManager.SIZE_PER_COUNT);
                sendMessageContext.setCommercialSendStats(BrokerStatsManager.StatsType.SEND_FAILURE);                sendMessageContext.setCommercialSendTimes(incValue);                sendMessageContext.setCommercialSendSize(wroteSize);                sendMessageContext.setCommercialOwner(owner);            }        }        return response;    }

进入方法,响应,org.apache.rocketmq.broker.processor.AbstractSendMessageProcessor#doResponse

 protected void doResponse(ChannelHandlerContext ctx, RemotingCommand request,        final RemotingCommand response) {        if (!request.isOnewayRPC()) {            try {                ctx.writeAndFlush(response);            } catch (Throwable e) {                log.error("SendMessageProcessor process request over, but response failed", e);                log.error(request.toString());                log.error(response.toString());            }        }    }

返回方法,发送消息,org.apache.rocketmq.broker.processor.SendMessageProcessor#sendMessage介绍过了。

返回方法,执行发送消息之后的钩子方法,org.apache.rocketmq.broker.processor.AbstractSendMessageProcessor#executeSendMessageHookAfter

  public void executeSendMessageHookAfter(final RemotingCommand response, final SendMessageContext context) {        if (hasSendMessageHook()) {            for (SendMessageHook hook : this.sendMessageHookList) {                try {                    if (response != null) {                        final SendMessageResponseHeader responseHeader =                            (SendMessageResponseHeader) response.readCustomHeader();                        context.setMsgId(responseHeader.getMsgId());                        context.setQueueId(responseHeader.getQueueId());                        context.setQueueOffset(responseHeader.getQueueOffset());                        context.setCode(response.getCode());                        context.setErrorMsg(response.getRemark());                    }                    hook.sendMessageAfter(context);                } catch (Throwable e) {                    // Ignore                }            }        }    }

返回方法,org.apache.rocketmq.broker.processor.SendMessageProcessor#processRequest结束。

 

说在最后

本次解析仅代表个人观点,仅供参考。

 

加入技术微信群

钉钉技术群

转载于:https://my.oschina.net/u/3775437/blog/3097025

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值