当前位置:网站首页>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
边栏推荐
- 今日3大面试Demo[Integer ASCII 类关系]
- DataV轮播表组件dv-scroll-board宽度问题
- 並發編程JUC的AQS底層源碼
- 100% understanding of 5 IO models
- Today's three interviews demo[integer ASCII class relationship]
- 即构「畅直播」,全链路升级的一站式直播服务
- 一种太阳能电荷泵供电电路的方案设计
- [cloud native] 2.3 kubernetes core practice (Part 1)
- 【原创】TypeScript字符串utf-8编码解码
- Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?
猜你喜欢
随机推荐
ArrayList和LinkedList的区别
How Oracle converts strings to multiple lines
第十一章 信号(一)- 概念
Redis configuration file details
直接修改/etc/crontab 文件内容,定时任务不生效
DV scroll board width of datav rotation table component
[10. difference]
Rough reading DS transunet: dual swing transformer u-net for medical image segmentation
Code source AQS sous - jacent pour la programmation simultanée juc
浏览器的markdown插件显示不了图片
oracle用一条sql查出哪些数据不在某个表里
正确的理解MySQL的MVCC
Coggle 30 days of ML July competition learning
【云原生】2.3 Kubernetes 核心实战(上)
数字IC-1.9 吃透通信协议中状态机的代码编写套路
三道基础面试题总结
VIM from dislike to dependence (19) -- substitution
MySQL lock details
[batch dos-cmd command - summary and summary] - how to distinguish the internal command and external command of CMD, and the difference between CMD command and run (win+r) command,
[ 扩散模型(Diffusion Model) ]