Search in sources :

Example 1 with SendMessageContext

use of org.apache.rocketmq.broker.mqtrace.SendMessageContext in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class SendMessageProcessor method processRequest.

@Override
public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    SendMessageContext mqtraceContext;
    switch(request.getCode()) {
        case // Broker Consumer将处理不了的消息发回服务器
        RequestCode.CONSUMER_SEND_MSG_BACK:
            return this.consumerSendMsgBack(ctx, request);
        default:
            // 解析请求
            SendMessageRequestHeader requestHeader = parseRequestHeader(request);
            if (requestHeader == null) {
                return null;
            }
            // 消息轨迹:记录到达 broker 的消息
            mqtraceContext = buildMsgContext(ctx, requestHeader);
            // 处理发送消息前逻辑
            this.executeSendMessageHookBefore(ctx, request, mqtraceContext);
            RemotingCommand response;
            if (requestHeader.isBatch()) {
                response = this.sendBatchMessage(ctx, request, mqtraceContext, requestHeader);
            } else {
                // 处理发送消息逻辑
                response = this.sendMessage(ctx, request, mqtraceContext, requestHeader);
            }
            // 消息轨迹:记录发送成功的消息
            this.executeSendMessageHookAfter(response, mqtraceContext);
            return response;
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) SendMessageContext(org.apache.rocketmq.broker.mqtrace.SendMessageContext)

Example 2 with SendMessageContext

use of org.apache.rocketmq.broker.mqtrace.SendMessageContext in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class SendMessageProcessorTest method testProcessRequest_WithHook.

@Test
public void testProcessRequest_WithHook() throws RemotingCommandException {
    when(messageStore.putMessage(any(MessageExtBrokerInner.class))).thenReturn(new PutMessageResult(PutMessageStatus.PUT_OK, new AppendMessageResult(AppendMessageStatus.PUT_OK)));
    List<SendMessageHook> sendMessageHookList = new ArrayList<>();
    final SendMessageContext[] sendMessageContext = new SendMessageContext[1];
    SendMessageHook sendMessageHook = new SendMessageHook() {

        @Override
        public String hookName() {
            return null;
        }

        @Override
        public void sendMessageBefore(SendMessageContext context) {
            sendMessageContext[0] = context;
        }

        @Override
        public void sendMessageAfter(SendMessageContext context) {
        }
    };
    sendMessageHookList.add(sendMessageHook);
    sendMessageProcessor.registerSendMessageHook(sendMessageHookList);
    assertPutResult(ResponseCode.SUCCESS);
    System.out.println(sendMessageContext[0]);
    assertThat(sendMessageContext[0]).isNotNull();
    assertThat(sendMessageContext[0].getTopic()).isEqualTo(topic);
    assertThat(sendMessageContext[0].getProducerGroup()).isEqualTo(group);
}
Also used : PutMessageResult(org.apache.rocketmq.store.PutMessageResult) SendMessageContext(org.apache.rocketmq.broker.mqtrace.SendMessageContext) ArrayList(java.util.ArrayList) MessageExtBrokerInner(org.apache.rocketmq.store.MessageExtBrokerInner) AppendMessageResult(org.apache.rocketmq.store.AppendMessageResult) SendMessageHook(org.apache.rocketmq.broker.mqtrace.SendMessageHook) Test(org.junit.Test)

Example 3 with SendMessageContext

use of org.apache.rocketmq.broker.mqtrace.SendMessageContext in project rocketmq by apache.

the class SendMessageProcessor method processRequest.

@Override
public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    SendMessageContext mqtraceContext;
    switch(request.getCode()) {
        case RequestCode.CONSUMER_SEND_MSG_BACK:
            return this.consumerSendMsgBack(ctx, request);
        default:
            SendMessageRequestHeader requestHeader = parseRequestHeader(request);
            if (requestHeader == null) {
                return null;
            }
            mqtraceContext = buildMsgContext(ctx, requestHeader);
            this.executeSendMessageHookBefore(ctx, request, mqtraceContext);
            RemotingCommand response;
            if (requestHeader.isBatch()) {
                response = this.sendBatchMessage(ctx, request, mqtraceContext, requestHeader);
            } else {
                response = this.sendMessage(ctx, request, mqtraceContext, requestHeader);
            }
            this.executeSendMessageHookAfter(response, mqtraceContext);
            return response;
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) SendMessageContext(org.apache.rocketmq.broker.mqtrace.SendMessageContext)

Example 4 with SendMessageContext

use of org.apache.rocketmq.broker.mqtrace.SendMessageContext in project rocketmq by apache.

the class SendMessageProcessorTest method testProcessRequest_WithHook.

@Test
public void testProcessRequest_WithHook() throws RemotingCommandException {
    when(messageStore.putMessage(any(MessageExtBrokerInner.class))).thenReturn(new PutMessageResult(PutMessageStatus.PUT_OK, new AppendMessageResult(AppendMessageStatus.PUT_OK)));
    List<SendMessageHook> sendMessageHookList = new ArrayList<>();
    final SendMessageContext[] sendMessageContext = new SendMessageContext[1];
    SendMessageHook sendMessageHook = new SendMessageHook() {

        @Override
        public String hookName() {
            return null;
        }

        @Override
        public void sendMessageBefore(SendMessageContext context) {
            sendMessageContext[0] = context;
        }

        @Override
        public void sendMessageAfter(SendMessageContext context) {
        }
    };
    sendMessageHookList.add(sendMessageHook);
    sendMessageProcessor.registerSendMessageHook(sendMessageHookList);
    assertPutResult(ResponseCode.SUCCESS);
    System.out.println(sendMessageContext[0]);
    assertThat(sendMessageContext[0]).isNotNull();
    assertThat(sendMessageContext[0].getTopic()).isEqualTo(topic);
    assertThat(sendMessageContext[0].getProducerGroup()).isEqualTo(group);
}
Also used : PutMessageResult(org.apache.rocketmq.store.PutMessageResult) SendMessageContext(org.apache.rocketmq.broker.mqtrace.SendMessageContext) ArrayList(java.util.ArrayList) MessageExtBrokerInner(org.apache.rocketmq.store.MessageExtBrokerInner) AppendMessageResult(org.apache.rocketmq.store.AppendMessageResult) SendMessageHook(org.apache.rocketmq.broker.mqtrace.SendMessageHook) Test(org.junit.Test)

Example 5 with SendMessageContext

use of org.apache.rocketmq.broker.mqtrace.SendMessageContext in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class AbstractSendMessageProcessor method buildMsgContext.

protected SendMessageContext buildMsgContext(ChannelHandlerContext ctx, SendMessageRequestHeader requestHeader) {
    if (!this.hasSendMessageHook()) {
        return null;
    }
    SendMessageContext mqtraceContext;
    mqtraceContext = new SendMessageContext();
    mqtraceContext.setProducerGroup(requestHeader.getProducerGroup());
    mqtraceContext.setTopic(requestHeader.getTopic());
    mqtraceContext.setMsgProps(requestHeader.getProperties());
    mqtraceContext.setBornHost(RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
    mqtraceContext.setBrokerAddr(this.brokerController.getBrokerAddr());
    mqtraceContext.setBrokerRegionId(this.brokerController.getBrokerConfig().getRegionId());
    mqtraceContext.setBornTimeStamp(requestHeader.getBornTimestamp());
    Map<String, String> properties = MessageDecoder.string2messageProperties(requestHeader.getProperties());
    String uniqueKey = properties.get(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX);
    properties.put(MessageConst.PROPERTY_MSG_REGION, this.brokerController.getBrokerConfig().getRegionId());
    properties.put(MessageConst.PROPERTY_TRACE_SWITCH, String.valueOf(this.brokerController.getBrokerConfig().isTraceOn()));
    requestHeader.setProperties(MessageDecoder.messageProperties2String(properties));
    if (uniqueKey == null) {
        uniqueKey = "";
    }
    mqtraceContext.setMsgUniqueKey(uniqueKey);
    return mqtraceContext;
}
Also used : SendMessageContext(org.apache.rocketmq.broker.mqtrace.SendMessageContext)

Aggregations

SendMessageContext (org.apache.rocketmq.broker.mqtrace.SendMessageContext)6 ArrayList (java.util.ArrayList)2 SendMessageHook (org.apache.rocketmq.broker.mqtrace.SendMessageHook)2 SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)2 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)2 AppendMessageResult (org.apache.rocketmq.store.AppendMessageResult)2 MessageExtBrokerInner (org.apache.rocketmq.store.MessageExtBrokerInner)2 PutMessageResult (org.apache.rocketmq.store.PutMessageResult)2 Test (org.junit.Test)2