rocketmq的生产者生产消息
Posted zhangzhiqin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rocketmq的生产者生产消息相关的知识,希望对你有一定的参考价值。
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的生产者生产消息的主要内容,如果未能解决你的问题,请参考以下文章