Search in sources :

Example 21 with SendMessageRequestHeader

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

the class MQClientAPIImplTest method testSendMessageAsync_WithException.

@Test
public void testSendMessageAsync_WithException() throws RemotingException, InterruptedException, MQBrokerException {
    doThrow(new RemotingTimeoutException("Remoting Exception in Test")).when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, 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).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, 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) InvokeCallback(org.apache.rocketmq.remoting.InvokeCallback) 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 22 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 23 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 24 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) {
                    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 25 with SendMessageRequestHeader

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

the class SendMessageProcessorTest method createSendMsgCommand.

private RemotingCommand createSendMsgCommand(int requestCode) {
    SendMessageRequestHeader requestHeader = new SendMessageRequestHeader();
    requestHeader.setProducerGroup(group);
    requestHeader.setTopic(topic);
    requestHeader.setDefaultTopic(MixAll.DEFAULT_TOPIC);
    requestHeader.setDefaultTopicQueueNums(3);
    requestHeader.setQueueId(1);
    requestHeader.setSysFlag(0);
    requestHeader.setBornTimestamp(System.currentTimeMillis());
    requestHeader.setFlag(124);
    requestHeader.setReconsumeTimes(0);
    RemotingCommand request = RemotingCommand.createRequestCommand(requestCode, requestHeader);
    request.setBody(new byte[] { 'a' });
    request.makeCustomHeaderToNet();
    return request;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)

Aggregations

SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)25 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)16 SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)14 Test (org.junit.Test)12 SendResult (org.apache.rocketmq.client.producer.SendResult)8 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)6 Mockito.doAnswer (org.mockito.Mockito.doAnswer)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 Answer (org.mockito.stubbing.Answer)6 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)4 InvokeCallback (org.apache.rocketmq.remoting.InvokeCallback)4 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)4 SendMessageContext (org.apache.rocketmq.broker.mqtrace.SendMessageContext)2 SendMessageHook (org.apache.rocketmq.broker.mqtrace.SendMessageHook)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 CheckForbiddenContext (org.apache.rocketmq.client.hook.CheckForbiddenContext)2 DefaultMQProducerImpl (org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl)2 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)2 SendCallback (org.apache.rocketmq.client.producer.SendCallback)2 TransactionSendResult (org.apache.rocketmq.client.producer.TransactionSendResult)2