Basic knowledge of Java RocketMQ Quick Start

Time:2019-8-11

How to use it

1. Introducing Rocketmq-client


<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.1.0-incubating</version>
</dependency>

2. Writing Producer

DefaultMQProducer producer = new DefaultMQProducer("producer_demo");
// Specify NameServer address
Producer.setNamesrvAddr ("192.168.116.115:9876; 192.168.116.116:9876"); 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
*/
producer.start();

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) {
e.printStackTrace();
Thread.sleep(1000);
}
}
producer.shutdown();

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 ("192.168.116.115:9876; 192.168.116.116:9876"); //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.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.subscribe("TopicTest", "*");
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
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) {
e.printStackTrace();
Return Consume ConcurrentlyStatus.RECONSUME_LATER;//Try again later
}
Return Consume ConcurrentlyStatus.CONSUME_SUCCESS;//Success in Consumption
}
});
consumer.start();
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 rocketmq-console-ng-1.0.jarBOOT-INFclassesapplication.properties 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.