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

The following examples show how to use com.alibaba.rocketmq.client.producer.DefaultMQProducer#start() . 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-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 2
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 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 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 5
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 6
Source File: RocketMQProducerService.java    From onetwo with Apache License 2.0 6 votes vote down vote up
@Override
	public void afterPropertiesSet() throws Exception {
		Assert.hasText(groupName);
//		Assert.hasText(namesrvAddr);
		
		DefaultMQProducer defaultMQProducer = new DefaultMQProducer(groupName);
		defaultMQProducer.setNamesrvAddr(namesrvAddr);
		defaultMQProducer.setVipChannelEnabled(false);
		defaultMQProducer.start();
		this.defaultMQProducer = defaultMQProducer;
	}
 
Example 7
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 8
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 9
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 10
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 11
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 12
Source File: RocketMqTracingCollector.java    From dubbo-plus with Apache License 2.0 5 votes vote down vote up
public RocketMqTracingCollector() {
    defaultMQProducer = new DefaultMQProducer(DstConstants.ROCKET_MQ_PRODUCER);
    defaultMQProducer.setNamesrvAddr(ConfigUtils.getProperty(DstConstants.ROCKET_MQ_NAME_SRV_ADD));
    try {
        defaultMQProducer.start();
    } catch (MQClientException e) {
        throw new IllegalArgumentException("fail to start rocketmq producer.",e);
    }
}
 
Example 13
Source File: MqProducer.java    From RocketMqCurrencyBoot with Apache License 2.0 5 votes vote down vote up
@PostConstruct
private void init() throws MQClientException
{
	if (transaction == null || transactionExecuter == null)
	{
		DefaultMQProducer defaultProducer = new DefaultMQProducer();
		// Producer 组名, 多个 Producer 如果属于一 个应用,发送同样的消息,则应该将它们 归为同一组
		defaultProducer.setProducerGroup(ProducerGroupName);
		// Name Server 地址列表
		defaultProducer.setNamesrvAddr(NamesrvAddr);
		// 生产者名称
		defaultProducer.setInstanceName(InstanceName);
		// 超时时间
		defaultProducer.setSendMsgTimeout(SendMsgTimeout);
		defaultProducer.start();
		producer = defaultProducer;
	}
	else
	{
		TransactionMQProducer transactionProducer = new TransactionMQProducer();
		// Producer 组名, 多个 Producer 如果属于一 个应用,发送同样的消息,则应该将它们 归为同一组
		transactionProducer.setProducerGroup(ProducerGroupName);
		// Name Server 地址列表
		transactionProducer.setNamesrvAddr(NamesrvAddr);
		// 生产者名称
		transactionProducer.setInstanceName(InstanceName);
		// 超时时间
		transactionProducer.setSendMsgTimeout(SendMsgTimeout);
		transactionProducer.setCheckThreadPoolMinSize(checkThreadPoolMinSize);
		transactionProducer.setCheckThreadPoolMaxSize(checkThreadPoolMaxSize);
		transactionProducer.setCheckRequestHoldMax(checkRequestHoldMax);
		transactionProducer.setTransactionCheckListener(transaction);
		transactionProducer.start();
		producer = transactionProducer;
	}

}
 
Example 14
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 15
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 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 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 18
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();
}
 
Example 19
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 20
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();
}