Basic knowledge of Java RocketMQ Quick Start


How to use it

1. Introducing Rocketmq-client


2. Writing Producer

DefaultMQProducer producer = new DefaultMQProducer("producer_demo");
// Specify NameServer address
Producer.setNamesrvAddr (";"); and // revised to its own
* The Producer object must be initialized by calling start before it is used. It can be initialized once.
* Note: Keep in mind that the start method cannot be invoked every time a message is sent

for (int i = 0; i < 997892; i++) {
try {
// Building messages
Message msg = new Message("TopicTest" /* Topic */,
"TagA" /* Tag */,
("Test RocketMQ" + i). getBytes (RemotingHelper. DEFAULT_CHARSET)
// Send Synchronized Messages
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
} catch (Exception e) {

3. Writing Consumer

* Consumer Group, a very important concept, will be gradually added in the future
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_demo");
// Specify NameServer address, with multiple addresses separated;
Consumer.setNamesrvAddr (";"); //Modified to your own
* Setting Consumer to start first from the head of the queue or from the tail of the queue
* If it's not the first time, continue to consume according to the position of last consumption.
consumer.subscribe("TopicTest", "*");
consumer.registerMessageListener(new MessageListenerConcurrently() {
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
ConsumeConcurrentlyContext context) {
try {
for(MessageExt msg:msgs){
String msgbody = new String(msg.getBody(), "utf-8");
System.out.println ("MessageBody:"+msgbody);//Output message content
} catch (Exception e) {
Return Consume ConcurrentlyStatus.RECONSUME_LATER;//Try again later
Return Consume ConcurrentlyStatus.CONSUME_SUCCESS;//Success in Consumption
System.out.printf("Consumer Started.%n");

4. Explanation

According to your environment, you can modify the value of NamesrvAddr. For my cluster, please refer to RocketMQ cluster deployment configuration. Later, through the RocketMQ console, you can see the previously built multi-Master multi-Slave mode, which replicates the cluster mode asynchronously.

5. Through RocketMQ Control Console

Rocketmq-console-ng is acquired by: rocketmq-console-ng, and then compiled by mavne to obtain jar. The commands are as follows:

mvn clean package -Dmaven.test.skip=true
java -jar target/rocketmq-console-ng-1.0.0.jar

After getting the rocketmq-console-ng-1.0.0.jar, find the file, and modify the value of rocketmq.config.namesrvAddr according to your NamesrvAddr.

Direct Start:

java -jar rocketmq-console-ng-1.0.0.jar

The console is based on spring boot. Spring boot is very convenient and popular, so it is necessary to learn spring boot (in fact, spring series, so spring must also be studied in depth), and then observe the operation through the console.

6. Operational observation

A good habit is to run Consumer first, then Producer, and then watch through the rocketmq-console-ng console.

After running, it is true that the amount of broker-a data plus broker-b data is equal to the amount of data we send, and the number of slaves is the same as the number of masters. The effect is as follows:

Looking at the data sent, the disks of the two machines are as follows:

So far, the RocketMQ Quick Start is over.

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.