首页 > 其他分享 >HM-RocketMQ1.4【RocketMQ消息发送样例】

HM-RocketMQ1.4【RocketMQ消息发送样例】

时间:2022-11-07 17:25:47浏览次数:71  
标签:producer HM 样例 org apache import RocketMQ1.4 consumer rocketmq

0 初始项目

image-20221102170815038

<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-client</artifactId>
    <version>4.4.0</version>
</dependency>
消息发送者步骤分析:
    1.创建消息生产者producer,并制定生产者组名
    2.指定Nameserver地址
    3.启动producer
    4.创建消息对象,指定主题Topic、Tag和消息体
    5.发送消息
    6.关闭生产者producer

消息消费者步骤分析:
    1.创建消费者Consumer,制定消费者组名
    2.指定Nameserver地址
    3.订阅主题Topic和Tag
    4.设置回调函数,处理消息
    5.启动消费者consumer

1 基本样例

1.1 消息发送

1.1.1 发送同步消息

这种可靠性同步地发送方式使用的比较广泛,比如:重要的消息通知,短信通知。


package com.yppah.base.producer;

import org.apache.rocketmq.client.exception.MQBrokerException;
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.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.concurrent.TimeUnit;

/**
 * @Author: haifei
 * @Date: 2022/11/2 17:15
 */
public class SyncProducer {

    public static void main(String[] args) throws Exception {
//        1.创建消息生产者producer,并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
//        2.指定Nameserver地址
        producer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
//        3.启动producer
        producer.start();

        for (int i = 0; i < 10; i++) { //快捷键:10.fori+回车
//        4.创建消息对象,指定主题Topic、Tag和消息体
            Message msg = new Message("base", "Tag1", ("hehe-" + i).getBytes());
//        5.发送消息
            SendResult result = producer.send(msg);
            System.out.println("发送结果:" + result);

            SendStatus status = result.getSendStatus(); //发送状态
            String msgId = result.getMsgId(); //消息ID
            int queueId = result.getMessageQueue().getQueueId(); //队列ID
            System.out.println("发送状态:" + status + ", 消息ID:" + msgId + ", 队列ID:" + queueId);

            //线程睡1秒,等待回传
            TimeUnit.SECONDS.sleep(1);
        }

//        6.关闭生产者producer
        producer.shutdown();
    }
}


image-20221103084113953

image-20221103084144910

1.1.2 发送异步消息

异步消息通常用在对响应时间敏感的业务场景,即发送端不能容忍长时间地等待Broker的响应。


package com.yppah.base.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;

import java.util.concurrent.TimeUnit;

/**
 * @Author: haifei
 * @Date: 2022/11/3 8:53
 */
public class AsyncProducer {

    public static void main(String[] args) throws Exception {
//        1.创建消息生产者producer,并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
//        2.指定Nameserver地址
        producer.setNamesrvAddr("192.168.115.15:98 76;192.168.115.16:9876");
//        3.启动producer
        producer.start();

        for (int i = 0; i < 10; i++) { //快捷键:10.fori+回车
//        4.创建消息对象,指定主题Topic、Tag和消息体
            Message msg = new Message("base", "Tag2", ("hihi-" + i).getBytes());
//        5.发送异步消息
            producer.send(msg, new SendCallback() {
                /**
                 * 发送成功回调函数
                 * @param sendResult
                 */
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.println("发送结果:" + sendResult);
                }

                /**
                 * 发送失败回调函数
                 * @param throwable
                 */
                @Override
                public void onException(Throwable throwable) {
                    System.out.println("发送异常:" + throwable);
                }
            });

            //线程睡1秒
            TimeUnit.SECONDS.sleep(1);
        }

//        6.关闭生产者producer
        producer.shutdown();
    }
}


image-20221103091339495

1.1.3 单向发送消息

这种方式主要用在不特别关心发送结果的场景,例如日志发送。


package com.yppah.base.producer;

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

import java.util.concurrent.TimeUnit;

/**
 * @Author: haifei
 * @Date: 2022/11/3 9:27
 */
public class OneWayProducer {

    public static void main(String[] args) throws Exception {
//        1.创建消息生产者producer,并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
//        2.指定Nameserver地址
        producer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
//        3.启动producer
        producer.start();

        for (int i = 0; i < 10; i++) { //快捷键:10.fori+回车
//        4.创建消息对象,指定主题Topic、Tag和消息体
            Message msg = new Message("base", "Tag3", ("hello-" + i).getBytes());
//        5.发送单向消息
            producer.sendOneway(msg);
            System.out.println(i);
            //线程睡1秒,等待回传
            TimeUnit.SECONDS.sleep(1);
        }

//        6.关闭生产者producer
        producer.shutdown();
    }
}


image-20221103094839180

1.2 消息消费

1.2.1 消息消费基本流程

tag1

package com.yppah.base.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Author: haifei
 * @Date: 2022/11/3 10:56
 */
public class Consumer {

    public static void main(String[] args) throws MQClientException {
//        1.创建消费者Consumer,制定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
//        2.指定Nameserver地址
        consumer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
//        3.订阅主题Topic和Tag
        consumer.subscribe("base", "Tag1");
//        4.设置回调函数,处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                System.out.println(msgList);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
//        5.启动消费者consumer
        consumer.start();
    }
}

image-20221103111043472

可以看到上面消费者消费了很多消息(来自1.1小结中累计发送的)


tag2

package com.yppah.base.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Author: haifei
 * @Date: 2022/11/3 10:56
 */
public class Consumer {

    public static void main(String[] args) throws MQClientException {
//        1.创建消费者Consumer,制定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
//        2.指定Nameserver地址
        consumer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
//        3.订阅主题Topic和Tag
        consumer.subscribe("base", "Tag2");
//        4.设置回调函数,处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
//                System.out.println(msgList);
                for (MessageExt msg: msgList) {
                    System.out.println(new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
//        5.启动消费者consumer
        consumer.start();
    }
}

image-20221103111522245

可以看到消费者一直处于监听状态,但此时队列汇总已没有消息以供消费

发送异步消息后,可以看到被消费掉了

image-20221103111713304

image-20221103111746393

1.2.2 负载均衡模式

消费者采用负载均衡方式消费消息,多个消费者共同消费队列消息,每个消费者处理的消息不同

image-20221103140340027

package com.yppah.base.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

/**
 * @Author: haifei
 * @Date: 2022/11/3 14:22
 */
public class Consumer2 {
    public static void main(String[] args) throws MQClientException {
//        1.创建消费者Consumer,制定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
//        2.指定Nameserver地址
        consumer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
//        3.订阅主题Topic和Tag
        consumer.subscribe("base", "Tag1 || Tag2"); //若想消费指定topic中的所有类型消息可以写*

        //设定消费模式:负载均衡(默认MessageModel.CLUSTERING) | 广播(MessageModel.BROADCASTING)
//        consumer.setMessageModel(MessageModel.BROADCASTING);

//        4.设置回调函数,处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
//                System.out.println(msgList);
                for (MessageExt msg: msgList) {
                    System.out.println(new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
//        5.启动消费者consumer
        consumer.start();
    }
}

启动两次consumer2,启动一次SyncProducer

image-20221103144110385

发送了十条消息

image-20221103144154666

image-20221103144206433

第一个consumer2消费了6条,第二个消费了4条

1.2.3 广播模式

image-20221103140250390

消费者采用广播的方式消费消息,每个消费者消费的消息都是相同的


package com.yppah.base.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

/**
 * @Author: haifei
 * @Date: 2022/11/3 14:22
 */
public class Consumer2 {
    public static void main(String[] args) throws MQClientException {
//        1.创建消费者Consumer,制定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
//        2.指定Nameserver地址
        consumer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
//        3.订阅主题Topic和Tag
        consumer.subscribe("base", "Tag1 || Tag2"); //若想消费指定topic中的所有类型消息可以写*

        //设定消费模式:负载均衡(默认MessageModel.CLUSTERING) | 广播(MessageModel.BROADCASTING)
        consumer.setMessageModel(MessageModel.BROADCASTING);

//        4.设置回调函数,处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
//                System.out.println(msgList);
                for (MessageExt msg: msgList) {
                    System.out.println(new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
//        5.启动消费者consumer
        consumer.start();
    }
}

启动两次consumer2,启动一次SyncProducer,发送了十条消息

image-20221103144504277

image-20221103144515950

可以看到两个consumer2均消费了十条消息

2 顺序消息

消息有序指的是可以按照消息的发送顺序来消费(FIFO)。RocketMQ可以严格的保证消息有序,可以分为分区有序或者全局有序。

顺序消费的原理解析,在默认的情况下消息发送会采取Round Robin轮询方式把消息发送到不同的queue(分区队列);而消费消息的时候从多个queue上拉取消息,这种情况发送和消费是不能保证顺序。但是如果控制发送的顺序消息只依次发送到同一个queue中,消费的时候只从这个queue上依次拉取,则就保证了顺序。当发送和消费参与的queue只有一个,则是全局有序;如果多个queue参与,则为分区有序,即相对每个queue,消息都是有序的。

image-20221105115322448

image-20221105151512331

下面用订单进行分区有序的示例。一个订单的顺序流程是:创建、付款、推送、完成。订单号相同的消息会被先后发送到同一个队列中,消费时,同一个OrderId获取到的肯定是同一个队列。

2.1 顺序消息生产

package com.yppah.order;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: haifei
 * @Date: 2022/11/5 15:16
 */


public class OrderStep {

    private long orderId;
    private String desc;

    public long getOrderId() {
        return orderId;
    }

    public void setOrderId(long orderId) {
        this.orderId = orderId;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "OrderStep{" +
                "orderId=" + orderId +
                ", desc='" + desc + '\'' +
                '}';
    }

    /**
     * 订单构建
     */
    public static List<OrderStep> buildOrders() {
        //  1039L   : 创建    付款 推送 完成
        //  1065L   :ssssss创建   付款
        //  7235L   :创建    付款
        List<OrderStep> orderList = new ArrayList<OrderStep>();

        OrderStep orderDemo = new OrderStep();
        orderDemo.setOrderId(1039L);
        orderDemo.setDesc("创建");
        orderList.add(orderDemo);

        orderDemo = new OrderStep();
        orderDemo.setOrderId(1065L);
        orderDemo.setDesc("创建");
        orderList.add(orderDemo);

        orderDemo = new OrderStep();
        orderDemo.setOrderId(1039L);
        orderDemo.setDesc("付款");
        orderList.add(orderDemo);

        orderDemo = new OrderStep();
        orderDemo.setOrderId(7235L);
        orderDemo.setDesc("创建");
        orderList.add(orderDemo);

        orderDemo = new OrderStep();
        orderDemo.setOrderId(1065L);
        orderDemo.setDesc("付款");
        orderList.add(orderDemo);

        orderDemo = new OrderStep();
        orderDemo.setOrderId(7235L);
        orderDemo.setDesc("付款");
        orderList.add(orderDemo);

        orderDemo = new OrderStep();
        orderDemo.setOrderId(1065L);
        orderDemo.setDesc("完成");
        orderList.add(orderDemo);

        orderDemo = new OrderStep();
        orderDemo.setOrderId(1039L);
        orderDemo.setDesc("推送");
        orderList.add(orderDemo);

        orderDemo = new OrderStep();
        orderDemo.setOrderId(7235L);
        orderDemo.setDesc("完成");
        orderList.add(orderDemo);

        orderDemo = new OrderStep();
        orderDemo.setOrderId(1039L);
        orderDemo.setDesc("完成");
        orderList.add(orderDemo);

        return orderList;
    }
}

package com.yppah.order;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.List;

/**
 * @Author: haifei
 * @Date: 2022/11/5 16:06
 */
/**
 * Producer,发送顺序消息
 */
public class Producer {

    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
//        1.创建消息生产者producer,并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
//        2.指定Nameserver地址
        producer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
//        3.启动producer
        producer.start();

        //构建消息集合
        List<OrderStep> orderSteps = OrderStep.buildOrders();
        //发送消息
        for (int i=0; i<orderSteps.size(); i++) {
            String body = orderSteps.get(i) + "";
            Message msg = new Message("OrderTopic", "Order", "i"+i, body.getBytes());
            /**
             * 参数1:消息对象
             * 参数2:消息队列选择器
             * 参数3:选择队列的业务标识(订单ID)
             */
            SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
                /**
                 *
                 * @param mqList:队列集合
                 * @param message:消息对象
                 * @param arg:业务标识参数
                 * @return
                 */
                @Override
                public MessageQueue select(List<MessageQueue> mqList, Message message, Object arg) {
                    long orderID = (long) arg;
                    long index = orderID % mqList.size();
                    return mqList.get((int) index);
                }
            }, orderSteps.get(i).getOrderId());
            System.out.println("发送结果:" + sendResult);
        }

        producer.shutdown();
    }


}

2.2 顺序消息消费

package com.yppah.order;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Author: haifei
 * @Date: 2022/11/5 16:48
 */
/**
 * 顺序消息消费
 */
public class Consumer3 {

    public static void main(String[] args) throws MQClientException {
//        1.创建消费者Consumer,制定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
//        2.指定Nameserver地址
        consumer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
//        3.订阅主题Topic和Tag
        consumer.subscribe("OrderTopic", "*");

        //4、注册消息监听器
        consumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println("线程名称:【" + Thread.currentThread().getName() + "】:" + new String(messageExt.getBody()));
                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });

        //5、启动消费者
        consumer.start();
        System.out.println("消费者启动咯");
    }
}


image-20221107134223003

image-20221107134405922

3 延时消息

3.1 延时消息消费

package com.yppah.delay;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Author: haifei
 * @Date: 2022/11/7 13:49
 */
public class DelayConsumer {

    public static void main(String[] args) throws MQClientException {
        //        1.创建消费者Consumer,制定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        //        2.指定Nameserver地址
        consumer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
        //        3.订阅主题Topic和Tag
        consumer.subscribe("delayTopic", "*");
        //        4.设置回调函数,处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            //接收消息内容
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt msg: msgList) {
                    long timeDelay = System.currentTimeMillis() - msg.getStoreTimestamp();
                    System.out.println("消息ID:【"+msg.getMsgId()+"】,延迟时间:"+timeDelay);
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //        5.启动消费者consumer
        consumer.start();

        System.out.println("消费者启动咯咯咯");
    }
}

3.2 延时消息生产

package com.yppah.delay;

import org.apache.rocketmq.client.exception.MQBrokerException;
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.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.concurrent.TimeUnit;

/**
 * @Author: haifei
 * @Date: 2022/11/7 13:49
 */
public class DelayProducer {

    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        //        1.创建消息生产者producer,并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //        2.指定Nameserver地址
        producer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
        //        3.启动producer
        producer.start();

        for (int i = 0; i < 10; i++) {
            //        4.创建消息对象,指定主题Topic、Tag和消息体
            Message msg = new Message("delayTopic", "Tag1", ("hi-" + i).getBytes());

            // 设置延迟时间
            msg.setDelayTimeLevel(3); //10s
            // private String messageDelayLevel = "1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h";

            //        5.发送消息
            SendResult result = producer.send(msg);
            System.out.println("发送结果:" + result);
            SendStatus status = result.getSendStatus(); //发送状态
            //线程睡1秒
            TimeUnit.SECONDS.sleep(1);
        }

        //        6.关闭生产者producer
        producer.shutdown();
    }
}

3.3 验证

image-20221107140835263

image-20221107140939103

image-20221107141138464

将会看到消息的消费比存储时间晚10秒

3.4 使用限制

现在RocketMq并不支持任意时间的延时,需要设置几个固定的延时等级,从1s到2h分别对应着等级1到18

// org/apache/rocketmq/store/config/MessageStoreConfig.java
private String messageDelayLevel = "1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h";

4 批量消息

批量发送消息能显著提高传递小消息的性能。限制是这些批量消息应该有相同的topic,相同的waitStoreMsgOK,而且不能是延时消息。此外,这一批消息的总大小不应超过4MB。

如果每次只发送不超过4MB的消息,则很容易使用批处理

package com.yppah.batch;

import com.sun.xml.internal.bind.v2.model.core.ID;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: haifei
 * @Date: 2022/11/7 14:17
 */
public class BatchProducer {

    public static void main(String[] args) throws Exception {
//        1.创建消息生产者producer,并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
//        2.指定Nameserver地址
        producer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
//        3.启动producer
        producer.start();


        List<Message> msgList = new ArrayList<>();
//        4.创建消息对象,指定主题Topic、Tag和消息体
        Message msg = new Message("batchTopic", "Tag1", ("hehe-1").getBytes());
        Message msg2 = new Message("batchTopic", "Tag1", ("hehe-2").getBytes());
        Message msg3 = new Message("batchTopic", "Tag1", ("hehe-3").getBytes());
        msgList.add(msg);
        msgList.add(msg2);
        msgList.add(msg3);

//        5.发送批量消息:直接send消息集合
        SendResult result = producer.send(msgList);
        System.out.println("发送结果:" + result);
        //线程睡1秒,等待回传
        TimeUnit.SECONDS.sleep(1);

//        6.关闭生产者producer
        producer.shutdown();
    }

}

package com.yppah.batch;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Author: haifei
 * @Date: 2022/11/7 14:17
 */
public class BatchConsumer {

    public static void main(String[] args) throws MQClientException {
//        1.创建消费者Consumer,制定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
//        2.指定Nameserver地址
        consumer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
//        3.订阅主题Topic和Tag
        consumer.subscribe("batchTopic", "*");
//        4.设置回调函数,处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt msg: msgList) {
                    System.out.println("consumeThread=" + Thread.currentThread().getName() + "," + new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
//        5.启动消费者consumer
        consumer.start();
    }

}

image-20221107144832828

image-20221107144955779


如果消息的总长度可能大于4MB时,这时候最好把消息进行分割

public class ListSplitter implements Iterator<List<Message>> {
   private final int SIZE_LIMIT = 1024 * 1024 * 4;
   private final List<Message> messages;
   private int currIndex;
   public ListSplitter(List<Message> messages) {
           this.messages = messages;
   }
    @Override 
    public boolean hasNext() {
       return currIndex < messages.size();
   }
   	@Override 
    public List<Message> next() {
       int nextIndex = currIndex;
       int totalSize = 0;
       for (; nextIndex < messages.size(); nextIndex++) {
           Message message = messages.get(nextIndex);
           int tmpSize = message.getTopic().length() + message.getBody().length;
           Map<String, String> properties = message.getProperties();
           for (Map.Entry<String, String> entry : properties.entrySet()) {
               tmpSize += entry.getKey().length() + entry.getValue().length();
           }
           tmpSize = tmpSize + 20; // 增加日志的开销20字节
           if (tmpSize > SIZE_LIMIT) {
               //单个消息超过了最大的限制
               //忽略,否则会阻塞分裂的进程
               if (nextIndex - currIndex == 0) {
                  //假如下一个子列表没有元素,则添加这个子列表然后退出循环,否则只是退出循环
                  nextIndex++;
               }
               break;
           }
           if (tmpSize + totalSize > SIZE_LIMIT) {
               break;
           } else {
               totalSize += tmpSize;
           }

       }
       List<Message> subList = messages.subList(currIndex, nextIndex);
       currIndex = nextIndex;
       return subList;
   }
}
//把大的消息分裂成若干个小的消息
ListSplitter splitter = new ListSplitter(messages);
while (splitter.hasNext()) {
  try {
      List<Message>  listItem = splitter.next();
      producer.send(listItem);
  } catch (Exception e) {
      e.printStackTrace();
      //处理error
  }
}

5 过滤消息

5.1 过滤方式

1、Tag

2、Sql表达式

在大多数情况下,TAG是一个简单而有用的设计,其可以来选择您想要的消息。例如:

DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("CID_EXAMPLE");
consumer.subscribe("TOPIC", "TAGA || TAGB || TAGC");

消费者将接收包含TAGA或TAGB或TAGC的消息。但是限制是一个消息只能有一个标签,这对于复杂的场景可能不起作用。

在这种情况下,可以使用SQL表达式筛选消息。SQL特性可以通过发送消息时的属性来进行计算。在RocketMQ定义的语法下,可以实现一些简单的逻辑。下面是一个例子:

------------
| message  |
|----------|  a > 5 AND b = 'abc'
| a = 10   |  --------------------> Gotten
| b = 'abc'|
| c = true |
------------
------------
| message  |
|----------|   a > 5 AND b = 'abc'
| a = 1    |  --------------------> Missed
| b = 'abc'|
| c = true |
------------

5.2 Sql基本语法

RocketMQ只定义了一些基本语法来支持这个特性。你也可以很容易地扩展它。

  • 数值比较,比如:>,>=,<,<=,BETWEEN,=;
  • 字符比较,比如:=,<>,IN;
  • IS NULL 或者 IS NOT NULL;
  • 逻辑符号 AND,OR,NOT;

常量支持类型为:

  • 数值,比如:123,3.1415;
  • 字符,比如:'abc',必须用单引号包裹起来;
  • NULL,特殊的常量
  • 布尔值,TRUEFALSE

只有使用push模式的消费者才能用使用SQL92标准的sql语句,接口如下:

public void subscribe(finalString topic, final MessageSelector messageSelector)

5.3 Tag过滤

package com.yppah.filter.tag;

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

import java.util.concurrent.TimeUnit;

/**
 * @Author: haifei
 * @Date: 2022/11/7 15:01
 */
public class TagProducer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        producer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
        producer.start();
        for (int i = 0; i < 3; i++) {
            Message msg = new Message("FilterTagTopic", "Tag1", ("oh:" + i).getBytes());
            SendResult result = producer.send(msg);
            System.out.println("发送结果:" + result);
            TimeUnit.SECONDS.sleep(1);
        }
        producer.shutdown();
    }

}

package com.yppah.filter.tag;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Author: haifei
 * @Date: 2022/11/7 15:01
 */
public class TagConsumer {

    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        consumer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
        consumer.subscribe("FilterTagTopic", "Tag1");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt msg: msgList) {
                    System.out.println("consumeThread=" + Thread.currentThread().getName() + "," + new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    }

}


image-20221107152424810

image-20221107152504054

image-20221107152539396


image-20221107152722038

image-20221107152820123

image-20221107153007899

5.4 Sql过滤

package com.yppah.filter.sql;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.MessageSelector;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Author: haifei
 * @Date: 2022/11/7 15:30
 */
public class SqlConsumer {

    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        consumer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
        
        consumer.subscribe("FilterSQLTopic", MessageSelector.bySql("i>5"));
        
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt msg: msgList) {
                    System.out.println("consumeThread=" + Thread.currentThread().getName() + "," + new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    }

}

package com.yppah.filter.sql;

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

import java.util.concurrent.TimeUnit;

/**
 * @Author: haifei
 * @Date: 2022/11/7 15:30
 */
public class SqlProducer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        producer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
        producer.start();
        for (int i = 0; i < 10; i++) {
            Message msg = new Message("FilterSQLTopic", "Tag1", ("oh:" + i).getBytes());

            msg.putUserProperty("i", String.valueOf(i));

            SendResult result = producer.send(msg);
            System.out.println("发送结果:" + result);
            TimeUnit.SECONDS.sleep(1);
        }
        producer.shutdown();
    }

}


image-20221107154525758

image-20221107154724392

image-20221107155234144

报错解决:(112条消息) 当RocketMQ使用SQL过滤时,The broker does not support consumer to filter message by SQL92解决_zhangzengxiu的博客-CSDN博客

参考HM-RocketMQ1.3【RocketMQ集群搭建】 - yub4by - 博客园 (cnblogs.com)中broker配置文件*4

修改完之后再来重启测试,ok

image-20221107161411122

6 事务消息

6.1 流程分析

image-20221107161629533

上图说明了事务消息的大致方案,其中分为两个流程:正常事务消息的发送及提交、事务消息的补偿流程。

6.1.1 流程

(1) 发送消息(half消息)。

(2) 服务端响应消息写入结果。

(3) 根据发送结果执行本地事务(如果写入失败,此时half消息对业务不可见,本地逻辑不执行)。

(4) 根据本地事务状态执行Commit或者Rollback(Commit操作生成消息索引,消息对消费者可见)

6.1.2 事务补偿

(1) 对没有Commit/Rollback的事务消息(pending状态的消息),从服务端发起一次“回查”

(2) Producer收到回查消息,检查回查消息对应的本地事务的状态

(3) 根据本地事务状态,重新Commit或者Rollback

其中,补偿阶段用于解决消息Commit或者Rollback发生超时或者失败的情况。

6.1.3 事务消息状态

事务消息共有三种状态,提交状态、回滚状态、中间状态:

  • TransactionStatus.CommitTransaction: 提交事务,它允许消费者消费此消息。
  • TransactionStatus.RollbackTransaction: 回滚事务,它代表该消息将被删除,不允许被消费。
  • TransactionStatus.Unknown: 中间状态,它代表需要检查消息队列来确定状态。

6.2 代码实现

6.2.1 创建事务性生产者

使用 TransactionMQProducer类创建生产者,并指定唯一的 ProducerGroup,就可以设置自定义线程池来处理这些检查请求。执行本地事务后、需要根据执行结果对消息队列进行回复。

package com.yppah.transcation;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.producer.*;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.concurrent.TimeUnit;

/**
 * @Author: haifei
 * @Date: 2022/11/7 15:30
 */
public class TransProducer {

    public static void main(String[] args) throws Exception {
        //创建消息生产者
        TransactionMQProducer producer = new TransactionMQProducer("group3");
        producer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
        //创建事务监听器
        producer.setTransactionListener(new TransactionListener() {
            /**
             * 在该方法中执行本地事务
             * @param message
             * @param o
             * @return
             */
            @Override
            public LocalTransactionState executeLocalTransaction(Message message, Object o) {
                if (StringUtils.equals("TAGA", message.getTags())){
                    return LocalTransactionState.COMMIT_MESSAGE;
                }else if (StringUtils.equals("TAGB", message.getTags())){
                    return LocalTransactionState.ROLLBACK_MESSAGE;
                }else if (StringUtils.equals("TAGC", message.getTags())){
                    return LocalTransactionState.UNKNOW;
                }
                return LocalTransactionState.UNKNOW;
            }
            /**
             * 该方法MQ进行消息事务状态回查
             * @param messageExt
             * @return
             */
            @Override
            public LocalTransactionState checkLocalTransaction(MessageExt messageExt) {
                System.out.println("消息Tag:" + messageExt.getTags());
                return LocalTransactionState.COMMIT_MESSAGE;
            }
        });
        producer.start();

        String[] tagArr = {"TAGA", "TAGB", "TAGC"}; //用于演示事务的三种状态
        for (int i = 0; i < 3; i++) {
            Message msg = new Message("TransactionTopic", tagArr[i], ("ohh:" + i).getBytes());
            SendResult result = producer.sendMessageInTransaction(msg, null);
            System.out.println("发送结果:" + result);
            TimeUnit.SECONDS.sleep(1);
        }

//        producer.shutdown(); //因为要回查,所以先注释掉
    }

}

package com.yppah.transcation;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.MessageSelector;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Author: haifei
 * @Date: 2022/11/7 15:30
 */
public class TransConsumer {

    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group3");
        consumer.setNamesrvAddr("192.168.115.15:9876;192.168.115.16:9876");
        consumer.subscribe("TransactionTopic", "*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt msg: msgList) {
                    System.out.println("consumeThread=" + Thread.currentThread().getName() + "," + new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    }

}

6.2.2 实现事务监听接口

当发送半消息成功时,我们使用 executeLocalTransaction 方法来执行本地事务。它返回前一节中提到的三个事务状态之一。checkLocalTranscation 方法用于检查本地事务状态,并回应消息队列的检查请求。它也是返回前一节中提到的三个事务状态之一。

public class TransactionListenerImpl implements TransactionListener {

    @Override
    public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {
        System.out.println("执行本地事务");
        if (StringUtils.equals("TagA", msg.getTags())) {
            return LocalTransactionState.COMMIT_MESSAGE;
        } else if (StringUtils.equals("TagB", msg.getTags())) {
            return LocalTransactionState.ROLLBACK_MESSAGE;
        } else {
            return LocalTransactionState.UNKNOW;
        }

    }

    @Override
    public LocalTransactionState checkLocalTransaction(MessageExt msg) {
        System.out.println("MQ检查消息Tag【"+msg.getTags()+"】的本地事务执行结果");
        return LocalTransactionState.COMMIT_MESSAGE;
    }
}

6.2.3 测试验证

image-20221107165624136

image-20221107165902715

第一条和第三条消息提交;第二条消息回滚,所以MQ会把这条消息删掉;故只有两条消息被消费

6.3 使用限制

  1. 事务消息不支持延时消息和批量消息。
  2. 为了避免单个消息被检查太多次而导致半队列消息累积,我们默认将单个消息的检查次数限制为 15 次,但是用户可以通过 Broker 配置文件的 transactionCheckMax参数来修改此限制。如果已经检查某条消息超过 N 次的话( N = transactionCheckMax ) 则 Broker 将丢弃此消息,并在默认情况下同时打印错误日志。用户可以通过重写 AbstractTransactionCheckListener 类来修改这个行为。
  3. 事务消息将在 Broker 配置文件中的参数 transactionMsgTimeout 这样的特定时间长度之后被检查。当发送事务消息时,用户还可以通过设置用户属性 CHECK_IMMUNITY_TIME_IN_SECONDS 来改变这个限制,该参数优先于 transactionMsgTimeout 参数。
  4. 事务性消息可能不止一次被检查或消费。
  5. 提交给用户的目标主题消息可能会失败,目前这依日志的记录而定。它的高可用性通过 RocketMQ 本身的高可用性机制来保证,如果希望确保事务消息不丢失、并且事务完整性得到保证,建议使用同步的双重写入机制。
  6. 事务消息的生产者 ID 不能与其他类型消息的生产者 ID 共享。与其他类型的消息不同,事务消息允许反向查询、MQ服务器能通过它们的生产者 ID 查询到消费者。

标签:producer,HM,样例,org,apache,import,RocketMQ1.4,consumer,rocketmq
From: https://www.cnblogs.com/yppah/p/16850892.html

相关文章