Spring Tips: Apache RocketMQ
Join the DZone community and get the full member experience.Join For Free
Hi, Spring fans! In this installment of Spring Tips, we’re going to look at Alibaba’s Apache RocketMQ. We’ve talked some about Alibaba in Spring Tips before. Check out the earlier Spring Tips installment in which we explore some of Spring Cloud Alibaba.
Running Apache RocketMQ
In order to use Apache RocketMQ, you’ll need to follow the steps in the RocketMQ quickstart. This Spring Tips installment introduces Apache RocketMQ, originally a technology developed and used internally at Alibaba and proven in the forge of 11/11, the famous Chinese sales holiday, sort of like “Cyber Monday,” or “Black Friday,” in the US. Sort of like that, but waaaaaay bigger. In 2019, Alibaba (alone, with no other e-commerce engines involved), made almost $40 billion USD in 24 hours. This required that trillions of messages be sent through something that could scale to meet the demand. RocketMQ is the only thing they could trust.
You’ll need to use Java 8 when running Apache RocketMQ. (You can use any version of Java when writing Spring applications that connect to Apache RocketMQ, of course.) I use SDK Manager (“SDKman” -
sdk) to switch to the appropriate version of Java.
That’ll install a version that works if it’s not already installed. Once that’s done, you’ll then need to run the NameServer.
Then you’ll need to run the Broker itself.
If you want to use SQL-based filtering, you need to add a property to the broker’s configuration,
$ROCKETMQ_HOME/conf/broker.conf, and then tell RocketMQ to use that configuration.
I use a script like this to launch everything.
Let’s look at a simple producer class that uses the Spring Boot autoconfiguration and the
In order to work with this, you’ll need to create a new project on the Spring Initializr. I generated a new project with the latest version of Java and then I made sure to include
Lombok. We also need the Apache RocketMQ client and the appropriate Spring Boot autoconfiguration:
The autoconfiguration will create a connection to the running Apache RocketMQ broker, informed by certain properties.
The first property,
name-server, tells the application where the Apache RocketMQ nameserver lives. The nameserver, then, knows where the broker lives. You’ll need to also specify a group for both the producer and the consumer. Here, we use
I don’t know if it can get much simpler than that! It’s a simple for-loop, processing each name, creating a new
Greeting object, and then using the
RocketMQTemplate to send the payload to an Apache RocketMQ topic,
greetings-topic. Here, we’ve used the overload of the
RocketMQTemplate object that accepts a
MessagePostProcessor is a callback in which we can transform the Spring Framework
Message object that will be sent out. In this example, we contribute a header value,
letter, that contains the first letter of the name. We’ll use this in the consumer.
Let’s look at the consumer. Generate a new Spring Boot application from the Spring Initializr and be sure to add the Apache RocketMQ autoconfiguration. You’ll need to specify the name server in
application.properties for the client, too.
The autoconfiguration supports defining beans that implement
T is the type of the payload that the consumer will receive. The payload, in this case, is the
In this example, the
SimpleConsumer simply logs all incoming messages from the
greetings-topic topic in Apache RocketMQ. Here, the consumer will process every message on the topic. Let’s look at another nice feature - selectors - that let us selectively process incoming messages. Let’s replace the existing RocketMQ listener with two new ones. Each one will use a SQL92-compatible predicate to determine whether incoming messages should be processed. One listener processes only the messages that have a
letter header matching
t. The other matches only those whose
letter header matches
Not bad, eh? There’s plenty of other things that Apache RocketMQ supports (besides processing trillions of messages in 24 hours!) It can store long tail messages on disk, without degrading performance. It supports serialization - the ordering of - of messages, transactions, batch processing, etc. It even supports scheduled messages - messages that are only delivered after a certain interval. Needless to say, I’m a big Apache RocketMQ fan.
Published at DZone with permission of Josh Long, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.