参考 https://blog.csdn.net/asdf08442a/article/details/54882769 整理出来的测试 demo

1、produce 生产者

 1 package com.bwdz.sp.comm.util.test;
 2 
 3 import org.apache.rocketmq.client.exception.MQBrokerException;
 4 import org.apache.rocketmq.client.exception.MQClientException;
 5 import org.apache.rocketmq.client.producer.DefaultMQProducer;
 6 import org.apache.rocketmq.client.producer.SendResult;
 7 import org.apache.rocketmq.client.producer.SendStatus;
 8 import org.apache.rocketmq.common.message.Message;
 9 import org.apache.rocketmq.remoting.exception.RemotingException;
10 
11 import java.util.UUID;
12 
13 /**
14  * Created by xy on 2018/11/16.
15  */
16 public class SyncProducer {
17     private static DefaultMQProducer producer = null;
18 
19     public static void main(String[] args) {
20         System.out.print("[----------]Start\n");
21         int pro_count = 1;
22         if (args.length > 0) {
23             pro_count = Integer.parseInt(args[0]);
24         }
25         boolean result = false;
26         try {
27             ProducerStart();
28             for (int i = 1; i < pro_count; i++) {
29                 String msg = "hello rocketmq "+ i+"".toString();
30                 SendMessage("qch_20170706",              //topic
31                         "Tag"+i,                           //tag
32                         "Key"+i,                           //key
33                          msg);                                  //body
34                 System.out.print(msg + "\n");
35             }
36         }finally {
37             producer.shutdown();
38         }
39         System.out.print("[----------]Succeed\n");
40     }
41 
42     private static boolean ProducerStart() {
43         producer = new DefaultMQProducer("pro_qch_test");
44         producer.setNamesrvAddr("192.168.69.173:9876");
45         producer.setInstanceName(UUID.randomUUID().toString());
46         try {
47             producer.start();
48         } catch(MQClientException e) {
49             e.printStackTrace();
50             return false;
51         }
52         return true;
53     }
54 
55     private static boolean SendMessage(String topic,String tag,String key, String str) {
56         Message msg = new Message(topic,tag,key,str.getBytes());
57         try {
58             SendResult result = producer.send(msg);
59             SendStatus status = result.getSendStatus();
60             System.out.println("___________________________SendMessage: "+status.name());
61         } catch (MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
62             e.printStackTrace();
63             return false;
64         }
65         return true;
66     }
67 }
View Code

相关文章:

  • 2022-12-23
  • 2021-07-03
  • 2022-03-06
  • 2022-02-03
  • 2021-05-13
  • 2020-02-04
  • 2021-10-15
  • 2021-11-07
猜你喜欢
  • 2021-11-29
  • 2021-08-14
  • 2022-03-02
  • 2021-12-05
  • 2021-09-09
  • 2022-12-23
  • 2021-06-01
相关资源
相似解决方案