码迷,mamicode.com
首页 > 其他好文 > 详细

rocketmq的生产者生产消息

时间:2019-02-04 11:38:02      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:top   out   bytes   throw   com   tst   生产   rocketmq   client   

package com.bfxy.rocketmq.model;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import com.bfxy.rocketmq.constants.Const;

public class Producer {

public static void main(String[] args) throws MQClientException, InterruptedException {
String group_name = "test_model_producer_name";
DefaultMQProducer producer = new DefaultMQProducer(group_name);
producer.setNamesrvAddr(Const.NAMESRV_ADDR_MASTER_SLAVE);
producer.start();

for (int i = 0; i < 10; i++) {
try {
String tag = (i % 2 ==0) ? "TagA":"TagB";
Message msg = new Message("test_model_topic2",// topic
tag,// tag
("信息内容" + i).getBytes()// body
);
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
}
catch (Exception e) {
e.printStackTrace();
Thread.sleep(1000);
}
}

producer.shutdown();
}
}

rocketmq的生产者生产消息

标签:top   out   bytes   throw   com   tst   生产   rocketmq   client   

原文地址:https://www.cnblogs.com/zhangzhiqin/p/10351555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!