General message
RocketMQ provides three ways to send ordinary messages: reliable synchronous sending, reliable asynchronous sending and one-way sending.
Reliable synchronous transmission
Synchronous sending refers to the communication mode in which the sender sends data and sends the next packet after receiving the echo from the receiver.
This method has a wide range of application scenarios, such as important notification email, enrollment SMS notification, marketing SMS system, etc.
Reliable asynchronous transmission
Asynchronous sending refers to the communication mode in which the sender sends the data and then sends the next packet without waiting for the receiver to send back the response. The sender receives the server response through the callback interface and processes the response results
Asynchronous sending refers to the communication mode in which the sender sends the data and then sends the next packet without waiting for the receiver to send back the response. The sender receives the server response through the callback interface and processes the response results
One way delivery
One way sending means that the sender is only responsible for sending the message, does not wait for the server to respond and does not trigger the callback function, that is, only sending the request and does not wait for the response.
It is suitable for some scenarios with very short time consumption but low reliability requirements, such as log collection.
<!--rely on--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency>
package com.leon.test; import com.leon.OrderApplication; import org.apache.rocketmq.client.producer.SendCallback; import org.apache.rocketmq.client.producer.SendResult; import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = OrderApplication.class) public class MessageTypeTest { @Autowired private RocketMQTemplate rocketMQTemplate; //Sync message @Test public void testSyncSend() { //Parameter 1: topic:tag //Parameter 2: message body //Parameter 3: timeout SendResult result = rocketMQTemplate.syncSend("test-topic-1:tag", "This is a synchronous message", 10000); System.out.println(result); } //Asynchronous message @Test public void testAsyncSend() throws InterruptedException { //Parameter 1: topic:tag //Parameter 2: message body //Parameter 3: callback rocketMQTemplate.asyncSend("test-topic-1", "This is an asynchronous message", new SendCallback() { //Callback for successful response @Override public void onSuccess(SendResult result) { System.out.println(result); } //Callback for abnormal response @Override public void onException(Throwable throwable) { System.out.println(throwable); } }); System.out.println("=================="); Thread.sleep(300000000); } //One way message @Test public void testOneWay() { for (int i = 0; i < 10; i++) { rocketMQTemplate.sendOneWay("test-topic-1", "This is a one-way message"); } } //One way sequential message @Test public void testOneWayOrderly() { for (int i = 0; i < 10; i++) { //The third parameter is used to determine which queue these messages are sent to rocketMQTemplate.sendOneWayOrderly("test-topic-1", "This is a one-way message","xx"); } } }
Comparison of three transmission modes
Sending method | Send TPS | Send result feedback | reliability |
---|---|---|---|
Send synchronously | fast | Yes | No loss |
Asynchronous sending | fast | Yes | No loss |
One way delivery | Fastest | nothing | Possible loss |