Spring Boot unveiling and actual combat: RabbitMQ

Keywords: Spring RabbitMQ SpringBoot xml

Spring Boot integrates RabbitMQ

Spring Boot is very easy to integrate RabbitMQ. It only needs two steps.

First, add RabbitMQ dependency in pom.xml.

org.springframework.boot

spring-boot-starter-amqp

The second step is to configure the information in src/main/resources/application.properties.

#rabbitmq

spring.rabbitmq.host=localhost

spring.rabbitmq.port=5672

spring.rabbitmq.username=guest

spring.rabbitmq.password=guest

Practical drill

A simple practical start

Let's implement a simple message sending and receiving.

Configuration

Register a queue with @ Bean in Spring Boot.

@Configuration

public class RabbitMQConfig {

public static final String QUEUE_NAME = "spring-boot-simple";

@Bean

public Queue queue() {

return new Queue(QUEUE_NAME);

}

}

Message producer

Create the message producer Sender. The message is sent by injecting an instance of AmqpTemplate interface.

@Service

public class Sender {

@Autowired

private AmqpTemplate rabbitTemplate;

public void send() {

System.out.println("Liang Guizhao sends a message...");

rabbitTemplate.convertAndSend(RabbitMQConfig.QUEUE_NAME, "Hello, Liang Guizhao!");

}

}

Message consumer

Create a message consumer Receiver. Use the @ RabbitListener annotation to define listening to the queue.

@Service

public class Receiver {

@Autowired

private AmqpTemplate rabbitTemplate;

@RabbitListener(queues = "spring-boot-simple")

public void receiveMessage(String message) {

System.out.println("Received <" + message + ">");

}

}

Function

@SpringBootApplication

@EnableAutoConfiguration

@ComponentScan(basePackages = { "com.lianggzone.springboot" })

public class RunMain {

public static void main(String[] args) {

SpringApplication.run(RunMain.class, args);

}

}

unit testing

Create unit test cases

public class RabbitMQTest {

@Autowired

private Sender sender;

@Test

public void send() throws Exception {

sender.send();

}

}

Practice of routing

After the above practical cases, we have a certain understanding of Spring Boot integration RabbitMQ. Now, let's take a look at the RabbitMQ routing scenario.

Configuration

In RabbitMQConfig, we register queues, forwarders, listeners, etc.

@Configuration

public class RabbitMQConfig2 {

public static final String QUEUE_NAME = "spring-boot";

public static final String QUEUE_EXCHANGE_NAME = "spring-boot-exchange";

@Bean

public Queue queue() {

// Persistence or not

boolean durable = true;

// Private queues that can only be used by the creator and are automatically deleted after disconnection

boolean exclusive = false;

// Whether to delete the queue automatically after all consumer clients are disconnected

boolean autoDelete = false;

return new Queue(QUEUE_NAME, durable, exclusive, autoDelete);

}

@Bean

public TopicExchange exchange() {

// Persistence or not

boolean durable = true;

// Whether to delete the queue automatically after all consumer clients are disconnected

boolean autoDelete = false;

return new TopicExchange(QUEUE_EXCHANGE_NAME, durable, autoDelete);

}

@Bean

public Binding binding(Queue queue, TopicExchange exchange) {

return BindingBuilder.bind(queue).to(exchange).with(QUEUE_NAME);

}

@Bean

SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,

MessageListenerAdapter listenerAdapter) {

SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();

container.setConnectionFactory(connectionFactory);

container.setQueueNames(QUEUE_NAME);

container.setMessageListener(listenerAdapter);

return container;

}

@Bean

MessageListenerAdapter listenerAdapter(Receiver receiver) {

return new MessageListenerAdapter(receiver, "receiveMessage");

}

}

Message producer

Create the message producer Sender. The message is sent by injecting an instance of AmqpTemplate interface.

@Service

public class Sender {

@Autowired

private AmqpTemplate rabbitTemplate;

public void send() {

System.out.println("Liang Guizhao sends a message...");

rabbitTemplate.convertAndSend(RabbitMQConfig2.QUEUE_NAME, "Hello, Liang Guizhao!");

}

}

Message consumer

Create a message consumer Receiver. Use the @ RabbitListener annotation to define listening to the queue.

@Service

public class Receiver {

public void receiveMessage(String message) {

System.out.println("Received <" + message + ">");

}

}

Function

@SpringBootApplication

@EnableAutoConfiguration

@ComponentScan(basePackages = { "com.lianggzone.springboot" })

public class RunMain {

public static void main(String[] args) {

SpringApplication.run(RunMain.class, args);

}

}

unit testing

Create unit test cases

public class RabbitMQTest {

@Autowired

private Sender sender;

@Test

public void send() throws Exception {

sender.send();

}

}

Write at the end: welcome to leave a message to discuss, pay more attention and keep updating!!!

Posted by Jenling on Sun, 05 Jan 2020 03:05:15 -0800