RocketMQ 1:生产者消息发送-单向发送、同步发送、异步发送

目录

      • 1.创建一个空的maven工程,引入rocket依赖
      • 单向发送
        • 2.创建单向发送类OneWayProducer
        • 3.查看打印
      • 同步发送
        • 4.创建同步发送类SyncProducer
        • 5.查看打印,消息按顺序发送,Queue 是数据分片的产物,默认是4个,可以修改配置参数defaultTopicQueueNums
      • 异步发送
        • 6.创建异步发送类AsyncProducer,使用countDownLatch保证10条消息同时发送,并打印反馈结果
        • 7.查看打印,因为是异步发送,所以顺序每次都会不一样

RocketMQ 1:生产者消息发送-单向发送、同步发送、异步发送_第1张图片

1.创建一个空的maven工程,引入rocket依赖

RocketMQ 1:生产者消息发送-单向发送、同步发送、异步发送_第2张图片



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>org.examplegroupId>
  <artifactId>RocketMQartifactId>
  <version>1.0-SNAPSHOTversion>

  <name>RocketMQname>

  <dependencies>
    
    <dependency>
      <groupId>org.apache.rocketmqgroupId>
      <artifactId>rocketmq-clientartifactId>
      <version>4.4.0version>
    dependency>
  dependencies>

project>

单向发送

单向(Oneway)发送特点为发送方只负责发送消息,不等待服务器回应且没有回调函数触发,即只发送请求不等待应答。此方式发送消息的过程耗 时非常短,一般在微秒级别。

2.创建单向发送类OneWayProducer

RocketMQ 1:生产者消息发送-单向发送、同步发送、异步发送_第3张图片

package org.example.normal;

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

/**
 * 单向发送
 **/
public class OneWayProducer {

    public static void main(String[] args) throws Exception {
        //生产者实例化
        DefaultMQProducer producer = new DefaultMQProducer("oneway");
        //指定mq服务器地址
        producer.setNamesrvAddr("192.168.42.112:9876");
        //启动实例
        producer.start();
        for (int i = 0; i < 10; i++) {
            //创建一个消息实例,指定topic、tag和消息体
            Message msg = new Message("TopicA", "TagA",
                    ("Hello RocketMQ" + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            //发送消息
            producer.sendOneway(msg);
            System.out.println(new String(msg.getBody()));
        }
        //实例关闭
        producer.shutdown();
    }
}

3.查看打印

RocketMQ 1:生产者消息发送-单向发送、同步发送、异步发送_第4张图片

同步发送

4.创建同步发送类SyncProducer

package org.example.normal;

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.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * 同步发送
 */
public class SyncProducer {
    public static void main(String[] args) throws Exception {
        //生产者实例化
        DefaultMQProducer producer = new DefaultMQProducer("sync");
        //指定mq服务器地址
        producer.setNamesrvAddr("192.168.42.112:9876");
        //启动实例
        producer.start();
        for (int i = 0; i < 10; i++) {
            //创建一个消息实例,指定topic、tag和消息体
            Message msg = new Message("TopicA", "TagB",
                    ("Hello RocketMQ" + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            //发送消息
            SendResult sendResult = producer.send(msg);
            System.out.println("status:" + sendResult.getSendStatus());
            System.out.println("MsgId:" + sendResult.getMsgId());
            System.out.println("QueueId:" + sendResult.getMessageQueue().getQueueId());
            System.out.println("value:" + new String(msg.getBody()));
        }
        //实例关闭
        producer.shutdown();
    }
}

5.查看打印,消息按顺序发送,Queue 是数据分片的产物,默认是4个,可以修改配置参数defaultTopicQueueNums

RocketMQ 1:生产者消息发送-单向发送、同步发送、异步发送_第5张图片

status:SEND_OK
MsgId:C0A800681C3418B4AAC289C740830000
QueueId:1
value:Hello RocketMQ0
status:SEND_OK
MsgId:C0A800681C3418B4AAC289C740910001
QueueId:2
value:Hello RocketMQ1
status:SEND_OK
MsgId:C0A800681C3418B4AAC289C740950002
QueueId:3
value:Hello RocketMQ2
status:SEND_OK
MsgId:C0A800681C3418B4AAC289C7409A0003
QueueId:0
value:Hello RocketMQ3
status:SEND_OK
MsgId:C0A800681C3418B4AAC289C7409C0004
QueueId:1
value:Hello RocketMQ4
status:SEND_OK
MsgId:C0A800681C3418B4AAC289C7409E0005
QueueId:2
value:Hello RocketMQ5
status:SEND_OK
MsgId:C0A800681C3418B4AAC289C740A00006
QueueId:3
value:Hello RocketMQ6
status:SEND_OK
MsgId:C0A800681C3418B4AAC289C740A10007
QueueId:0
value:Hello RocketMQ7
status:SEND_OK
MsgId:C0A800681C3418B4AAC289C740A30008
QueueId:1
value:Hello RocketMQ8
status:SEND_OK
MsgId:C0A800681C3418B4AAC289C740A50009
QueueId:2
value:Hello RocketMQ9

异步发送

6.创建异步发送类AsyncProducer,使用countDownLatch保证10条消息同时发送,并打印反馈结果

package org.example.normal;

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.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * 异步发送
 **/
public class AsyncProducer {
    public static void main(String[] args) throws Exception {
        //生产者实例化
        DefaultMQProducer producer = new DefaultMQProducer("async");
        //指定mq服务器地址
        producer.setNamesrvAddr("192.168.42.112:9876");
        //启动实例
        producer.start();
        //发送异步失败时的最大重试次数,默认2次,这里不重试
        producer.setRetryTimesWhenSendAsyncFailed(0);

        int count = 10;
        final CountDownLatch countDownLatch = new CountDownLatch(count);
        for (int i = 0; i < count; i++) {
            final int index = i;
            //创建一个消息实例,指定topic、tag和消息体
            final Message msg = new Message("TopicA", "TagC", "OrderID" + index,
                    ("Hello RocketMQ" + index).getBytes(RemotingHelper.DEFAULT_CHARSET));

            //发送异步消息
            producer.send(msg, new SendCallback() {
                public void onSuccess(SendResult sendResult) {
                    countDownLatch.countDown();
                    System.out.println("success:" + index + "," + new String(msg.getBody()));
                }
                public void onException(Throwable throwable) {
                    countDownLatch.countDown();
                    System.out.println("Exception:" + index + "," + throwable);
                    throwable.printStackTrace();
                }
            });
        }
        countDownLatch.await(5, TimeUnit.SECONDS);
        //实例关闭
        producer.shutdown();
    }
}

7.查看打印,因为是异步发送,所以顺序每次都会不一样

RocketMQ 1:生产者消息发送-单向发送、同步发送、异步发送_第6张图片
gitee下载地址
https://gitee.com/fisher3652/RocketMQ.git

你可能感兴趣的:(RocketMQ)