packageorg.rocketmq;importorg.apache.rocketmq.client.consumer.DefaultMQPushConsumer;importorg.apache.rocketmq.client.exception.MQBrokerException;importorg.apache.rocketmq.client.exception.MQClientException;importorg.apache.rocketmq.client.producer.*;importorg.apache.rocketmq.common.message.Message;importorg.apache.rocketmq.common.message.MessageExt;importorg.apache.rocketmq.common.message.MessageQueue;importorg.apache.rocketmq.remoting.exception.RemotingException;importjavax.security.auth.callback.Callback;importjava.util.ArrayList;importjava.util.List;publicclassRocketMQProducer{/**
* 1.1普通消息发送之同步消息发送
*/privatestaticvoidsendSyncMessage()throwsMQClientException,RemotingException,InterruptedException,MQBrokerException{// 实例化一个producer对象 && 设置生产者组DefaultMQProducer producer =newDefaultMQProducer("Study");// 设置MQ的nameServer的地址
producer.setNamesrvAddr("172.17.0.2:9876");// 开始producer发送消息
producer.start();// // 1.1 以单条消息为粒度发送100条消息体为: this is test message of normal & sync & single : number of xxx;//// // 获取message对象//// Message message ;//// for (int i = 1; i <= 100; i++) {//// // 创建消息对象 参数:Topic:TopicOneTest tags:normal & sync & single body:this is test message of normal & sync & single : number of xxx// message = new Message("TopicOneTest","normal & sync & single",("this is test message of normal & sync & single : number of " + i).getBytes());//// // 发送消息// producer.send(message);// }// 1.2 以10条记录为一批次发送400~499的共计100条消息,消息体为: this is test message of nomal & sync & batch : number of xxx;// 批量消息的大小不能超过 1MiB && 同一批 batch 中 topic 必须相同// 创建List对象List<Message> msgList =newArrayList<Message>();for(int i =400; i <500; i++){// 往消息列表中添加消息
msgList.add(newMessage("TopicOneTest","normal & sync & batch",("this is test message of normal & sync & batch : number of "+ i).getBytes()));// 判断如果消息为某一批次的最后一个元素,则发送消息并且清空消息列表if(i %10==9){// 发送消息
producer.send(msgList);// 清空消息列表
msgList.clear();}}// 关闭producer
producer.shutdown();}/**
* 1.2普通消息之异步消息发送
*/privatestaticvoidsendAsyncMessage()throwsMQClientException,RemotingException,InterruptedException,MQBrokerException{// 实例化一个生产者DefaultMQProducer producer =newDefaultMQProducer("Study");// 设置nameService地址
producer.setNamesrvAddr("172.17.0.2:9876");// 开始生产者
producer.start();// 设置发送失败重试次数
producer.setRetryTimesWhenSendAsyncFailed(0);// 批量发送消息,从200到299,消息体为 : this is test message of normal & async & single : number of xxxfor(int i =200; i <300; i++){// 发送异步消息
producer.send(newMessage("TopicOneTest","normal & async & single",("this is test message of normal & async & single : number of "+ i).getBytes()),newSendCallback(){@OverridepublicvoidonSuccess(SendResult sendResult){// 返回结果发送成功了需要做什么事情System.out.println(sendResult.getMsgId()+" : "+"消息消息发送成功了");}@OverridepublicvoidonException(Throwable throwable){// 返回结果发送异常了做什么事情System.out.println(throwable);}});}// 尝试使用批量发送方式// !!! 异步发送只支持单条发送// 并不能结束生产者// producer.shutdown();}/**
* 单向发送
*/privatestaticvoidsendOneWayMessage()throwsMQClientException,RemotingException,InterruptedException{// 实例化生产者对象DefaultMQProducer producer =newDefaultMQProducer("Study");// 设置NameServer的地址
producer.setNamesrvAddr("172.17.0.2:9876");// 生产者开始发送消息
producer.start();// 发送666到888的消息: 大江东去浪淘尽,奔流到海不复回:xxxfor(int i =10000000; i <=10000900; i++){
producer.sendOneway(newMessage("TopicOneTest","normal & oneWay & single",("大江东去浪淘尽,奔流到海不复回:"+ i).getBytes()));System.out.println("Test"+ i);}// 测试能不能停止
producer.shutdown();}/**
* 2.顺序消息
* 顺序消息的实现是通过相同的key发送到相同的分区(queue)中
*/privatestaticvoidsendMessageOrder()throwsMQClientException,RemotingException,InterruptedException,MQBrokerException{// 实例化一个生产者DefaultMQProducer producer =newDefaultMQProducer("Study");// 设置nameServer的地址
producer.setNamesrvAddr("172.17.0.2:9876");// 开始发送消息
producer.start();Message msg;for(int i =1000; i <1100; i++){//构建消息对象
msg =newMessage("TopicOneTest","order & single",("this is a message of order : num of "+ i).getBytes());// 发送消息(根据订单ID发送到不同的队列里) // 认为i为订单编号
producer.send(msg,newMessageQueueSelector(){@OverridepublicMessageQueueselect(List<MessageQueue> list,Message message,Object o){// 实现队列选择器// 根据订单ID决定分发到主题中的那个队列里(分区)int orderId =(int)o;// 将订单ID转化为整数型return list.get(orderId % list.size());}}, i);}// 关闭连接
producer.shutdown();}/**
* 3.消息延迟发送
* */privatestaticvoidsendMessageDelay()throwsMQClientException,RemotingException,InterruptedException,MQBrokerException{// 实例化生产者对象DefaultMQProducer producer =newDefaultMQProducer("Study");// 设置nameServer
producer.setNamesrvAddr("172.17.0.2:9876");// 发送消息
producer.start();Message msg;for(int i =1700; i <1800; i++){
msg =newMessage("TopicOneTest","delay & single",("this is a message of delay : number is "+ i).getBytes());if(i %3==0){// 设置消息延迟登记
msg.setDelayTimeLevel(4);}
producer.send(msg);}
producer.shutdown();}/**
* 4.事务消息发送(暂未实践)
* */privatestaticvoidsendMessageTransaction(){// 实现接口TransactionListener transactionListener =newTransactionListener(){@OverridepublicLocalTransactionStateexecuteLocalTransaction(Message message,Object o){returnnull;}@OverridepublicLocalTransactionStatecheckLocalTransaction(MessageExt messageExt){returnnull;}};}/**
* 5.发送属性消息
* */privatestaticvoidsendPropertyMessage()throwsMQClientException,RemotingException,InterruptedException,MQBrokerException{// 实例化生产者DefaultMQProducer producer =newDefaultMQProducer("Study");// 设置nameServer
producer.setNamesrvAddr("172.17.0.2:9876");// 实例化消息
producer.start();Message message =newMessage("TopicOneTest","Tag-A","This is message send by whh on 20201007, is used by Test!!".getBytes());
message.putUserProperty("user","whh");
message.putUserProperty("time","20221007");
message.putUserProperty("use","Test");
producer.send(message);
producer.shutdown();}publicstaticvoidmain(String[] args)throwsInterruptedException,RemotingException,MQClientException,MQBrokerException{// System.out.println("hello world");// 普通消息 之 同步消息发送// sendSyncMessage();// 普通消息之异步消息发送// sendAsyncMessage();// 发送单向消息// sendOneWayMessage();// 发送顺序消息// sendMessageOrder();// 发送延迟消息// sendMessageDelay();sendPropertyMessage();}}