Message queues. The three message queues are Pegion, Kafka and RabbitMQ. Pegion is a component developed by the company itself. Today, the process of integrating RabbitMQ in SpringBoot, storing messages in a message queue and consuming them.
Introduction to Message Broker and AMQP
Message Broker is an architectural pattern for Message validation, transmission, and routing that is designed to be used in the following scenarios:
- Messages are routed to one or more destinations
- Messages are translated into other representations
- Perform aggregation of messages, decomposition of messages, and sending the results to their destinations, and then regrouping accordingly back to the message user
- Invoke the Web service to retrieve the data
- Response event or error
- Use a publish-subscribe model to provide content or topic-based message routing
AMQP is short for Advanced Message Queuing Protocol, which is an open standard application layer Protocol for message-oriented middleware. AMQP defines these features:
- The message direction
- The message queue
- Message routing (including point-to-point and publish-subscribe patterns)
- reliability
- security
RabbitMQ
RabbitMQ is a middleware product based on the AMQP protocol. It can support multiple operating systems, multiple programming languages and cover almost all mainstream enterprise technology platforms.
SpringBoot integration
Let’s get a feel for RabbitMQ by integrating RabbitMQ in a SpringBoot application and implementing a simple send and receive message example.
Integrating RabbitMQ into SpringBoot is easy because the Starter POMs module, AMQP, supports RabbitMQ well. Here’s how:
- Create a new SpringBoot project named “rabbitmq-hello”.
- Import the following dependencies in POP. XML, where spring-boot-starter-amqp is used to support RabbitMQ.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> The < version > 1.3.7. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>Copy the code
- Configure RabbitMQ connections and user information in application.properties. Users can go back to the installation above and create users on the admin page.
spring.application.name=rabbitmq-hello
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=spring
spring.rabbitmq.password=123456
Copy the code
- Create the message producer Sender. Messages are sent by injecting an instance of the AmqpTemplate interface, which defines a set of basic operations for the AMQP protocol. Its concrete implementation is injected in SpringBoot depending on the configuration. At that producer, we generate a string and send it to a queue named Hello.
@Component
public class Sender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String context = "hello " + new Date();
System.out.println("Sender : " + context);
this.rabbitTemplate.convertAndSend("hello", context); }}Copy the code
- Create a message consumer Receiver. The @RabbitListener annotation defines the class to listen on the Hello queue, and the @Rabbithandler annotation specifies what to do with the message. So, the consumer implements consumption of the Hello queue to output the string content of the message.
@Component
@RabbitListener(queues = "hello")
public class Receiver {
@RabbitHandler
public void process(String hello) {
System.out.println("Receiver : "+ hello); }}Copy the code
- Create the RabbitMQ configuration class RabbitConfig to configure advanced information such as queues, switches, and routes. Here we will focus on getting started, with minimal configuration defined to complete a basic production and consumption process.
@Configuration
public class RabbitConfig {
@Bean
public Queue helloQueue() {
return new Queue("hello"); }}Copy the code
- Create the main application class:
@SpringBootApplication public class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); }}Copy the code
- Create a unit test class to invoke message production:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = HelloApplication.class) public class HelloApplicationTests { @Autowired private Sender sender; @Test public void hello() throws Exception { sender.send(); }}Copy the code
Now that you’ve finished writing the program, let’s try it out. First make sure RabbitMQ Server is started and then do the following:
-
Starting the main application class, from the console, we see that the application has created a connection to springcloud in 127.0.0.1:5672. o.s.a.r.c.CachingConnectionFactory : Created new connection: SimpleConnection@29836d32 [delegate=amqp://[email protected]:5672/] Meanwhile, via the RabbitMQ control panel, You can see that Connection and Channels contain entries for the current Connection.
-
Running the unit test class, we can see the following output from the console: the message is sent to the RabbitMQ Server’s Hello queue. Sender : hello Sun Sep 25 11:06:11 CST 2016
-
Switching to the console of the application main class, we can see output similar to the following, where the consumer executes on the listener of the Hello queue and prints the received message information. Receiver : hello Sun Sep 25 11:06:11 CST 2016
And you’re done!