Search in sources :

Example 11 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class Producer method main.

public static void main(String[] args) throws MQClientException, InterruptedException {
    /*
         * Instantiate with a producer group name.
         */
    // DefaultMQProducer里面的注释很全了
    DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
    // 指定NameServer地址
    producer.setNamesrvAddr("127.0.0.1:9876");
    /*
         * Specify name server addresses.
         * <p/>
         *
         * Alternatively, you may specify name server addresses via exporting environmental variable: NAMESRV_ADDR
         * <pre>
         * {@code
         * producer.setNamesrvAddr("name-server1-ip:9876;name-server2-ip:9876");
         * }
         * </pre>
         */
    /*
         * Launch the instance.
         */
    /**
     * Producer对象在使用之前必须要调用start初始化,初始化一次即可
     * 注意:切记不可以在每次发送消息时,都调用start方法
     */
    producer.start();
    producer.setSendLatencyFaultEnable(true);
    for (int i = 0; i < 5; i++) {
        try {
            /*
                 * Create a message instance, specifying topic, tag and message body.
                 */
            Message msg = new Message("TopicTest", /* Topic */
            "TagA", /* Tag */
            UUID.randomUUID().toString(), ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            // 添加定时发送
            msg.setDelayTimeLevel(3);
            /*
                 * Call send message to deliver message to one of brokers.
                 */
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
        } catch (Exception e) {
            e.printStackTrace();
            Thread.sleep(1000);
        }
    }
    /*
         * Shut down once the producer instance is not longer in use.
         */
    producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 12 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class AsyncProducer method main.

public static void main(String[] args) throws MQClientException, InterruptedException, UnsupportedEncodingException {
    DefaultMQProducer producer = new DefaultMQProducer("Jodie_Daily_test");
    producer.start();
    producer.setRetryTimesWhenSendAsyncFailed(0);
    for (int i = 0; i < 10000000; i++) {
        try {
            final int index = i;
            Message msg = new Message("Jodie_topic_1023", "TagA", "OrderID188", "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
            producer.send(msg, new SendCallback() {

                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId());
                }

                @Override
                public void onException(Throwable e) {
                    System.out.printf("%-10d Exception %s %n", index, e);
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    producer.shutdown();
}
Also used : 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) MQClientException(org.apache.rocketmq.client.exception.MQClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 13 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class Producer method main.

public static void main(String[] args) throws MQClientException, InterruptedException {
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
    producer.start();
    for (int i = 0; i < 10000000; i++) try {
        {
            Message msg = new Message("TopicTest", "TagA", "OrderID188", "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 14 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

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)

Example 15 with SendResult

use of org.apache.rocketmq.client.producer.SendResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

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)

Aggregations

SendResult (org.apache.rocketmq.client.producer.SendResult)88 Message (org.apache.rocketmq.common.message.Message)57 MQClientException (org.apache.rocketmq.client.exception.MQClientException)39 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)37 Test (org.junit.Test)31 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 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 ArrayList (java.util.ArrayList)7 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