RocketMQ Client 3.6.2.final framework introduction and usage guide
RocketMQ is a reliable, high -performance distributed message queue system.In order to interact with RocketMQ, we can use RocketMQ Client 3.6.2.final framework.This article will introduce the basic concepts and guidelines of the RocketMQ Client 3.6.2.final framework, and provide some Java code examples.
Rocketmq Client 3.6.2.final framework is divided into two main parts: producers and consumers.Producers are used to send messages to message queues, and consumers are used to receive messages from the message queue.
First, we need to introduce Rocketmq Client 3.6.2.final dependence.It can be achieved by adding the following dependencies in Maven's pom.xml file:
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>3.6.2</version>
</dependency>
Next, we will introduce how to use the RocketMQ Client framework to achieve basic producers and consumer functions.
### Producer (PRODUCER)
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 default message producer
DefaultMQProducer producer = new DefaultMQProducer("producer_group");
// Specify the address of nameServer
producer.setNamesrvAddr("localhost:9876");
// Start the producer
producer.start();
// Create a message
Message message = new Message("topic", "tag", "Hello, RocketMQ!".getBytes());
// Send messages and get the result of sending results
SendResult sendResult = producer.send(message);
System.out.println ("Send results:" + Sendresult);
// Close the producer
producer.shutdown();
}
}
In the above code, we created a message producer called "Producer_group" and specified the address of NameServer.We then created a message object and used the producer to send the message.The results of the sending will be printed.
### Consume (Consume)
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
public class RocketMQConsumer {
public static void main(String[] args) throws Exception {
// Create a default push type message Consumers
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_group");
// Specify the address of nameServer
consumer.setNamesrvAddr("localhost:9876");
// Subscribe to message
consumer.subscribe("topic", "*");
// Register message monitor
consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
for (MessageExt msg : msgs) {
// Treat the message
System.out.println ("Receive message:" + New String (msg.getBody ()));
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
// Start consumers
consumer.start();
}
}
In the above code, we created a message consumer named "Consumer_group" and specified the address of NameServer.Then, we subscribed all the messages of the "Topic" theme and registered a message monitor.When the news arrives, the listener will print the message content.
RocketMQ Client 3.6.2.final framework provides rich configuration options and advanced features, such as message order, transaction messages, etc., can be configured and used according to requirements in practical applications.
In summary, this article introduces the basic concepts and guidelines of the RocketMQ Client 3.6.2.final framework, and provides some Java code examples.By mastering the RocketMQ Client framework, we can easily implement the sending and receiving of the message, and provide a reliable solution for message communication of the distributed system.