Search in sources :

Example 91 with SendResult

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

the class BatchSendIT method testBatchSend_CheckProperties.

@Test
public void testBatchSend_CheckProperties() throws Exception {
    List<Message> messageList = new ArrayList<>();
    Message message = new Message();
    message.setTopic(topic);
    message.setKeys("keys123");
    message.setTags("tags123");
    message.setWaitStoreMsgOK(false);
    message.setBuyerId("buyerid123");
    message.setFlag(123);
    message.setBody("body".getBytes());
    messageList.add(message);
    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);
    Message messageByOffset = producer.viewMessage(offsetIds[0]);
    Message messageByMsgId = producer.viewMessage(topic, msgIds[0]);
    System.out.println(messageByOffset);
    System.out.println(messageByMsgId);
    Assert.assertEquals(message.getTopic(), messageByMsgId.getTopic());
    Assert.assertEquals(message.getTopic(), messageByOffset.getTopic());
    Assert.assertEquals(message.getKeys(), messageByOffset.getKeys());
    Assert.assertEquals(message.getKeys(), messageByMsgId.getKeys());
    Assert.assertEquals(message.getTags(), messageByOffset.getTags());
    Assert.assertEquals(message.getTags(), messageByMsgId.getTags());
    Assert.assertEquals(message.isWaitStoreMsgOK(), messageByOffset.isWaitStoreMsgOK());
    Assert.assertEquals(message.isWaitStoreMsgOK(), messageByMsgId.isWaitStoreMsgOK());
    Assert.assertEquals(message.getBuyerId(), messageByOffset.getBuyerId());
    Assert.assertEquals(message.getBuyerId(), messageByMsgId.getBuyerId());
    Assert.assertEquals(message.getFlag(), messageByOffset.getFlag());
    Assert.assertEquals(message.getFlag(), messageByMsgId.getFlag());
}
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)

Example 92 with SendResult

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

the class MessageExceptionIT method testProducerSmoke.

@Test
public void testProducerSmoke() {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    SendResult sendResult = null;
    try {
        sendResult = producer.send(msg);
    } catch (Exception e) {
    }
    assertThat(sendResult).isNotEqualTo(null);
    assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) Test(org.junit.Test)

Example 93 with SendResult

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

the class CanalRocketMQProducer method sendMessage.

@SuppressWarnings("deprecation")
private void sendMessage(List<Message> messages, int partition) {
    if (messages.isEmpty()) {
        return;
    }
    // 获取一下messageQueue
    DefaultMQProducerImpl innerProducer = this.defaultMQProducer.getDefaultMQProducerImpl();
    TopicPublishInfo topicInfo = innerProducer.getTopicPublishInfoTable().get(messages.get(0).getTopic());
    if (topicInfo == null) {
        for (Message message : messages) {
            sendMessage(message, partition);
        }
    } else {
        // 批量发送
        List<MessageQueue> queues = topicInfo.getMessageQueueList();
        int size = queues.size();
        if (size <= 0) {
            // 可能是第一次创建
            for (Message message : messages) {
                sendMessage(message, partition);
            }
        } else {
            MessageQueue queue;
            if (partition >= size) {
                queue = queues.get(partition % size);
            } else {
                queue = queues.get(partition);
            }
            try {
                // 阿里云RocketMQ暂不支持批量发送消息,当canal.mq.flatMessage = true时,会发送失败
                SendResult sendResult = this.defaultMQProducer.send(messages, queue);
                if (logger.isDebugEnabled()) {
                    logger.debug("Send Message Result: {}", sendResult);
                }
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : TopicPublishInfo(org.apache.rocketmq.client.impl.producer.TopicPublishInfo) Message(org.apache.rocketmq.common.message.Message) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducerImpl(org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl)

Example 94 with SendResult

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

the class RocketMQOriginalProducer method sendAsync.

@GetMapping("/original/sendAsync")
public String sendAsync() throws UnsupportedEncodingException, RemotingException, MQClientException, InterruptedException {
    Message msg = new Message("TopicTest", "TagA", "OrderID188", "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
    producer.send(msg, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
            System.out.printf(sendResult.getMsgId());
        }

        @Override
        public void onException(Throwable e) {
            System.out.printf("Exception", e);
            e.printStackTrace();
        }
    });
    return "success";
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) SendCallback(org.apache.rocketmq.client.producer.SendCallback) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 95 with SendResult

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

the class RocketMQSpringBootProducer method templatesendAsync.

@GetMapping("/template/sendAsync")
public String templatesendAsync() {
    String topic = "TopicTest";
    rocketMQTemplate.asyncSend(topic, MessageBuilder.withPayload("Hello, World!2222".getBytes(UTF8)).build(), new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
            System.out.printf("async onSucess SendResult=%s %n", sendResult);
        }

        @Override
        public void onException(Throwable e) {
            System.out.printf("async onException Throwable=%s %n", e);
        }
    });
    return "success";
}
Also used : SendResult(org.apache.rocketmq.client.producer.SendResult) SendCallback(org.apache.rocketmq.client.producer.SendCallback) GetMapping(org.springframework.web.bind.annotation.GetMapping)

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