Search in sources :

Example 1 with SendMessageResponseHeader

use of org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader in project rocketmq by apache.

the class MQClientAPIImplTest method createSuccessResponse.

private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());
    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);
    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageResponseHeader(org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader)

Example 2 with SendMessageResponseHeader

use of org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader in project rocketmq by apache.

the class AbstractSendMessageProcessor method 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
            }
        }
    }
}
Also used : SendMessageResponseHeader(org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader) SendMessageHook(org.apache.rocketmq.broker.mqtrace.SendMessageHook)

Example 3 with SendMessageResponseHeader

use of org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader in project rocketmq by apache.

the class SendMessageProcessor method sendMessage.

private RemotingCommand sendMessage(final ChannelHandlerContext ctx, final RemotingCommand request, final SendMessageContext sendMessageContext, final SendMessageRequestHeader requestHeader) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    final SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    response.setOpaque(request.getOpaque());
    response.addExtField(MessageConst.PROPERTY_MSG_REGION, this.brokerController.getBrokerConfig().getRegionId());
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, String.valueOf(this.brokerController.getBrokerConfig().isTraceOn()));
    log.debug("receive SendMessage request command, {}", request);
    final long startTimstamp = this.brokerController.getBrokerConfig().getStartAcceptSendRequestTimeStamp();
    if (this.brokerController.getMessageStore().now() < startTimstamp) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("broker unable to service, until %s", UtilAll.timeMillisToHumanString2(startTimstamp)));
        return response;
    }
    response.setCode(-1);
    super.msgCheck(ctx, requestHeader, response);
    if (response.getCode() != -1) {
        return response;
    }
    final byte[] body = request.getBody();
    int queueIdInt = requestHeader.getQueueId();
    TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(requestHeader.getTopic());
    if (queueIdInt < 0) {
        queueIdInt = Math.abs(this.random.nextInt() % 99999999) % topicConfig.getWriteQueueNums();
    }
    MessageExtBrokerInner msgInner = new MessageExtBrokerInner();
    msgInner.setTopic(requestHeader.getTopic());
    msgInner.setQueueId(queueIdInt);
    if (!handleRetryAndDLQ(requestHeader, response, request, msgInner, topicConfig)) {
        return response;
    }
    msgInner.setBody(body);
    msgInner.setFlag(requestHeader.getFlag());
    MessageAccessor.setProperties(msgInner, MessageDecoder.string2messageProperties(requestHeader.getProperties()));
    msgInner.setPropertiesString(requestHeader.getProperties());
    msgInner.setBornTimestamp(requestHeader.getBornTimestamp());
    msgInner.setBornHost(ctx.channel().remoteAddress());
    msgInner.setStoreHost(this.getStoreHost());
    msgInner.setReconsumeTimes(requestHeader.getReconsumeTimes() == null ? 0 : requestHeader.getReconsumeTimes());
    if (this.brokerController.getBrokerConfig().isRejectTransactionMessage()) {
        String traFlag = msgInner.getProperty(MessageConst.PROPERTY_TRANSACTION_PREPARED);
        if (traFlag != null) {
            response.setCode(ResponseCode.NO_PERMISSION);
            response.setRemark("the broker[" + this.brokerController.getBrokerConfig().getBrokerIP1() + "] sending transaction message is forbidden");
            return response;
        }
    }
    PutMessageResult putMessageResult = this.brokerController.getMessageStore().putMessage(msgInner);
    return handlePutMessageResult(putMessageResult, response, request, msgInner, responseHeader, sendMessageContext, ctx, queueIdInt);
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageResponseHeader(org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader) PutMessageResult(org.apache.rocketmq.store.PutMessageResult) MessageExtBrokerInner(org.apache.rocketmq.store.MessageExtBrokerInner) TopicConfig(org.apache.rocketmq.common.TopicConfig)

Example 4 with SendMessageResponseHeader

use of org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImplTest method createSuccessResponse.

private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());
    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);
    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageResponseHeader(org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader)

Example 5 with SendMessageResponseHeader

use of org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader in project rocketmq by apache.

the class SendMessageProcessor method sendBatchMessage.

private RemotingCommand sendBatchMessage(final ChannelHandlerContext ctx, final RemotingCommand request, final SendMessageContext sendMessageContext, final SendMessageRequestHeader requestHeader) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    final SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    response.setOpaque(request.getOpaque());
    response.addExtField(MessageConst.PROPERTY_MSG_REGION, this.brokerController.getBrokerConfig().getRegionId());
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, String.valueOf(this.brokerController.getBrokerConfig().isTraceOn()));
    log.debug("Receive SendMessage request command {}", request);
    final long startTimstamp = this.brokerController.getBrokerConfig().getStartAcceptSendRequestTimeStamp();
    if (this.brokerController.getMessageStore().now() < startTimstamp) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("broker unable to service, until %s", UtilAll.timeMillisToHumanString2(startTimstamp)));
        return response;
    }
    response.setCode(-1);
    super.msgCheck(ctx, requestHeader, response);
    if (response.getCode() != -1) {
        return response;
    }
    int queueIdInt = requestHeader.getQueueId();
    TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(requestHeader.getTopic());
    if (queueIdInt < 0) {
        queueIdInt = Math.abs(this.random.nextInt() % 99999999) % topicConfig.getWriteQueueNums();
    }
    if (requestHeader.getTopic().length() > Byte.MAX_VALUE) {
        response.setCode(ResponseCode.MESSAGE_ILLEGAL);
        response.setRemark("message topic length too long " + requestHeader.getTopic().length());
        return response;
    }
    if (requestHeader.getTopic() != null && requestHeader.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
        response.setCode(ResponseCode.MESSAGE_ILLEGAL);
        response.setRemark("batch request does not support retry group " + requestHeader.getTopic());
        return response;
    }
    MessageExtBatch messageExtBatch = new MessageExtBatch();
    messageExtBatch.setTopic(requestHeader.getTopic());
    messageExtBatch.setQueueId(queueIdInt);
    int sysFlag = requestHeader.getSysFlag();
    if (TopicFilterType.MULTI_TAG == topicConfig.getTopicFilterType()) {
        sysFlag |= MessageSysFlag.MULTI_TAGS_FLAG;
    }
    messageExtBatch.setSysFlag(sysFlag);
    messageExtBatch.setFlag(requestHeader.getFlag());
    MessageAccessor.setProperties(messageExtBatch, MessageDecoder.string2messageProperties(requestHeader.getProperties()));
    messageExtBatch.setBody(request.getBody());
    messageExtBatch.setBornTimestamp(requestHeader.getBornTimestamp());
    messageExtBatch.setBornHost(ctx.channel().remoteAddress());
    messageExtBatch.setStoreHost(this.getStoreHost());
    messageExtBatch.setReconsumeTimes(requestHeader.getReconsumeTimes() == null ? 0 : requestHeader.getReconsumeTimes());
    PutMessageResult putMessageResult = this.brokerController.getMessageStore().putMessages(messageExtBatch);
    return handlePutMessageResult(putMessageResult, response, request, messageExtBatch, responseHeader, sendMessageContext, ctx, queueIdInt);
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageResponseHeader(org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader) PutMessageResult(org.apache.rocketmq.store.PutMessageResult) MessageExtBatch(org.apache.rocketmq.common.message.MessageExtBatch) TopicConfig(org.apache.rocketmq.common.TopicConfig)

Aggregations

SendMessageResponseHeader (org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader)8 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)6 TopicConfig (org.apache.rocketmq.common.TopicConfig)4 PutMessageResult (org.apache.rocketmq.store.PutMessageResult)4 SendMessageHook (org.apache.rocketmq.broker.mqtrace.SendMessageHook)2 MessageExtBatch (org.apache.rocketmq.common.message.MessageExtBatch)2 MessageExtBrokerInner (org.apache.rocketmq.store.MessageExtBrokerInner)2