当前位置:网站首页>Rockermq message sending and consumption mode
Rockermq message sending and consumption mode
2022-06-27 08:33:00 【Brother baa is invincible】
Catalog
Differences among three transmission modes
Message sending mode
brief introduction
RocketMQ There are three sending modes , They are synchronous transmission 、 Send asynchronously 、 Send one way , Different patterns are applicable to different business scenarios
Code
public static void main(String[] args) {
DefaultMQProducer defaultProducer = getDefaultProducer();
Producer producer = new Producer();
producer.Sync(defaultProducer);
producer.Async(defaultProducer);
producer.oneway(defaultProducer);
defaultProducer.shutdown();
}
public static DefaultMQProducer getDefaultProducer() {
try {
DefaultMQProducer producer = new DefaultMQProducer("producerGroup");
producer.setNamesrvAddr("localhost:9876");
producer.start();
return producer;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* The synchronous
* @param producer
*/
public void Sync(DefaultMQProducer producer) {
try {
// After the synchronization message fails to be sent , Resend several times
producer.setRetryTimesWhenSendFailed(0);
Message msg = new Message("topic", " The synchronous ".getBytes());
SendResult res = producer.send(msg);
System.out.println("res" + res);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Send asynchronously
* @param producer
*/
public void Async(DefaultMQProducer producer) {
try {
// After asynchronous message sending fails , Resend several times
producer.setRetryTimesWhenSendAsyncFailed(0);
Message msg = new Message("topic", " Asynchronous messaging ".getBytes());
producer.send(msg, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
System.out.println("sendResult:" + sendResult);
}
@Override
public void onException(Throwable throwable) {
System.out.println("throwable:" + throwable);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Send one way
* @param producer
*/
public void oneway(DefaultMQProducer producer) {
try {
Message msg = new Message("topic", " Send one way ".getBytes());
producer.sendOneway(msg);
}catch (Exception e) {
e.printStackTrace();
}
}Differences among three transmission modes
The synchronous : Message sent to master broker And sync to slave broker after , Will respond to the client , Slow efficiency , But the risk of losing data is small
Send asynchronously : Message sent to master broker And then respond to the client , No need to wait for a successful sync to slave broker, Efficient , The risk is high . for example master broker After processing the message , After responding to the client , Not synchronized to slave broker
Send one way : Producers only need to produce messages , There is no need to broker Return results , The most efficient , The highest risk , Applicable to scenarios where message loss is allowed
Message consumption mode
brief introduction
RocketMQ There are two consumption patterns , Cluster (CLUSTERING) And broadcast (BROADCASTING), The default is cluster mode . Consumption patterns are defined by consumers .
Cluster consumption mode
Introduce
Cluster consumption mode means that messages are consumed by only one consumer in the cluster , If there are multiple clusters , Each cluster will consume only once , When a message is re delivered, it cannot be guaranteed that it will be routed to the same machine , Message status by broker maintain
Code
public static void main(String[] args) throws Exception {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumerGroup");
consumer.setNamesrvAddr("localhost:9876");
consumer.subscribe("topic", "*");
consumer.setMessageModel(MessageModel.CLUSTERING);
consumer.registerMessageListener((MessageListenerConcurrently) (msgList, context) -> {
for (MessageExt msg : msgList) {
System.out.println(new String(msg.getBody()));;
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
consumer.start();
System.out.println("ConsumerA start...");
}step
Start the two consumers with the above code A、B

Use the producer code to send a message or create... In the monitoring platform topic And send a message
The steps for the monitoring platform to send messages are as follows

As a result, only one consumer will receive the message
Broadcast consumption mode
Introduce
Broadcast messages are messages that are pushed to all currently registered consumers in the cluster , If there is no consumer online at present, it will wait until a consumer pulls a message , But if consumption fails, it will not be reinvested
demonstration
Put the above code to MessageModel Switch to BROADCASTING that will do
consumer.setMessageModel(MessageModel.BROADCASTING);The other steps are the same
边栏推荐
- Several cases that do not initialize classes
- Code source AQS sous - jacent pour la programmation simultanée juc
- MySQL lock details
- Analysis log log
- [c++ primer notes] Chapter 4 expression
- Redis的事务
- 招聘需求 视觉工程师
- 今日3大面试Demo[Integer ASCII 类关系]
- SQL Sever column name or number of supplied values does not match the table definition
- Tips for using Jupiter notebook
猜你喜欢

MATLAB小技巧(19)矩阵分析--主成分分析

并发编程JUC的AQS底层源码

Mapping of Taobao virtual product store opening tutorial
![[MySQL basic] general syntax 1](/img/f2/fb38409c034546e503d08a0b96cc61.png)
[MySQL basic] general syntax 1

Zabbix部署说明(Server+Win客户端+交换机(H3C))

MySQL锁详解

ServletConfig与ServletContext

Object含有Copy方法?
![[batch dos-cmd command - summary and summary] - parameters%0,%1,%2,%[0-9],%0-9 in the batch command and batch command parameter position switching command shift, operator% usage in the DOS command](/img/05/19299c47d54d4ede95322b5a923093.png)
[batch dos-cmd command - summary and summary] - parameters%0,%1,%2,%[0-9],%0-9 in the batch command and batch command parameter position switching command shift, operator% usage in the DOS command

Redis的事务
随机推荐
正确的理解MySQL的MVCC
C# 解决使用SQLite 的相对路径问题
关于el-date-picker点击清空参数变为null的问题
Associated GIS: all roads lead to ue5 City
Some considerations on operation / method overloading for thread to release lock resources
100% understanding of 5 IO models
RockerMQ消息发送与消费模式
ArrayList和LinkedList的区别
JVM common garbage collector
(original) custom drawable
Analysis of key technologies for live broadcast pain points -- second opening, clarity and fluency of the first frame
Reference | upgrade win11 mobile hotspot can not be opened or connected
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
Five basic types of redis
Static code block vs construction code block
That is, a one-stop live broadcast service with "smooth live broadcast" and full link upgrade
Lvgl usage demo and instructions 2
Imx8qxp DMA resources and usage (unfinished)
DataV轮播表组件dv-scroll-board宽度问题
See how much volatile you know