当前位置:网站首页>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
边栏推荐
猜你喜欢

Coggle 30 days of ML July competition learning

Oracle uses an SQL to find out which data is not in a table

Creation process and memory layout of objects at JVM level

lvgl使用demo及说明2

浏览器的markdown插件显示不了图片

Lvgl description 3 about the use of lvgl Guide

win10为任意文件添加右键菜单

IO管脚配置和pinctrl驱动

IMX8QXP DMA资源和使用(未完结)

Object含有Copy方法?
随机推荐
[daily practice] realization of product card animation effect
Associated GIS: all roads lead to ue5 City
Lvgl usage demo and instructions 2
RockerMQ消息发送模式
Lvgl description 3 about the use of lvgl Guide
[batch dos-cmd command - summary and summary] - output / display command - echo
The difference between ArrayList and LinkedList
Redis configuration file details
[MySQL basic] general syntax 1
About the problem that the El date picker Click to clear the parameter and make it null
2022.06.26 (LC Luo 6101 Luo determines whether the matrix is an X matrix)
This, constructor, static, and inter call must be understood!
[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
Flow chart of Alipay wechat payment business
[12. maximum continuous non repeating subsequence]
(original) custom drawable
Pin details in rust
Tips for using Jupiter notebook
Correctly understand MySQL mvcc
[notes on c++ primer] Chapter 3 string, vector and array