Search in sources :

Example 26 with SendMessageRequestHeader

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

the class MQClientAPIImplTest method testSendMessageOneWay_WithException.

@Test
public void testSendMessageOneWay_WithException() throws RemotingException, InterruptedException, MQBrokerException {
    doThrow(new RemotingTimeoutException("Remoting Exception in Test")).when(remotingClient).invokeOneway(anyString(), any(RemotingCommand.class), anyLong());
    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ONEWAY, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(RemotingException.class);
    } catch (RemotingException e) {
        assertThat(e).hasMessage("Remoting Exception in Test");
    }
    doThrow(new InterruptedException("Interrupted Exception in Test")).when(remotingClient).invokeOneway(anyString(), any(RemotingCommand.class), anyLong());
    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ONEWAY, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(InterruptedException.class);
    } catch (InterruptedException e) {
        assertThat(e).hasMessage("Interrupted Exception in Test");
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) Test(org.junit.Test)

Example 27 with SendMessageRequestHeader

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

the class MQClientAPIImplTest method testSendMessageOneWay_Success.

@Test
public void testSendMessageOneWay_Success() throws RemotingException, InterruptedException, MQBrokerException {
    doNothing().when(remotingClient).invokeOneway(anyString(), any(RemotingCommand.class), anyLong());
    SendResult sendResult = mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ONEWAY, new SendMessageContext(), defaultMQProducerImpl);
    assertThat(sendResult).isNull();
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) SendResult(org.apache.rocketmq.client.producer.SendResult) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) Test(org.junit.Test)

Example 28 with SendMessageRequestHeader

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

the class ReplyMessageProcessor method parseRequestHeader.

@Override
protected SendMessageRequestHeader parseRequestHeader(RemotingCommand request) throws RemotingCommandException {
    SendMessageRequestHeaderV2 requestHeaderV2 = null;
    SendMessageRequestHeader requestHeader = null;
    switch(request.getCode()) {
        case RequestCode.SEND_REPLY_MESSAGE_V2:
            requestHeaderV2 = (SendMessageRequestHeaderV2) request.decodeCommandCustomHeader(SendMessageRequestHeaderV2.class);
        case RequestCode.SEND_REPLY_MESSAGE:
            if (null == requestHeaderV2) {
                requestHeader = (SendMessageRequestHeader) request.decodeCommandCustomHeader(SendMessageRequestHeader.class);
            } else {
                requestHeader = SendMessageRequestHeaderV2.createSendMessageRequestHeaderV1(requestHeaderV2);
            }
        default:
            break;
    }
    return requestHeader;
}
Also used : SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) SendMessageRequestHeaderV2(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeaderV2)

Example 29 with SendMessageRequestHeader

use of org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader 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) {
                    String namespace = NamespaceUtil.getNamespaceFromResource(requestHeader.getTopic());
                    context.setNamespace(namespace);
                    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 30 with SendMessageRequestHeader

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

the class SendMessageProcessor method asyncProcessRequest.

public CompletableFuture<RemotingCommand> asyncProcessRequest(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final SendMessageContext mqtraceContext;
    switch(request.getCode()) {
        case RequestCode.CONSUMER_SEND_MSG_BACK:
            return this.asyncConsumerSendMsgBack(ctx, request);
        default:
            SendMessageRequestHeader requestHeader = parseRequestHeader(request);
            if (requestHeader == null) {
                return CompletableFuture.completedFuture(null);
            }
            mqtraceContext = buildMsgContext(ctx, requestHeader);
            this.executeSendMessageHookBefore(ctx, request, mqtraceContext);
            if (requestHeader.isBatch()) {
                return this.asyncSendBatchMessage(ctx, request, mqtraceContext, requestHeader);
            } else {
                return this.asyncSendMessage(ctx, request, mqtraceContext, requestHeader);
            }
    }
}
Also used : SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) SendMessageContext(org.apache.rocketmq.broker.mqtrace.SendMessageContext)

Aggregations

SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)40 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)26 Test (org.junit.Test)19 SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)15 SendResult (org.apache.rocketmq.client.producer.SendResult)10 Mockito.doAnswer (org.mockito.Mockito.doAnswer)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 Answer (org.mockito.stubbing.Answer)7 ByteBuffer (java.nio.ByteBuffer)6 RemotingCommandException (org.apache.rocketmq.remoting.exception.RemotingCommandException)6 InvokeCallback (org.apache.rocketmq.remoting.InvokeCallback)5 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)5 SendMessageContext (org.apache.rocketmq.broker.mqtrace.SendMessageContext)4 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)4 DefaultMQProducerImpl (org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl)4 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)4 SendCallback (org.apache.rocketmq.client.producer.SendCallback)4 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)4 SendMessageRequestHeaderV2 (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeaderV2)3 ResponseFuture (org.apache.rocketmq.remoting.netty.ResponseFuture)3