This is the 27th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

Before we talked about the concept of RocketMQ, let us finish the book, this time to talk about the specific use of RocketMQ, the company’s main push OF MQ is RocketMQ, back and forth to read a little bit of the source code, used for a month, talk about the basic part. It can meet your daily business needs.

review

RocketMQ is a message queue with high performance supporting distributed construction and high throughput. It is an open source middleware of Alibaba. When I was in college, my classmate finished the project and asked me a question: How can the back-end server written in Python communicate with your Java? However, RocketMQ can fulfill the role of communication and solve the above problems. Of course, MQ has many application scenarios, the above is just one of them. It can also be used for peak clipping and valley filling to resist concurrent pressure and bring high performance returns to the server.

The premise

Before starting to use MQ, we need to import the package of MQ. Since I am the project of the company, the package I use is the official starter automatic assembly package. With SpringBoot, the project can be built quickly to achieve agile development. You can also import the native MQ package from Maven’s central repository.

<dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-spring-boot-starter</artifactId> The < version > 2.1.0 < / version > < / dependency >Copy the code

consumer

After importing the Mq JAR package, click on the JAR package to find one of these annotations in his file

With this annotation we can implement simple consumer listening, with the Topic implementation connecting to the MQ Broker for information.

ConsumerGroup = "wopen_gateway_consumer_group", Topic = "${stong. Wopen. Rocketmq. Topic. Gateway}") / / use yml to inject @ Slf4j public class GatewayConsumer implements RocketMQListener<DeviceVo>{ @Autowired private GatewayService service; @Autowired private PushMessageService pushMessageService; @override public void onMessage(DeviceVo gatewayVo) {Copy the code

Injecting a topic in the form of YML makes it possible to change a topic without modifying the code.

producer

The producer’s official MQ based start package is easier for developers to use. It demonstrates the synchronous method invocation, which is the same as the one-way send, but returns a different result.

@Autowired private RocketMQTemplate mqTemplate; / / injection Mqtemplate @ Value (" ${WFTP. Rocketmq. Topic. The aerator} ") / / injection needs to send the topic above the consumer USES configurable topic private String aeratorTopic;Copy the code

In a word, it is ten thousand times easier than the original native send. Other asynchronous and synchronous methods are the same. Note that there is a callback that needs to be handled by you.

SendResult SendResult = mqtemplate. syncSend(aeratorTopic, business data);Copy the code