RABBITMQ basic components and Springboot integration RABBITMQ Simple Example

Source: Internet
Author: User
Tags rabbitmq

Exchanger (Exchange)

The switch is like a router, we first send the message to the exchanger, and then the switch based on the BIND key (binding key) and the producer sends the message when the routing key Routingkey,

Exchange types (fanout,direct,topic) deliver messages to the corresponding queue by Exchange type. (It is important to understand this concept, which is fully reflected in the code that follows).

RABBITMQ basic knowledge to view Message Queuing RABBITMQ basic knowledge

Queuing (queue)

The queue that holds the message.

bind (binding)

How does the exchanger know which queue to deliver this message to? This requires a binding. That is, using a binding key to bind a queue to a switch (Exchange), so the exchanger knows which queue to post the message to based on the routing key. (This is fully reflected in the following code)

Join RabbitMQ maven Dependency

Configuration

Configuration in the Application.yaml file

Rabbitmqconfig.java Configuration

@Configuration

public class Rabbitmqconfig {

Public final static String queue_name = "Spring-boot-queue";

Public final static String exchange_name = "Spring-boot-exchange";

Public final static String Binding_key = "spring.boot.key.#";

Create a queue

@Bean

Public queue Queue () {

return new Queue (queue_name);

}

Create a topic type of exchanger

@Bean

Public Topicexchange Exchange () {

return new Topicexchange (Exchange_name);

}

To bind a queue to a switch (Exchange) using the routing key (Routingkey)

@Bean

Public binding binding (queue queue, Topicexchange Exchange) {

return Bindingbuilder.bind (Queue). to (Exchange). with (Binding_key);

}

}

Note: The above configuration is Topicexchange

In real business, multiple queues and binding can be configured to meet demand.

producers

The Convertandsend method of calling Rabbittemplate directly is possible. As you can see from the code below, instead of sending the message directly to the queue, we send it to the exchanger, which then posts our message to the corresponding queue based on the routing key.

@RestController

public class Producercontroller {

@Autowired

Private Rabbittemplate rabbittemplate;

@GetMapping ("/sendmessage")

Public String SendMessage () {

New Thread ((), {

for (int i = 0; i <; i++) {

String value = new DateTime (). toString ("Yyyy-mm-dd HH:mm:ss");

Console.log ("Send Message {}", value);

Rabbittemplate.convertandsend (Rabbitmqconfig.exchange_name, Rabbitmqconfig.routing_key, value);

}

}). Start ();

return "OK";

}

}

Consumer

The consumer is also very simple, only need the corresponding method to add @RabbitListener annotations, specify the queue name to listen to.

Run the project

Run the project, then open the browser and enter Http://localhost:9999/sendMessage (specific address according to the server). In the console you can see the producers are constantly sending messages, consumers continue to consume messages.

Control Desk

Open the RabbitMQ Web console, and you can see the switches and queues that we have just configured in the code, as well as binding information.

View the details of the exchanger

View queues

This article transferred from: https://www.toutiao.com/i6578064914143773187/

RABBITMQ basic components and Springboot integration RABBITMQ Simple Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.