SpringBoot 系列 | 第十篇:使用RabbitMQ收发消息
本篇文章参考了官方文档:Messaging with RabbitMQ
Spring Boot中使用RabbitQM需要机器安装好RabbitMQ。没有了解RabbitMQ的可以参考一下文章:
- RabbitMq使用 | 第一篇:安装和Hello World
- RabbitMq使用 | 第二篇:消息队列和确认
- RabbitMQ使用 | 第三篇:发布/订阅模式
- RabbitMQ 使用 | 第四篇:路由选择
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置
spring.rabbitmq.host=120.55.72.39
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
创建一个消息接受者
@Component
public class Receiver {
private CountDownLatch latch = new CountDownLatch(1);
public void receiveMessage(String message) {
System.out.println("Received <" + message + ">");
latch.countDown();
}
public CountDownLatch getLatch() {
return latch;
}
}
这个Receiver
是一个简单的POJO
,定义了一个方法用于接收消息,名字可以自定义
为了简便,这个
POJO
有一个CountDownLatch
.在这里的场景用于确保消息收到之后程序才会关闭。
注册消息接受者和发送消息
Spring AMQP
的RabbitTemplate
提供了需要使用RabbitMQ
发送和接收消息的功能,需要定义以下配置:
A message listener container
Declare the queue,the exchange,and the binding between them
A components to send some messages to test the listener
SpringBoot
自动创建了一个ConnectFactory
和一个RabbitTemplate
。
程序代码:
@SpringBootApplication
public class Application {
final static String queueName = "spring-boot";
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
TopicExchange exchange() {
return new TopicExchange("spring-boot-exchange");
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(queueName);
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Application.class, args);
}
}
上述代码定义了一个listenerAdapter
方法声明消息接收监听器,会监听spring-boot
队列的消息,因为Receiver
是一个POJO
,他需要使用一个MessageListenerAdapter
来包装,定义了它的接收方法为receiveMessage
发送一条消息
@Component
public class Runner implements CommandLineRunner {
private final RabbitTemplate rabbitTemplate;
private final Receiver receiver;
private final ConfigurableApplicationContext context;
public Runner(Receiver receiver, RabbitTemplate rabbitTemplate,
ConfigurableApplicationContext context) {
this.receiver = receiver;
this.rabbitTemplate = rabbitTemplate;
this.context = context;
}
@Override
public void run(String... args) throws Exception {
System.out.println("Sending message...");
rabbitTemplate.convertAndSend("spring-boot-exchange","spring-boot", "Hello from RabbitMQ!");
receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
context.close();
}
}
运行程序,你会发现如下输出:
Sending message...
Received <Hello from RabbitMQ!>