当前位置:网站首页>Rockermq message sending mode
Rockermq message sending mode
2022-06-27 08:33:00 【Brother baa is invincible】
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
maven rely on
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.6.1</version>
</dependency>Producer 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
边栏推荐
猜你喜欢

Design of a solar charge pump power supply circuit

粗读DS-TransUNet: Dual Swin Transformer U-Net for Medical Image Segmentation

AQS underlying source code of concurrent programming JUC

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

並發編程JUC的AQS底層源碼

oracle用一条sql查出哪些数据不在某个表里
![[MySQL basic] general syntax 1](/img/f2/fb38409c034546e503d08a0b96cc61.png)
[MySQL basic] general syntax 1

Redis的事务

Creation process and memory layout of objects at JVM level

MySQL lock details
随机推荐
[original] typescript string UTF-8 encoding and decoding
Redis五种基本类型
vim 从嫌弃到依赖(20)——global 命令
[ 扩散模型(Diffusion Model) ]
[batch dos-cmd command - summary and summary] - output / display command - echo
三道基础面试题总结
JVM常见的垃圾收集器
内存泄露的最直接表现
2022.6.26-----leetcode.710
this,构造器,静态,之间调用,必须搞懂啊!
Correctly understand MySQL mvcc
See how much volatile you know
MySQL lock details
【原创】TypeScript字符串utf-8编码解码
Coggle 30 days of ML July competition learning
Redis installation under Linux
VIM from dislike to dependence (19) -- substitution
Matlab tips (19) matrix analysis -- principal component analysis
LVGL GUI GUIDER移植代码到STM32
[10. difference]