RocketMQ笔记(六):示例代码工具类

  引入RocketMQ客户端的maven依赖:

<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-client</artifactId>
    <version>5.0.0</version>
</dependency>

1、客户端实例获取工具类 - ClientUtils

  1 import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
  2 import org.apache.rocketmq.client.consumer.MessageSelector;
  3 import org.apache.rocketmq.client.exception.MQClientException;
  4 import org.apache.rocketmq.client.producer.DefaultMQProducer;
  5 import java.util.Objects;
  6 
  7 /**
  8  * @Description: MQ客户端工具
  9  */
 10 public class ClientUtils {
 11 
 12     /**
 13      * 获取生产者实例
 14      * @param producerGroup
 15      * @param namesrvAddr
 16      * @return
 17      */
 18     public static DefaultMQProducer gainProducerInstance(String producerGroup, String namesrvAddr) {
 19         try {
 20             // 实例化消息生产者Producer
 21             DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
 22             // 设置NameServer的地址
 23             producer.setNamesrvAddr(namesrvAddr);
 24             // 启动Producer实例
 25             producer.start();
 26             return producer;
 27         } catch (MQClientException e) {
 28             e.printStackTrace();
 29         }
 30         return null;
 31     }
 32 
 33     /**
 34      * 关闭生产者实例
 35      * @param producer
 36      */
 37     public static void shutdownProducer( DefaultMQProducer producer) {
 38         producer.shutdown();
 39     }
 40 
 41     /**
 42      * 获取消费者实例
 43      * @param consumerGroup
 44      * @param namesrvAddr
 45      * @param topicName
 46      * @param subExpression
 47      * @return
 48      */
 49     public static DefaultMQPushConsumer gainConsumerInstance(String consumerGroup, String namesrvAddr, String topicName, String subExpression) {
 50         try {
 51             // 实例化消费者
 52             DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(consumerGroup);
 53             // 设置NameServer的地址
 54             if (Objects.nonNull(namesrvAddr)) {
 55                 consumer.setNamesrvAddr(namesrvAddr);
 56             }
 57             // 订阅一个或者多个Topic,以及Tag来过滤需要消费的消息
 58             consumer.subscribe(topicName, subExpression);
 59             return consumer;
 60         } catch (MQClientException e) {
 61             e.printStackTrace();
 62         }
 63         return null;
 64     }
 65 
 66     /**
 67      * 获取消费者实例
 68      *
 69      * @param consumerGroup
 70      * @param namesrvAddr
 71      * @param topicName
 72      * @param messageSelector
 73      * @return
 74      */
 75     public static DefaultMQPushConsumer gainConsumerInstance(String consumerGroup, String namesrvAddr, String topicName, MessageSelector messageSelector) {
 76         try {
 77             // 实例化消费者
 78             DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(consumerGroup);
 79             // 设置NameServer的地址
 80             if (Objects.nonNull(namesrvAddr)) {
 81                 consumer.setNamesrvAddr(namesrvAddr);
 82             }
 83             // 订阅一个或者多个Topic,以及Tag来过滤需要消费的消息
 84             consumer.subscribe(topicName, messageSelector);
 85             return consumer;
 86         } catch (MQClientException e) {
 87             e.printStackTrace();
 88         }
 89         return null;
 90     }
 91 
 92     /**
 93      * 启动消费者实例
 94      * @param consumer
 95      */
 96     public static void startupConsumer(DefaultMQPushConsumer consumer) {
 97         try {
 98             consumer.start();
 99             System.out.printf("消费者已启动.%n");
100         } catch (MQClientException e) {
101             e.printStackTrace();
102         }
103     }
104 }

2、常量类 - RMQConstant

 1 /**
 2  * @Description: 常量类
 3  */
 4 public class RMQConstant {
 5 
 6     // 生产者组
 7     public static final String TEST_GROUP = "test-group";
 8     public static final String COMM_GROUP = "comm-group";
 9     public static final String ORDER_GROUP = "order-group";
10     public static final String DELAY_GROUP = "delay-group";
11     public static final String BATCH_GROUP = "batch-group";
12     public static final String TRX_GROUP = "trx-group";
13 
14     // NameSrv地址
15     public static final String NAEMSRV_ADDR = "192.168.33.55:9876";
16 
17     // 测试Topic
18     public static final String TEST_TOPIC = "TestTopic";
19     public static final String COMM_TOPIC = "CommTopic";
20     public static final String ORDER_TOPIC = "OrderTopic";
21     public static final String BATCH_TOPIC = "BatchTopic";
22     public static final String TRX_TOPIC = "TrxTopic";
23 
24     // 表达式
25     public static final String SUB_EXPRESSION_ALL = "*";
26 
27     // 消息标签
28     public static final String MSG_TAG_A = "TagA";
29     public static final String MSG_TAG_B = "TagB";
30     public static final String MSG_TAG_C = "TagC";
31     public static final String MSG_TAG_D = "TagD";
32     public static final String MSG_TAG_E = "TagE";
33 }

3、日期工具类 - DateUtils

 1 import java.text.SimpleDateFormat;
 2 import java.util.Date;
 3 
 4 /**
 5  * @Description: 日期工具类
 6  */
 7 public class DateUtils {
 8 
 9     public static String PARTTERN1 = "yyyy-MM-dd HH:mm:ss";
10 
11     /**
12      * 获取当前时间
13      * @return
14      */
15     public static String now() {
16         return new SimpleDateFormat(PARTTERN1).format(new Date());
17     }
18 }

 

posted @ 2023-05-05 08:23  无虑的小猪  阅读(485)  评论(0编辑  收藏  举报