RocketMQ学习笔记(三)

集成SpringBoot

基础配置


    org.springframework.boot
    spring-boot-starter-parent
    2.2.6.RELEASE
     



    
        org.springframework.boot
        spring-boot-starter-web
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
    

    
        org.apache.rocketmq
        rocketmq-spring-boot-starter
        2.2.3
    

发送同步消息

配置

rocketmq.name-server=localhost:9876
rocketmq.producer.group=my-group

发送同步消息

@Resource
private RocketMQTemplate rocketMQTemplate;

@Test
public void testSendSyncMsg(){
    String topic = "helloTopic";
    Message message = MessageBuilder.withPayload("发送同步消息").build();
    rocketMQTemplate.syncSend(topic,message);
}

消费消息

import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(consumerGroup = "helloTopicGroup",topic = "helloTopic")
public class HelloListener implements RocketMQListener {
    @Override
    public void onMessage(MessageExt messageExt) {
        System.out.println("消息内容为===>"+new String(messageExt.getBody()));
    }
}

发送异步消息

@Test
public void testSendASyncMsg() {
    String topic = "helloTopic";
    Message message = MessageBuilder.withPayload("发送异步消息").build();
    rocketMQTemplate.asyncSend(topic, message ,new SendCallback() {
        @Override
        public void onSuccess(SendResult sendResult) {
            System.out.println("消息发送状态===>"+sendResult.getSendStatus());
        }

        @Override
        public void onException(Throwable throwable) {
            System.out.println("出现异常");
        }
    });

    Thread.sleep(3000);
}

发送一次性消息

@Test
public void testSendOneWayMsg() {
    String topic = "helloTopic";
    Message message = MessageBuilder.withPayload("发送一次性消息").build();
    rocketMQTemplate.sendOneWay(topic,message);
}

消费模式

设置广播消费模式
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(consumerGroup = "helloTopicGroup",topic = "helloTopic",messageModel = MessageModel.BROADCASTING)
public class HelloListener implements RocketMQListener {
    @Override
    public void onMessage(MessageExt messageExt) {
        System.out.println("消息内容为===>"+new String(messageExt.getBody()));
    }
}

延时消息

@Test
public void testSendDelayMsg(){
    String topic = "helloTopic";
    Message message = MessageBuilder.withPayload("延时消息").build();
    rocketMQTemplate.syncSend(topic,message,3000,3);
}

Tag消息过滤

发送消息
@Test
public void testSendTagMsg(){
    String topic = "tagTopic";
    Message message1 = MessageBuilder.withPayload("消息1").build();
    Message message2 = MessageBuilder.withPayload("消息2").build();
    Message message3 = MessageBuilder.withPayload("消息3").build();
    rocketMQTemplate.sendOneWay(topic+":"+"TagA",message1);
    rocketMQTemplate.sendOneWay(topic+":"+"TagB",message2);
    rocketMQTemplate.sendOneWay(topic+":"+"TagC",message3);
}
消费消息
@Component
@RocketMQMessageListener(consumerGroup = "tagTopicGroup",topic = "tagTopic",selectorExpression = "TagA || TagC")
public class TagListener implements RocketMQListener {
    @Override
    public void onMessage(MessageExt messageExt) {
        System.out.println("Tag 消息内容为===>"+new String(messageExt.getBody()));
    }
}

SQL消息过滤

发送者
@Test
public void testSendSqlMsg(){
    String topic = "sqlTopic";
    Message message1 = MessageBuilder.withPayload("用户1").setHeader("age",20).build();
    Message message2 = MessageBuilder.withPayload("用户2").setHeader("age",35).build();
    Message message3 = MessageBuilder.withPayload("用户3").setHeader("age",66).build();
    rocketMQTemplate.sendOneWay(topic,message1);
    rocketMQTemplate.sendOneWay(topic,message2);
    rocketMQTemplate.sendOneWay(topic,message3);
}
消费者
@Component
@RocketMQMessageListener(consumerGroup = "sqlTopicGroup",topic = "sqlTopic",selectorType = SelectorType.SQL92,selectorExpression = "age < 30")
public class SqlListener implements RocketMQListener {
    @Override
    public void onMessage(MessageExt messageExt) {
        System.out.println("Tag 消息内容为===>"+new String(messageExt.getBody()));
    }
}

顺序消息

发送者
@Test
public void testSendOrderlyMsg(){
    String topic = "helloTopic";
    String id = "11";
    Message message1 = MessageBuilder.withPayload(id).build();
    rocketMQTemplate.setMessageQueueSelector((list, message, o) -> {
        long id1 = Long.parseLong(o.toString());
        int index = (int) (id1 % list.size());
        return list.get(index);
    });
    rocketMQTemplate.sendOneWayOrderly(topic,message1,id);
}
消费者
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(consumerGroup = "orderTopicGroup",topic = "orderTopic",consumeMode = ConsumeMode.ORDERLY)
public class OrderListener implements RocketMQListener {
    @Override
    public void onMessage(MessageExt messageExt) {
        System.out.println("线程===>"+Thread.currentThread().getName()+",队列ID===>"+messageExt.getQueueId()+",消息内容为===>"+new String(messageExt.getBody()));
    }
}

死信队列

  • 消息者在消息处理完成之后,会给消息中间件反馈处理的情况(RocketMQ已经帮我们做了),这个反馈叫ACK
  • 死信队列:消息中间件给消费者发送消息,如果消费不成功,隔段时间会继续发送,但是重试的次数是有限的,重试16次,如果重试16次都不成功,说明程序无法自动消费该消息,该消息会进入到死信队列DLQ队列中,通常需要监听,拿出这些消息出来,进行手动处理。
  • 重试间隔:30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h

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