RocketMQ可靠同步发送代码


import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * 同步发送
 */
public class SyncProducer {
	public static void main(String[] args) throws Exception {
		DefaultMQProducer producer = new DefaultMQProducer("sync");
		producer.setNamesrvAddr("zjj101:9876");

		producer.start();
		for (int i = 0; i < 10; i++) {
			Message msg = new Message("TopicTest",
					"TagB",
					("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
			);
			SendResult sendResult = producer.send(msg);
			//调用send方法会有个sendResult这个同步相应结果,来知道我消息是否发送成功了。
			//发送的一条消息必须要等待我消息响应完了才能发送第二条消息。
			System.out.printf("%s%n%n%n", sendResult.getSendStatus() + ":(MsgId):"
					+ sendResult.getMsgId() + ":(queueId):"
					+ sendResult.getMessageQueue().getQueueId()
					+ "(value):" + new String(msg.getBody()));
		}
		producer.shutdown();
	}
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:C马雯娟 返回首页