How to use RocketMQ in Java to achieve message communication
RocketMQ is a distributed messaging middleware that has the characteristics of high reliability, high throughput, low latency, and scalability. It is developed and open-source by Alibaba Group and is a messaging middleware used by Alibaba in production environments.
The main advantages of RocketMQ include:
1. High reliability: RocketMQ uses a master-slave architecture and has a replica mechanism to ensure high reliability of messages.
2. High throughput: RocketMQ uses a pull based consumption method, supporting batch sending and receiving of messages, and can achieve message throughput of millions per second.
3. Low latency: RocketMQ supports asynchronous message sending and sequential messaging, providing low latency message transmission.
4. Scalability: RocketMQ supports horizontal scalability, allowing for the addition or reduction of message servers based on business needs, while also supporting dynamic configuration and hot updates.
5. Multi language support: RocketMQ provides clients in multiple languages such as Java, C++, Python, Go, etc., which can be easily integrated into various applications.
The drawbacks of RocketMQ include:
1. The configuration is relatively complex: The deployment and configuration of RocketMQ require a certain technical background and experience.
2. Relatively limited functionality: Compared to other messaging middleware, RocketMQ has relatively less functionality and does not support advanced features such as distributed transactions.
The following is the complete sample code for implementing RocketMQ message sending and receiving using Java:
1. Introducing Maven dependencies:
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.5.2</version>
</dependency>
2. Message sending:
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
public class RocketMQProducer {
public static void main(String[] args) throws Exception {
//Create a producer and specify the producer group name
DefaultMQProducer producer = new DefaultMQProducer("producer_group");
//Set Namesrv addresses, separating multiple addresses with semicolons
producer.setNamesrvAddr("127.0.0.1:9876");
//Start Producer Instance
producer.start();
//Create a message object and specify the message subject, label, and content
Message message = new Message("topic", "tag", "Hello, RocketMQ".getBytes());
//Send a message and obtain the sending result
SendResult result = producer.send(message);
System. out. println ("Message sending result:"+result);
//Close Producer Instance
producer.shutdown();
}
}
3. Message reception:
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.consumer.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
public class RocketMQConsumer {
public static void main(String[] args) throws InterruptedException, MQClientException {
//Create a consumer and specify the consumer group name
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_group");
//Set Namesrv addresses, separating multiple addresses with semicolons
consumer.setNamesrvAddr("127.0.0.1:9876");
//Subscribe to message themes and tags
consumer.subscribe("topic", "*");
//Register a message listener to process messages
consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
for (MessageExt message : msgs) {
System. out. println ("Received message:"+new String (message. getBody()));
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
//Launch Consumer Instance
consumer.start();
System. out. println ("Consumer started successfully");
}
}
Sample configuration:
In the 'conf' folder under the installation directory of RocketMQ, there are two key configuration files: 'namesrv. properties' and' broker. properties'` Namesrv. properties' configures the network and memory parameters of Namesrv, while 'broker. properties' configures the network, memory, and storage parameters of Broker. You can make corresponding modifications to these two configuration files according to your needs.
RocketMQ official document:[ https://rocketmq.apache.org/ ]( https://rocketmq.apache.org/ )