Search in sources :

Example 86 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project rocketmq by apache.

the class ProducerImplTest method testSend_Not_OK.

@Test
public void testSend_Not_OK() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
    SendResult sendResult = new SendResult();
    sendResult.setSendStatus(SendStatus.FLUSH_DISK_TIMEOUT);
    when(rocketmqProducer.send(any(Message.class), anyLong())).thenReturn(sendResult);
    try {
        producer.send(producer.createBytesMessageToTopic("HELLO_TOPIC", new byte[] { 'a' }));
        failBecauseExceptionWasNotThrown(OMSRuntimeException.class);
    } catch (Exception e) {
        assertThat(e).hasMessageContaining("Send message to RocketMQ broker failed.");
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) OMSRuntimeException(io.openmessaging.exception.OMSRuntimeException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) Test(org.junit.Test)

Example 87 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project rocketmq by apache.

the class SequenceProducerImplTest method testSend_WithCommit.

@Test
public void testSend_WithCommit() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
    SendResult sendResult = new SendResult();
    sendResult.setMsgId("TestMsgID");
    sendResult.setSendStatus(SendStatus.SEND_OK);
    when(rocketmqProducer.send(ArgumentMatchers.<Message>anyList())).thenReturn(sendResult);
    when(rocketmqProducer.getMaxMessageSize()).thenReturn(1024);
    final BytesMessage message = producer.createBytesMessageToTopic("HELLO_TOPIC", new byte[] { 'a' });
    producer.send(message);
    producer.commit();
    assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo("TestMsgID");
}
Also used : SendResult(org.apache.rocketmq.client.producer.SendResult) BytesMessage(io.openmessaging.BytesMessage) Test(org.junit.Test)

Example 88 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project rocketmq by apache.

the class AsyncSendExceptionIT method testQueueIdBigThanQueueNum.

@Test
public void testQueueIdBigThanQueueNum() throws Exception {
    int queueId = 100;
    sendFail = false;
    MessageQueue mq = new MessageQueue(topic, broker1Name, queueId);
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    producer.send(msg, mq, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
        }

        @Override
        public void onException(Throwable throwable) {
            sendFail = true;
        }
    });
    int checkNum = 50;
    while (!sendFail && checkNum > 0) {
        checkNum--;
        TestUtils.waitForMoment(100);
    }
    producer.shutdown();
    assertThat(sendFail).isEqualTo(true);
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) SendCallback(org.apache.rocketmq.client.producer.SendCallback) Test(org.junit.Test)

Example 89 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project rocketmq by apache.

the class AsyncSendExceptionIT method testQueueIdSmallZero.

@Test
public void testQueueIdSmallZero() throws Exception {
    int queueId = -100;
    sendFail = true;
    MessageQueue mq = new MessageQueue(topic, broker1Name, queueId);
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    producer.send(msg, mq, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
            sendFail = false;
        }

        @Override
        public void onException(Throwable throwable) {
            sendFail = true;
        }
    });
    int checkNum = 50;
    while (sendFail && checkNum > 0) {
        checkNum--;
        TestUtils.waitForMoment(100);
    }
    producer.shutdown();
    assertThat(sendFail).isEqualTo(false);
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) SendCallback(org.apache.rocketmq.client.producer.SendCallback) Test(org.junit.Test)

Example 90 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project rocketmq by apache.

the class BatchSendIT method testBatchSend_ViewMessage.

@Test
public void testBatchSend_ViewMessage() throws Exception {
    List<Message> messageList = new ArrayList<>();
    int batchNum = 100;
    for (int i = 0; i < batchNum; i++) {
        messageList.add(new Message(topic, RandomUtils.getStringByUUID().getBytes()));
    }
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    SendResult sendResult = producer.send(messageList);
    Assert.assertEquals(SendStatus.SEND_OK, sendResult.getSendStatus());
    String[] offsetIds = sendResult.getOffsetMsgId().split(",");
    String[] msgIds = sendResult.getMsgId().split(",");
    Assert.assertEquals(messageList.size(), offsetIds.length);
    Assert.assertEquals(messageList.size(), msgIds.length);
    Thread.sleep(2000);
    for (int i = 0; i < 3; i++) {
        producer.viewMessage(offsetIds[random.nextInt(batchNum)]);
    }
    for (int i = 0; i < 3; i++) {
        producer.viewMessage(topic, msgIds[random.nextInt(batchNum)]);
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) ArrayList(java.util.ArrayList) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Test(org.junit.Test)

Aggregations

SendResult (org.apache.rocketmq.client.producer.SendResult)95 Message (org.apache.rocketmq.common.message.Message)64 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)42 MQClientException (org.apache.rocketmq.client.exception.MQClientException)39 Test (org.junit.Test)32 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)15 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)15 SendCallback (org.apache.rocketmq.client.producer.SendCallback)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)11 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)10 ArrayList (java.util.ArrayList)8 SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)8 SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)8 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)8 IOException (java.io.IOException)6 TransactionSendResult (org.apache.rocketmq.client.producer.TransactionSendResult)6 RemotingConnectException (org.apache.rocketmq.remoting.exception.RemotingConnectException)6 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)6 MessageExt (org.apache.rocketmq.common.message.MessageExt)5 BytesMessage (io.openmessaging.BytesMessage)4