RocketMQ使用

RocketMQ角色
RocketMQ使用_第1张图片

RocketMQ的基本概念
RocketMQ使用_第2张图片

同步发送消息

/**
 * @author
 * @create 2023-04-08 17:24
 * 发送同步消息
 * 适用于重要的消息 例如消息通知,短信通知
 */
public class SyncProducer {
    public static void main(String[] args) throws Exception {
        String topic="testRocketMQ";
        String tag="Tag1";
        String body="Hello RocketMQ";
        //创建消息生产者Producer,并制订生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //指定NameServer地址
        producer.setNamesrvAddr("localhost:9876");
        //启动producer
        producer.start();
        //创建消息对象,指定主题topic,tag和消息体
        for (int i = 0; i < 10; i++) {
            /**
             * 主题 topic
             * 消息的tag
             * 消息内容
             */
            Message message = new Message("testRocketMQ","Tag1",("Hello RocketMQ"+i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            //发送消息
            SendResult result = producer.send(message,1000);
            //发送状态
            SendStatus status = result.getSendStatus();
            System.out.println("发送状态->"+status);
            String msgId = result.getMsgId();
            System.out.println("消息id->"+msgId);
            int queueId = result.getMessageQueue().getQueueId();
            System.out.println("消息接收队列id"+queueId);
            TimeUnit.SECONDS.sleep(1);//线程休息1s
        }
        //关闭生产者producer
        producer.shutdown();
    }
}

异步发送

/**
 * @author
 * @create 2023-04-08 19:52
 * 发送异步消息,异步监听,通过回调接口的方式接受服务端的响应
 * 适用于数据量较大,不能容忍阻塞场景的
 */
public class ASyncProducer {
    public static void main(String[] args) throws Exception {
//创建消息生产者Producer,并制订生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //指定NameServer地址
        producer.setNamesrvAddr("localhost:9876");
        //启动producer
        producer.start();
        //创建消息对象,指定主题topic,tag和消息体
        for (int i = 0; i < 10; i++) {
            /**
             * 主题 topic
             * 消息的tag
             * 消息内容
             */
            Message message = new Message("testRocketMQ","Tag1",("Hello RocketMQ"+i).getBytes(RemotingHelper.DEFAULT_CHARSET));

            int index=i;
            //发送消息
             producer.send(message, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.printf("%-10d OK %s %n", index,
                            sendResult.getMsgId());
                    System.out.println("发送成功"+sendResult);
                }

                @Override
                public void onException(Throwable throwable) {
                    System.out.printf("%-10d Exception %s %n", index, throwable);
                }
            });

            TimeUnit.SECONDS.sleep(1);//线程休息1s
        }
        //关闭生产者producer
        producer.shutdown();
    }

}

单向发送

/**
 * 单向发送,只管发送,不管响应
 * 例如日志收集场景
 * @author
 * @create 2023-04-08 20:22
 */
public class OneWayProducer {
    public static void main(String[] args) throws Exception {
        //创建消息生产者Producer,并制订生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //指定NameServer地址
        producer.setNamesrvAddr("localhost:9876");
        //启动producer
        producer.start();
        //创建消息对象,指定主题topic,tag和消息体
        for (int i = 0; i < 10; i++) {
            /**
             * 主题 topic
             * 消息的tag
             * 消息内容
             */
            Message message = new Message("testRocketMQ","Tag1",("Hello RocketMQ"+i).getBytes(RemotingHelper.DEFAULT_CHARSET));

            int index=i;
            //发送消息
            producer.sendOneway(message);

            TimeUnit.SECONDS.sleep(1);//线程休息1s
        }
        //关闭生产者producer
        producer.shutdown();
    }
}

集群消费 /负载均衡模式消费

/**
 * @author
 * @create 2023-04-08 20:29
 */
public class Consumer {
    public static void main(String[] args) throws Exception {
        // 实例化消息生产者,指定组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        // 指定Namesrv地址信息.
        consumer.setNamesrvAddr("localhost:9876");
        // 订阅Topic
        consumer.subscribe("testRocketMQ","*");
//        consumer.subscribe("base","Tag1 || Tag2 || Tag3");
//        consumer.subscribe("testRocketMQ","Tag1");
        //负载均衡模式消费  集群模式消费
        consumer.setMessageModel(MessageModel.CLUSTERING);
//        consumer.setMessageModel(MessageModel.BROADCASTING);//广播模式
        // 注册回调函数,处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt ext : list) {
                    String str = new String(ext.getBody());
                    System.out.println(str);
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    }
}

广播模式

        consumer.setMessageModel(MessageModel.BROADCASTING);//广播模式

顺序消息的生产与消费

/**
 * 顺序消息
 */
public class Producer {
    public static void main(String[] args) throws Exception {
//        1.创建消息生产者producer ,并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
//        2.制定NameServer地址
        producer.setNamesrvAddr("192.168.40.147:9876");
//        producer.setSendMsgTimeout(10000);
//        3.启动Producer
        producer.start();

        List<OrderStep> orderSteps = OrderStep.buildOrders();
        for (int i=0;i<=orderSteps.size()+11;i++) {
            //4.创建消息
            Message message = new Message("OrderTopic","Order",
                    String.valueOf(orderSteps.get(i)).getBytes());

            /**
             * 参数一 消息对象
             * 参数二 消息队列的选择器
             * 参数三 选择队列的业务标识(订单ID)
             */
            //5.发送消息
            SendResult sendResult = producer.send(message, new MessageQueueSelector() {
                /**
                 * @param list  队列集合
                 * @param message  消息对象
                 * @param obj  业务标识参数
                 * @return
                 */
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object obj) {
                    long orderId = (long) obj;
                    int size = list.size();
                    int index = (int) (orderId % size);
                    return list.get(index);
                }
            }, orderSteps.get(i).getOrderId());  //orderSteps.get(i).getOrderId() 订单ID
        }
//        6.关闭生产者Producer
        producer.shutdown();
    }
}
/**
 * 顺序消息
 */
public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //1.创建消费者Consumer,制定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        //2.指定Nameserver地址
        consumer.setNamesrvAddr("192.168.40.147:9876");
        //3.订阅主题Topic和Tag
        consumer.subscribe("OrderTopic","*");
        //4.注册消息监听器
        consumer.registerMessageListener(new MessageListenerOrderly() {
            //使用监听器消费消息
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
                for (MessageExt message : list) {
                    System.out.println("消息内容"+new String(message.getBody()));
                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });
        //5.启动消费者
        consumer.start();
        System.out.println("消费者启动");
    }
}
/**
 * 订单构建者
 */
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   : 创建   付款
        //  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;
    }
}

延时消息

/**
 * 延时消息
 * @author
 * @create 2022-02-19 23:46
 */
public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //1.创建消费者Consumer,制定消费者组
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        //2.指定NameServer地址
        consumer.setNamesrvAddr("192.168.40.147:9876");
        //3.订阅主题Topic和Tag
        consumer.subscribe("DelayTopic","*");

        //4.设置回调函数,处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            //接受消息内容
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt message : list) {
//                    System.out.println(new String(message.getBody()));
                    // Print approximate delay time period
                    System.out.println("Receive message[msgId=" + message.getMsgId() + "] " + (System.currentTimeMillis() - message.getStoreTimestamp()) + "ms later");

                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //5.启动消费者consumer
        consumer.start();
        System.out.println("消费者启动");
    }
}
/**
 * 延时消息
 * @author
 * @create 2022-02-19 22:52
 */
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.40.147:9876");
//        3.启动Producer
        producer.start();
//        4.创建消息对象,指定主题Topic、Tag和消息体
        for (int i = 0; i < 10; i++) {
            /**
             * 消息主题 topic
             * 消息tag
             * 消息内容
             */
            Message message = new Message("DelayTopic","Tag2",("渔阳+"+i).getBytes());
            //延时级别   设置延时时间   //从1到18 依次设定
            message.setDelayTimeLevel(2);
            
//        5.发送消息
            producer.send(message);
            System.out.println("=====================");
//            System.out.printf("%s%n", result);
            TimeUnit.SECONDS.sleep(1);  //线程休眠
        }
//        6.关闭生产者Producer
        producer.shutdown();
    }
}

批量消息

/**
 * 批量消息
 * @author
 * @create 2022-02-19 23:46
 */
public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //1.创建消费者Consumer,制定消费者组
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        //2.指定NameServer地址
        consumer.setNamesrvAddr("192.168.40.147:9876");
        //3.订阅主题Topic和Tag
        consumer.subscribe("BatchTest","*");
        //4.设置回调函数,处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            //接受消息内容
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt message : list) {
//                    System.out.println(new String(message.getBody()));
                    // Print approximate delay time period
                    System.out.println("Receive message[msgId=" + message.getMsgId() + "] " + (System.currentTimeMillis() - message.getStoreTimestamp()) + "ms later");
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //5.启动消费者consumer
        consumer.start();
        System.out.println("消费者启动");
    }
}
/**
 * 批量消息
 * 注意点:每次只发送不超过4MB的消息
 * 如果消息的总长度可能超过4MB时,这时候最好把消息进行分割
 * @author
 * @create 2022-02-19 22:52
 */
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.40.147:9876");
//        3.启动Producer
        producer.start();
//        4.创建消息对象,指定主题Topic、Tag和消息体
        String topic = "BatchTest";
        List<Message> messageList = new ArrayList<>();
        messageList.add(new Message(topic, "TagA", "OrderID001", "Hello world 0".getBytes()));
        messageList.add(new Message(topic, "TagA", "OrderID002", "Hello world 1".getBytes()));
        messageList.add(new Message(topic, "TagA", "OrderID003", "Hello world 2".getBytes()));
        try {
            //5.发生消息   //批量
            producer.send(messageList);
        } catch (Exception e) {
            e.printStackTrace();
            //处理error
        }
//        6.关闭生产者Producer
        producer.shutdown();
        System.out.println("over");
    }
}

过滤消息
sql模式

/**
 * 使用tag  过滤  本质就是  消费者的tag要和生产者的tag一致
 * 可以使用 "*"  代表所有tag
 * @author
 * @create 2022-02-20 14:52
 */
public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //1.创建消费者Consumer,制定消费者组
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        //2.指定NameServer地址
        consumer.setNamesrvAddr("192.168.40.147:9876");
        //3.订阅主题Topic和Tag
        consumer.subscribe("FilterSQLTopic", MessageSelector.bySql("i>5"));
        //4.设置回调函数,处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            //接受消息内容
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt msg : msgs) {
                    System.out.println("consumeThread=" + Thread.currentThread().getName() + "," + new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //5.启动消费者consumer
        consumer.start();
        System.out.println("消费者启动");
    }
}
/**
 * 过滤消息
 * 使用tag  过滤  本质就是  消费者的tag要和生产者的tag一致
 * @author
 * @create 2022-02-19 20:26
 */
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.40.147:9876");
//        producer.setSendMsgTimeout(10000);
//        3.启动Producer
        producer.start();
//        4.创建消息对象,指定主题Topic、Tag和消息体
        for (int i = 0; i < 10; i++) {
            /**
             * 参数一:消息主题Topic
             * 参数二:消息Tag
             * 参数三:消息内容
             */
            Message message = new Message("FilterSQLTopic","Tag1",("渔阳+"+i).getBytes());
            message.putUserProperty("i",String.valueOf(i));
            //5.发送消息
            SendResult result = producer.send(message);
            //发送状态
            SendStatus status = result.getSendStatus();
            System.out.println("发送结果:" + result);
            //线程睡1秒
            TimeUnit.SECONDS.sleep(2);
        }
//        6.关闭生产者Producer
        producer.shutdown();
    }

}

tag模式

/**
 * 使用tag  过滤  本质就是  消费者的tag要和生产者的tag一致
 * 可以使用 "*"  代表所有tag
 * @author
 * @create 2022-02-20 14:52
 */
public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //1.创建消费者Consumer,制定消费者组
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        //2.指定NameServer地址
        consumer.setNamesrvAddr("192.168.40.147:9876");
        //3.订阅主题Topic和Tag
        consumer.subscribe("FilterTagTopic","Tag1  || Tag2");
        //4.设置回调函数,处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            //接受消息内容
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt message : list) {
                    System.out.println(new String(message.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //5.启动消费者consumer
        consumer.start();
        System.out.println("消费者启动");
    }
}
/**
 * 过滤消息
 * 使用tag  过滤  本质就是  消费者的tag要和生产者的tag一致
 * @author
 * @create 2022-02-19 20:26
 */
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.40.147:9876");
//        producer.setSendMsgTimeout(10000);
//        3.启动Producer
        producer.start();

        //String [] tags = {"Tag1","Tag2","Tag3"};
//        4.创建消息对象,指定主题Topic、Tag和消息体
        for (int i = 0; i < 10; i++) {

            Message message = new Message("FilterTagTopic","Tag1",("渔阳+"+i).getBytes());
            //可以写正则表达式
			//Message message = new Message("FilterTagTopic",tags[i%tags.length],("渔阳+"+i).getBytes());
//        5.发送消息
            SendResult result = producer.send(message);
        //发送状态
            SendStatus status = result.getSendStatus();

            System.out.println("发送结果:"+result);
            System.out.println("=====================");
//            System.out.printf("%s%n", result);
        }
//        6.关闭生产者Producer
        producer.shutdown();
    }
}

事务消息

/**
 * 过滤消息
 * 使用tag  过滤  本质就是  消费者的tag要和生产者的tag一致
 * @author
 * @create 2022-02-19 20:26
 */
public class Producer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
//        1.创建消息生产者producer ,并制定生产者组名
        TransactionMQProducer producer = new TransactionMQProducer("group5");
//        2.制定NameServer地址
        producer.setNamesrvAddr("192.168.40.147:9876");

        //添加事务监听器
        producer.setTransactionListener(new TransactionListener() {
            /**
             * 在该方法中执行本地事务
             * @param message
             * @param o
             * @return
             */
            @Override
            public LocalTransactionState executeLocalTransaction(Message message, Object o) {
//                if (message.getTags().equals("TAGA")){
                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;
            }

            /**
             * LocalTransactionState.UNKNOW
             * 该方法时MQ进行消息事务状态回查
             * @param messageExt
             * @return
             */
            @Override
            public LocalTransactionState checkLocalTransaction(MessageExt messageExt) {
                System.out.println("消息的Tag:" + messageExt.getTags());
                return LocalTransactionState.COMMIT_MESSAGE;
            }
        });

//        3.启动Producer
        producer.start();

        String[] tags={"TAGA","TAGB","TAGC"};
//        4.创建消息对象,指定主题Topic、Tag和消息体
        /**
         * 参数一:消息主题Topic
         * 参数二:消息Tag
         * 参数三:消息内容
         */
        for (int i = 0; i < 3; i++) {

            Message message = new Message("TransactionTopic",tags[i],("渔阳+"+i).getBytes());

//        5.发送消息
            SendResult result = producer.sendMessageInTransaction(message,null);
        //发送状态
            SendStatus status = result.getSendStatus();

            System.out.println("发送结果:"+result);
            System.out.println("=====================");
//            System.out.printf("%s%n", result);


        }
        //6.关闭生产者producer
        producer.shutdown();
    }
}
/**
 * 使用tag  过滤  本质就是  消费者的tag要和生产者的tag一致
 * 可以使用 "*"  代表所有tag
 * @author
 * @create 2022-02-20 14:52
 */
public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //1.创建消费者Consumer,制定消费者组
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        //2.指定NameServer地址
        consumer.setNamesrvAddr("192.168.40.147:9876");
        //3.订阅主题Topic和Tag
        consumer.subscribe("TransactionTopic","*");

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

你可能感兴趣的:(学习笔记,rocketmq)