Search in sources :

Example 1 with SendMessageHook

use of org.apache.rocketmq.broker.mqtrace.SendMessageHook 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 2 with SendMessageHook

use of org.apache.rocketmq.broker.mqtrace.SendMessageHook 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 3 with SendMessageHook

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

the class AbstractSendMessageProcessor method executeSendMessageHookBefore.

public void executeSendMessageHookBefore(final ChannelHandlerContext ctx, final RemotingCommand request, SendMessageContext context) {
    if (hasSendMessageHook()) {
        for (SendMessageHook hook : this.sendMessageHookList) {
            try {
                final SendMessageRequestHeader requestHeader = parseRequestHeader(request);
                if (null != requestHeader) {
                    context.setProducerGroup(requestHeader.getProducerGroup());
                    context.setTopic(requestHeader.getTopic());
                    context.setBodyLength(request.getBody().length);
                    context.setMsgProps(requestHeader.getProperties());
                    context.setBornHost(RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
                    context.setBrokerAddr(this.brokerController.getBrokerAddr());
                    context.setQueueId(requestHeader.getQueueId());
                }
                hook.sendMessageBefore(context);
                if (requestHeader != null) {
                    requestHeader.setProperties(context.getMsgProps());
                }
            } catch (Throwable e) {
            // Ignore
            }
        }
    }
}
Also used : SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) SendMessageHook(org.apache.rocketmq.broker.mqtrace.SendMessageHook)

Example 4 with SendMessageHook

use of org.apache.rocketmq.broker.mqtrace.SendMessageHook 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 5 with SendMessageHook

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

the class AbstractSendMessageProcessor method executeSendMessageHookBefore.

public void executeSendMessageHookBefore(final ChannelHandlerContext ctx, final RemotingCommand request, SendMessageContext context) {
    if (hasSendMessageHook()) {
        for (SendMessageHook hook : this.sendMessageHookList) {
            try {
                final SendMessageRequestHeader requestHeader = parseRequestHeader(request);
                if (null != requestHeader) {
                    context.setProducerGroup(requestHeader.getProducerGroup());
                    context.setTopic(requestHeader.getTopic());
                    context.setBodyLength(request.getBody().length);
                    context.setMsgProps(requestHeader.getProperties());
                    context.setBornHost(RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
                    context.setBrokerAddr(this.brokerController.getBrokerAddr());
                    context.setQueueId(requestHeader.getQueueId());
                }
                hook.sendMessageBefore(context);
                if (requestHeader != null) {
                    requestHeader.setProperties(context.getMsgProps());
                }
            } catch (Throwable e) {
            // Ignore
            }
        }
    }
}
Also used : SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) SendMessageHook(org.apache.rocketmq.broker.mqtrace.SendMessageHook)

Aggregations

SendMessageHook (org.apache.rocketmq.broker.mqtrace.SendMessageHook)6 ArrayList (java.util.ArrayList)2 SendMessageContext (org.apache.rocketmq.broker.mqtrace.SendMessageContext)2 SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)2 SendMessageResponseHeader (org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader)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