Java Code Examples for com.alibaba.rocketmq.client.producer.DefaultMQProducer#shutdown()

The following examples show how to use com.alibaba.rocketmq.client.producer.DefaultMQProducer#shutdown() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Producer.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
    producer.start();

    try {
        for (int i = 0; i < 6000000; i++) {
            Message msg = new Message("TopicFilter7",// topic
                    "TagA",// tag
                    "OrderID001",// key
                    ("Hello MetaQ").getBytes(RemotingHelper.DEFAULT_CHARSET));// body

            msg.putUserProperty("SequenceId", String.valueOf(i));

            SendResult sendResult = producer.send(msg);
            System.out.println(sendResult);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    producer.shutdown();
}
 
Example 2
Source File: Producer.java    From RocketMQ-Master-analyze with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
    producer.start();

    try {
        for (int i = 0; i < 6000000; i++) {
            Message msg = new Message("TopicFilter7", // topic
                "TagA", // tag
                "OrderID001", // key
                ("Hello MetaQ").getBytes());// body

            msg.putUserProperty("SequenceId", String.valueOf(i));

            SendResult sendResult = producer.send(msg);
            System.out.println(sendResult);
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    producer.shutdown();
}
 
Example 3
Source File: Producer.java    From RocketMQ-Master-analyze with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");

    producer.start();

    for (int i = 0; i < 1000; i++) {
        try {
            Message msg = new Message("TopicTest", // topic
                "TagA", // tag
                ("Hello RocketMQ " + i).getBytes()// body
            );
            SendResult sendResult = producer.send(msg);
            System.out.println(sendResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            Thread.sleep(1000);
        }
    }

    producer.shutdown();
}
 
Example 4
Source File: Producer.java    From zheng with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    DefaultMQProducer producer = new DefaultMQProducer("Producer");
    producer.setNamesrvAddr("127.0.0.1:9876");
    try {
        producer.start();
        long time = System.currentTimeMillis();
        System.out.println("开始:" + time);

        int a = 100000;

        for (int i = 1; i <= a; i++) {
            Message msg = new Message("PushTopic", "push", i + "", "Just for test.".getBytes());
            SendResult result = producer.send(msg);
            System.out.println("id:" + result.getMsgId() + " result:" + result.getSendStatus());
        }
        System.out.println("结束,消耗:" + (System.currentTimeMillis() - time));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        producer.shutdown();
    }
}
 
Example 5
Source File: SendMsgStatusCommand.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) {
    final DefaultMQProducer producer = new DefaultMQProducer("PID_SMSC",rpcHook);
    producer.setInstanceName("PID_SMSC_" + System.currentTimeMillis());

    try {
        producer.start();
        String brokerName = commandLine.getOptionValue('b').trim();
        int messageSize = commandLine.hasOption('s') ? Integer.parseInt(commandLine.getOptionValue('s')) : 128;
        int count = commandLine.hasOption('c') ? Integer.parseInt(commandLine.getOptionValue('c')) : 50;

        producer.send(buildMessage(brokerName, 16));

        for (int i = 0; i < count; i++) {
            long begin = System.currentTimeMillis();
            SendResult result = producer.send(buildMessage(brokerName, messageSize));
            System.out.println("rt:" + (System.currentTimeMillis() - begin) + "ms, SendResult=" + result);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        producer.shutdown();
    }
}
 
Example 6
Source File: Producer.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
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",// topic
                            "TagA",// tag
                            "OrderID188",// key
                            ("Hello MetaQ").getBytes(RemotingHelper.DEFAULT_CHARSET));// body
                    SendResult sendResult = producer.send(msg);
                    System.out.println(sendResult);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        producer.shutdown();
    }
 
Example 7
Source File: SendMsgStatusCommand.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) {
    final DefaultMQProducer producer = new DefaultMQProducer("PID_SMSC");
    producer.setInstanceName("PID_SMSC_" + System.currentTimeMillis());

    try {
        producer.start();
        String brokerName = commandLine.getOptionValue('b').trim();
        int messageSize = commandLine.hasOption('s') ? Integer.parseInt(commandLine.getOptionValue('s')) : 128;
        int count = commandLine.hasOption('c') ? Integer.parseInt(commandLine.getOptionValue('s')) : 20;
        for (int i = 0; i < count; i++) {
            long begin = System.currentTimeMillis();
            SendResult result = producer.send(buildMessage(brokerName, messageSize));
            System.out.println("rt:" + (System.currentTimeMillis() - begin) + "ms, SendResult=" + result);
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        producer.shutdown();
    }
}
 
Example 8
Source File: Producer.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
    producer.start();

    try {
        for (int i = 0; i < 6000000; i++) {
            Message msg = new Message("TopicFilter7",// topic
                "TagA",// tag
                "OrderID001",// key
                ("Hello MetaQ").getBytes());// body

            msg.putUserProperty("SequenceId", String.valueOf(i));

            SendResult sendResult = producer.send(msg);
            System.out.println(sendResult);
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    producer.shutdown();
}
 
Example 9
Source File: Producer.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("yyzGroup2");
        producer.setNamesrvAddr("10.2.223.157:9876;10.2.223.158:9876;10.2.223.159:9876");
       // producer.setNamesrvAddr("10.2.223.228:9876");
        producer.start();
        for(int i = 0; i < 111111; ++i) {
//            Thread.currentThread().sleep(50);
//            for (String item : array) {
            Message msg = new Message("yyztest2",// topic
                    "TAG",// tag
                    "ffff",// 注意, msgkey对帮助业务排查消息投递问题很有帮助,请设置成和消息有关的业务属性,比如订单id ,商品id .
                    "yang ya zhou".getBytes());// body //默认会设置等待消息存储成功。
            SendResult sendResult = null;
            try {//同步发送消息 ,并且等待消息存储成功,超时时间3s .
                System.out.println("send msg with msgKey:" + msg.getKeys());
                sendResult = producer.send(msg); //DefaultMQProducer.send
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(sendResult);
            Thread.sleep(300);
        }
        System.out.println(System.getProperty("user.home") );
        producer.shutdown();
    }
 
Example 10
Source File: TestProducer.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
    producer.start();
    for (int i = 0; i < 1; i++)
        try {
            {
                Message msg = new Message("TopicTest1",// topic
                    "TagA",// tag
                    "key113",// key
                    ("Hello MetaQ").getBytes());// body
                SendResult sendResult = producer.send(msg);
                System.out.println(sendResult);

                QueryResult queryMessage =
                        producer.queryMessage("TopicTest1", "key113", 10, 0, System.currentTimeMillis());
                for (MessageExt m : queryMessage.getMessageList()) {
                    System.out.println(m);
                }
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    producer.shutdown();
}
 
Example 11
Source File: Producer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    CommandLine commandLine = buildCommandline(args);
    if (commandLine != null) {
        String group = commandLine.getOptionValue('g');
        String topic = commandLine.getOptionValue('t');
        String tags = commandLine.getOptionValue('a');
        String keys = commandLine.getOptionValue('k');
        String msgCount = commandLine.getOptionValue('c');

        DefaultMQProducer producer = new DefaultMQProducer(group);
        producer.setInstanceName(Long.toString(System.currentTimeMillis()));

        producer.start();

        for (int i = 0; i < Integer.parseInt(msgCount); i++) {
            try {
                Message msg = new Message(//
                        topic,// topic
                        tags,// tag
                        keys,// key
                        ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));// body
                SendResult sendResult = producer.send(msg);

                System.out.printf("%-8d %s%n", i, sendResult);
            } catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(1000);
            }
        }

        producer.shutdown();
    }
}
 
Example 12
Source File: TestProducer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {

        DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");

        producer.start();

        for (int i = 0; i < 1; i++)
            try {
                {
                    Message msg = new Message("TopicTest1",// topic
                            "TagA",// tag
                            "key113",// key
                            ("Hello MetaQ").getBytes(RemotingHelper.DEFAULT_CHARSET));// body
                    SendResult sendResult = producer.send(msg);
                    System.out.println(sendResult);

                    QueryResult queryMessage =
                            producer.queryMessage("TopicTest1", "key113", 10, 0, System.currentTimeMillis());
                    for (MessageExt m : queryMessage.getMessageList()) {
                        System.out.println(m);
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        producer.shutdown();
    }
 
Example 13
Source File: MqProducer.java    From RocketMqCurrencyBoot with Apache License 2.0 5 votes vote down vote up
@PreDestroy
private void Destroy()
{
	if (producer instanceof DefaultMQProducer)
	{
		DefaultMQProducer producerMq = (DefaultMQProducer) producer;
		if (producerMq != null)
		{
			producerMq.shutdown();
		}
	}
}
 
Example 14
Source File: Producer.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    CommandLine commandLine = buildCommandline(args);
    if (commandLine != null) {
        String group = commandLine.getOptionValue('g');
        String topic = commandLine.getOptionValue('t');
        String tags = commandLine.getOptionValue('a');
        String keys = commandLine.getOptionValue('k');
        String msgCount = commandLine.getOptionValue('c');

        DefaultMQProducer producer = new DefaultMQProducer(group);
        producer.setInstanceName(Long.toString(System.currentTimeMillis()));

        producer.start();

        for (int i = 0; i < Integer.parseInt(msgCount); i++) {
            try {
                Message msg = new Message(//
                    topic, // topic
                    tags, // tag
                    keys, // key
                    ("Hello RocketMQ " + i).getBytes());// body
                SendResult sendResult = producer.send(msg);

                System.out.printf("%-8d %s\n", i, sendResult);
            }
            catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(1000);
            }
        }

        producer.shutdown();
    }
}
 
Example 15
Source File: Producer.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    CommandLine commandLine = buildCommandline(args);
    if (commandLine != null) {
        String group = commandLine.getOptionValue('g');
        String topic = commandLine.getOptionValue('t');
        String tags = commandLine.getOptionValue('a');
        String keys = commandLine.getOptionValue('k');
        String msgCount = commandLine.getOptionValue('c');

        DefaultMQProducer producer = new DefaultMQProducer(group);
        producer.setInstanceName(Long.toString(System.currentTimeMillis()));

        producer.start();

        for (int i = 0; i < Integer.parseInt(msgCount); i++) {
            try {
                Message msg = new Message(//
                    topic,// topic
                    tags,// tag
                    keys,// key
                    ("Hello RocketMQ " + i).getBytes());// body
                SendResult sendResult = producer.send(msg);

                System.out.printf("%-8d %s\n", i, sendResult);
            }
            catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(1000);
            }
        }

        producer.shutdown();
    }
}
 
Example 16
Source File: SimpleConsumerProducerTest.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test
public void producerConsumerTest() throws MQClientException, InterruptedException {
    System.setProperty("rocketmq.namesrv.domain", "jmenv.tbsite.alipay.net");

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("S_fundmng_demo_producer");
    DefaultMQProducer producer = new DefaultMQProducer("P_fundmng_demo_producer");

    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
    consumer.subscribe(TOPIC_TEST, null);

    final AtomicLong lastReceivedMills = new AtomicLong(System.currentTimeMillis());

    final AtomicLong consumeTimes = new AtomicLong(0);

    consumer.registerMessageListener(new MessageListenerConcurrently() {
        public ConsumeConcurrentlyStatus consumeMessage(final List<MessageExt> msgs,
                                                        final ConsumeConcurrentlyContext context) {
            System.out.println("Received" + consumeTimes.incrementAndGet() + "messages !");

            lastReceivedMills.set(System.currentTimeMillis());

            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    consumer.start();
    producer.start();

    for (int i = 0; i < 100; i++) {
        try {
            Message msg = new Message(TOPIC_TEST, ("Hello RocketMQ " + i).getBytes());
            SendResult sendResult = producer.send(msg);
            System.out.println(sendResult);
        } catch (Exception e) {
            TimeUnit.SECONDS.sleep(1);
        }
    }

    // wait no messages
    while ((System.currentTimeMillis() - lastReceivedMills.get()) < 5000) {
        TimeUnit.MILLISECONDS.sleep(200);
    }

    consumer.shutdown();
    producer.shutdown();
}
 
Example 17
Source File: SimpleConsumerProducerTest.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void producerConsumerTest() throws MQClientException, InterruptedException {
    System.setProperty("rocketmq.namesrv.domain", "jmenv.tbsite.alipay.net");

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("S_fundmng_demo_producer");
    DefaultMQProducer producer = new DefaultMQProducer("P_fundmng_demo_producer");

    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
    consumer.subscribe(TOPIC_TEST, null);

    final AtomicLong lastReceivedMills = new AtomicLong(System.currentTimeMillis());

    final AtomicLong consumeTimes = new AtomicLong(0);

    consumer.registerMessageListener(new MessageListenerConcurrently() {
        public ConsumeConcurrentlyStatus consumeMessage(final List<MessageExt> msgs,
                                                        final ConsumeConcurrentlyContext context) {
            System.out.println("Received" + consumeTimes.incrementAndGet() + "messages !");

            lastReceivedMills.set(System.currentTimeMillis());

            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    consumer.start();
    producer.start();

    for (int i = 0; i < 100; i++) {
        try {
            Message msg = new Message(TOPIC_TEST, ("Hello RocketMQ " + i).getBytes());
            SendResult sendResult = producer.send(msg);
            System.out.println(sendResult);
        } catch (Exception e) {
            TimeUnit.SECONDS.sleep(1);
        }
    }

    // wait no messages
    while ((System.currentTimeMillis() - lastReceivedMills.get()) < 5000) {
        TimeUnit.MILLISECONDS.sleep(200);
    }

    consumer.shutdown();
    producer.shutdown();
}
 
Example 18
Source File: SimpleConsumerProducerTest.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
@Test
public void producerConsumerTest() throws MQClientException, InterruptedException {
    System.setProperty("rocketmq.namesrv.domain", "jmenv.tbsite.alipay.net");

    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("S_fundmng_demo_producer");
    DefaultMQProducer producer = new DefaultMQProducer("P_fundmng_demo_producer");

    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
    consumer.subscribe(TOPIC_TEST, null);

    final AtomicLong lastReceivedMills = new AtomicLong(System.currentTimeMillis());

    final AtomicLong consumeTimes = new AtomicLong(0);

    consumer.registerMessageListener(new MessageListenerConcurrently() {
        public ConsumeConcurrentlyStatus consumeMessage(final List<MessageExt> msgs,
                final ConsumeConcurrentlyContext context) {
            System.out.println("Received" + consumeTimes.incrementAndGet() + "messages !");

            lastReceivedMills.set(System.currentTimeMillis());

            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    consumer.start();
    producer.start();

    for (int i = 0; i < 100; i++) {
        try {
            Message msg = new Message(TOPIC_TEST, ("Hello RocketMQ " + i).getBytes());
            SendResult sendResult = producer.send(msg);
            System.out.println(sendResult);
        }
        catch (Exception e) {
            TimeUnit.SECONDS.sleep(1);
        }
    }

    // wait no messages
    while ((System.currentTimeMillis() - lastReceivedMills.get()) < 5000) {
        TimeUnit.MILLISECONDS.sleep(200);
    }

    consumer.shutdown();
    producer.shutdown();
}
 
Example 19
Source File: TestProducer.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    /**
     * 一个应用创建一个Producer,由应用来维护此对象,可以设置为全局对象或者单例<br>
     * 注意:ProducerGroupName需要由应用来保证唯一<br>
     * ProducerGroup这个概念发送普通的消息时,作用不大,但是发送分布式事务消息时,比较关键,
     * 因为服务器会回查这个Group下的任意一个Producer
     */
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
    /**
     * Producer对象在使用之前必须要调用start初始化,初始化一次即可<br>
     * 注意:切记不可以在每次发送消息时,都调用start方法
     */
    producer.start();

    /**
     * 下面这段代码表明一个Producer对象可以发送多个topic,多个tag的消息。
     * 注意:send方法是同步调用,只要不抛异常就标识成功。但是发送成功也可会有多种状态,<br>
     * 例如消息写入Master成功,但是Slave不成功,这种情况消息属于成功,但是对于个别应用如果对消息可靠性要求极高,<br>
     * 需要对这种情况做处理。另外,消息可能会存在发送失败的情况,失败重试由应用来处理。
     */
    for (int i = 0; i < 1; i++)
        try {
            {
                Message msg = new Message("TopicTest1", // topic
                    "TagA", // tag
                    "key113", // key
                    ("Hello MetaQ").getBytes());// body
                SendResult sendResult = producer.send(msg);
                System.out.println(sendResult);

                QueryResult queryMessage =
                        producer.queryMessage("TopicTest1", "key113", 10, 0, System.currentTimeMillis());
                for (MessageExt m : queryMessage.getMessageList()) {
                    System.out.println(m);
                }
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }

    /**
     * 应用退出时,要调用shutdown来清理资源,关闭网络连接,从MetaQ服务器上注销自己
     * 注意:我们建议应用在JBOSS、Tomcat等容器的退出钩子里调用shutdown方法
     */
    producer.shutdown();
}
 
Example 20
Source File: Producer.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    /**
     * 一个应用创建一个Producer,由应用来维护此对象,可以设置为全局对象或者单例<br>
     * 注意:ProducerGroupName需要由应用来保证唯一<br>
     * ProducerGroup这个概念发送普通的消息时,作用不大,但是发送分布式事务消息时,比较关键,
     * 因为服务器会回查这个Group下的任意一个Producer
     */
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");

    /**
     * Producer对象在使用之前必须要调用start初始化,初始化一次即可<br>
     * 注意:切记不可以在每次发送消息时,都调用start方法
     */
    producer.start();

    /**
     * 下面这段代码表明一个Producer对象可以发送多个topic,多个tag的消息。
     * 注意:send方法是同步调用,只要不抛异常就标识成功。但是发送成功也可会有多种状态,<br>
     * 例如消息写入Master成功,但是Slave不成功,这种情况消息属于成功,但是对于个别应用如果对消息可靠性要求极高,<br>
     * 需要对这种情况做处理。另外,消息可能会存在发送失败的情况,失败重试由应用来处理。
     */
    for (int i = 0; i < 1; i++)
        try {
            {
                Message msg = new Message("TopicTest1", // topic
                    "TagA", // tag
                    "OrderID188", // key
                    ("Hello MetaQ").getBytes());// body
                SendResult sendResult = producer.send(msg);
                System.out.println(sendResult);
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }

    /**
     * 应用退出时,要调用shutdown来清理资源,关闭网络连接,从MetaQ服务器上注销自己
     * 注意:我们建议应用在JBOSS、Tomcat等容器的退出钩子里调用shutdown方法
     */
    producer.shutdown();
}