Search in sources :

Example 11 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer 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 DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer 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 DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer 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 DefaultMQProducer

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

the class AsyncSendExceptionIT method testSendMQNull.

@Test(expected = java.lang.NullPointerException.class)
public void testSendMQNull() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    MessageQueue messageQueue = null;
    producer.send(msg, messageQueue, SendCallBackFactory.getSendCallBack());
}
Also used : Message(org.apache.rocketmq.common.message.Message) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Test(org.junit.Test)

Example 15 with DefaultMQProducer

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

the class AsyncSendExceptionIT method testSendSelectorNull.

@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSendSelectorNull() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    MessageQueueSelector selector = null;
    producer.send(msg, selector, 100, SendCallBackFactory.getSendCallBack());
}
Also used : MessageQueueSelector(org.apache.rocketmq.client.producer.MessageQueueSelector) Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Test(org.junit.Test)

Aggregations

DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)90 Message (org.apache.rocketmq.common.message.Message)63 SendResult (org.apache.rocketmq.client.producer.SendResult)36 MQClientException (org.apache.rocketmq.client.exception.MQClientException)31 Test (org.junit.Test)27 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)17 MessageQueueSelector (org.apache.rocketmq.client.producer.MessageQueueSelector)13 SendCallback (org.apache.rocketmq.client.producer.SendCallback)11 ArrayList (java.util.ArrayList)10 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)8 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)8 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 MQProducer (org.apache.rocketmq.client.producer.MQProducer)5 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)4 CommandLine (org.apache.commons.cli.CommandLine)3 IOException (java.io.IOException)2 BigDecimal (java.math.BigDecimal)2 Date (java.util.Date)2 LinkedList (java.util.LinkedList)2