RocketMQ(七):跟着官网学习敲代码

本文围绕RocketMQ展开,介绍了定时、顺序、事务三种消息的操作。包括分别创建对应主题,发送同步、异步定时和顺序消息,发送同步事务消息,以及使用SimpleConsumer和PushConsumer消费这三种消息。

接上篇:RocketMQ(六):跟着官网学习敲代码

1 定时消息

        在/bin目录下执行以下命令,创建定时主题:

sh mqadmin updateTopic -c DefaultCluster -t DelayTopic001 -o true -n 127.0.0.1:9876 -a +message.type=DELAY

1.1 发送同步、异步定时消息

public class DelayMessageTest {
    private static final String TOPIC_DELAY = "DelayTopic001";
    private static final String TAG = "TAG01";

    public static void main(String[] args) throws ClientException {
        //加载服务
        final ClientServiceProvider provider= ClientServiceProvider.loadService();
        //创建producer
        Producer producer = ProducerSingleton.getInstance(TOPIC_DELAY);
       //发送 同步定时消息
        //sendDelayMessage(provider,producer) ;
        //发送异步定时消息
        sentDelayMessageAsync(provider,producer);
    }
    
    public static void sendDelayMessage(ClientServiceProvider provider,Producer producer){
        for (int i = 0; i < 20; i++) {
            //消息体
            byte[] messageBody = ("孔乙己"+i).getBytes(StandardCharsets.UTF_8);
            //设置消息延迟时间-3秒钟
            Duration messageDelayTimes = Duration.ofSeconds(3);
            //设置消息属性
            Message message = provider.newMessageBuilder()
                    .setTag(TAG)
                    .setTopic(TOPIC_DELAY)
                    //设置唯一索引键
                    .setKeys("001")
                    .setBody(messageBody)
                    .setDeliveryTimestamp(System.currentTimeMillis() + messageDelayTimes.toMillis())
                    .build();
            try {
                SendReceipt sendReceipt = producer.send(message);
                log.info("发送定时消息成功,messageId = {}" ,sendReceipt.getMessageId());
            }catch (Throwable t){
                log.error("发送定时消息失败",t);
            }
        }
        
    }
    //发送异步定时消息
    public static void sentDelayMessageAsync(ClientServiceProvider provider,Producer producer){
        for (int i = 0; i < 20; i++) {
            //消息体
            byte[] messageBody = ("孔乙己"+i).getBytes(StandardCharsets.UTF_8);
            Duration messageDelayTimes = Duration.ofSeconds(3);
            //设置消息属性
            Message message = provider.newMessageBuilder()
                    .setTag(TAG)
                    .setTopic(TOPIC_DELAY)
                    //设置唯一索引键
                    .setKeys("001")
                    .setBody(messageBody)
                    .setDeliveryTimestamp(System.currentTimeMillis() + messageDelayTimes.toMillis())
                    .build();

            CompletableFuture<SendReceipt> future = producer.sendAsync(message);
            ExecutorService threadPool = Executors.newFixedThreadPool(6);
            future.whenCompleteAsync((sendReceipt,throwable) ->{
                if(null != throwable){
                    log.info("消息发送失败");
                }
                log.info("消息发送成功,messageId = {}", sendReceipt.getMessageId());
            },threadPool);
        }
    }
}

1.2 分别使用SimpleConsumer和PushConsumer消费定时消息

package com.jay.demo01;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.apis.ClientConfiguration;
import org.apache.rocketmq.client.apis.ClientException;
import org.apache.rocketmq.client.apis.ClientServiceProvider;
import org.apache.rocketmq.client.apis.consumer.*;
import org.apache.rocketmq.client.apis.message.MessageId;
import org.apache.rocketmq.client.apis.message.MessageView;
import org.apache.rocketmq.shaded.com.google.errorprone.annotations.Var;

import java.io.IOException;
import java.time.Duration;
import java.util.Collections;
import java.util.List;

@Slf4j
public class PushConsumerAndSimpleConsumerTest {
    //接入点
    private static final String ENDPOINT = "x.x.x.x:8081";
    //普通消息消费者组名
    private static final String GROUPNAME = "groupName01";
    private static final String DEYALGROUPNAME = "delayGroupName001";
    //Tag标签
    private static final String TAG = "TAG01";
    //普通主题
    private static final String TOPIC = "topic001";
    private static final String TOPIC_DELAY = "DelayTopic001";
    //定时消息主题
    public static void main(String[] args) throws ClientException, IOException, InterruptedException {
        final ClientServiceProvider provider = ClientServiceProvider.loadService();
        //测试PushConsumer
        //testPushConsumer(provider);
        //测试SimpleConsumer
        testSimpleConsumer(provider);
    }

    public static void testPushConsumer(ClientServiceProvider provider)throws InterruptedException, IOException, ClientException{
        //定义ClientConfiguration,设置接入点
        ClientConfiguration clientConfiguration = ClientConfiguration.newBuilder()
                .setEndpoints(ENDPOINT)
                .build();
        //定义过滤表达式
        FilterExpression filterExpression = new FilterExpression(TAG, FilterExpressionType.TAG);
        //定义PushConsumer
        log.info("pushConsumer 开始接收信息");
        PushConsumer pushConsumer = provider.newPushConsumerBuilder()
                //设置配置信息
                .setClientConfiguration(clientConfiguration)
                //设置消费者组
                .setConsumerGro
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

geminigoth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值