Search in sources :

Example 31 with SendMessageRequestHeader

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

the class MQClientAPIImplTest method testSendMessageSync_Success.

@Test
public void testSendMessageSync_Success() throws InterruptedException, RemotingException, MQBrokerException {
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            RemotingCommand request = mock.getArgument(1);
            return createSuccessResponse(request);
        }
    }).when(remotingClient).invokeSync(anyString(), any(RemotingCommand.class), anyLong());
    SendMessageRequestHeader requestHeader = createSendMessageRequestHeader();
    SendResult sendResult = mqClientAPI.sendMessage(brokerAddr, brokerName, msg, requestHeader, 3 * 1000, CommunicationMode.SYNC, new SendMessageContext(), defaultMQProducerImpl);
    assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
    assertThat(sendResult.getOffsetMsgId()).isEqualTo("123");
    assertThat(sendResult.getQueueOffset()).isEqualTo(123L);
    assertThat(sendResult.getMessageQueue().getQueueId()).isEqualTo(1);
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SendResult(org.apache.rocketmq.client.producer.SendResult) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) Test(org.junit.Test)

Example 32 with SendMessageRequestHeader

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

the class MQClientAPIImplTest method testSendMessageAsync_Success.

@Test
public void testSendMessageAsync_Success() throws RemotingException, InterruptedException, MQBrokerException {
    doNothing().when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    SendResult sendResult = mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, new SendMessageContext(), defaultMQProducerImpl);
    assertThat(sendResult).isNull();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            InvokeCallback callback = mock.getArgument(3);
            RemotingCommand request = mock.getArgument(1);
            ResponseFuture responseFuture = new ResponseFuture(null, request.getOpaque(), 3 * 1000, null, null);
            responseFuture.setResponseCommand(createSuccessResponse(request));
            callback.operationComplete(responseFuture);
            return null;
        }
    }).when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    SendMessageContext sendMessageContext = new SendMessageContext();
    sendMessageContext.setProducer(new DefaultMQProducerImpl(new DefaultMQProducer()));
    mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
            assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
            assertThat(sendResult.getOffsetMsgId()).isEqualTo("123");
            assertThat(sendResult.getQueueOffset()).isEqualTo(123L);
            assertThat(sendResult.getMessageQueue().getQueueId()).isEqualTo(1);
        }

        @Override
        public void onException(Throwable e) {
        }
    }, null, null, 0, sendMessageContext, defaultMQProducerImpl);
}
Also used : InvokeCallback(org.apache.rocketmq.remoting.InvokeCallback) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) ResponseFuture(org.apache.rocketmq.remoting.netty.ResponseFuture) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SendResult(org.apache.rocketmq.client.producer.SendResult) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) DefaultMQProducerImpl(org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl) SendCallback(org.apache.rocketmq.client.producer.SendCallback) Test(org.junit.Test)

Example 33 with SendMessageRequestHeader

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

the class MQClientAPIImplTest method createSendMessageRequestHeader.

private SendMessageRequestHeader createSendMessageRequestHeader() {
    SendMessageRequestHeader requestHeader = new SendMessageRequestHeader();
    requestHeader.setBornTimestamp(System.currentTimeMillis());
    requestHeader.setTopic(topic);
    requestHeader.setProducerGroup(group);
    requestHeader.setQueueId(1);
    requestHeader.setMaxReconsumeTimes(10);
    return requestHeader;
}
Also used : SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)

Example 34 with SendMessageRequestHeader

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

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));
    SendMessageContext sendMessageContext = new SendMessageContext();
    sendMessageContext.setProducer(new DefaultMQProducerImpl(new DefaultMQProducer()));
    mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
        }

        @Override
        public void onException(Throwable e) {
            assertThat(e).hasMessage("Remoting Exception in Test");
        }
    }, null, null, 0, sendMessageContext, defaultMQProducerImpl);
    doThrow(new InterruptedException("Interrupted Exception in Test")).when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
        }

        @Override
        public void onException(Throwable e) {
            assertThat(e).hasMessage("Interrupted Exception in Test");
        }
    }, null, null, 0, sendMessageContext, defaultMQProducerImpl);
}
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) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducerImpl(org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) SendCallback(org.apache.rocketmq.client.producer.SendCallback) Test(org.junit.Test)

Example 35 with SendMessageRequestHeader

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

the class PlainAccessValidatorTest method validateNullAccessKeyTest.

@Test(expected = AclException.class)
public void validateNullAccessKeyTest() {
    SessionCredentials sessionCredentials = new SessionCredentials();
    sessionCredentials.setAccessKey("RocketMQ1");
    sessionCredentials.setSecretKey("1234");
    AclClientRPCHook aclClientRPCHook = new AclClientRPCHook(sessionCredentials);
    SendMessageRequestHeader messageRequestHeader = new SendMessageRequestHeader();
    messageRequestHeader.setTopic("topicB");
    RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, messageRequestHeader);
    aclClientRPCHook.doBeforeRequest("", remotingCommand);
    ByteBuffer buf = remotingCommand.encodeHeader();
    buf.getInt();
    buf = ByteBuffer.allocate(buf.limit() - buf.position()).put(buf);
    buf.position(0);
    try {
        PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(RemotingCommand.decode(buf), "192.168.1.1");
        plainAccessValidator.validate(accessResource);
    } catch (RemotingCommandException e) {
        e.printStackTrace();
        Assert.fail("Should not throw IOException");
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SessionCredentials(org.apache.rocketmq.acl.common.SessionCredentials) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) ByteBuffer(java.nio.ByteBuffer) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) Test(org.junit.Test)

Aggregations

SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)67 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)43 Test (org.junit.Test)32 SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)23 SendResult (org.apache.rocketmq.client.producer.SendResult)16 ByteBuffer (java.nio.ByteBuffer)12 RemotingCommandException (org.apache.rocketmq.remoting.exception.RemotingCommandException)12 Mockito.doAnswer (org.mockito.Mockito.doAnswer)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 Answer (org.mockito.stubbing.Answer)11 InvokeCallback (org.apache.rocketmq.remoting.InvokeCallback)8 DefaultMQProducerImpl (org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl)7 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)7 SendCallback (org.apache.rocketmq.client.producer.SendCallback)7 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)7 SendMessageContext (org.apache.rocketmq.broker.mqtrace.SendMessageContext)6 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)6 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)6 SendMessageRequestHeaderV2 (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeaderV2)5 ResponseFuture (org.apache.rocketmq.remoting.netty.ResponseFuture)5