当前位置:网站首页>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

原网站

版权声明
本文为[Brother baa is invincible]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270814520593.html