SpringBoot 系列 | 第十篇:使用RabbitMQ收发消息

本文介绍如何在SpringBoot项目中集成RabbitMQ实现消息收发。通过添加依赖、配置参数及编写代码,实现了消息的发送与接收。文章还详细介绍了配置过程及代码示例。

SpringBoot 系列 | 第十篇:使用RabbitMQ收发消息

本篇文章参考了官方文档:Messaging with RabbitMQ

Spring Boot中使用RabbitQM需要机器安装好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 AMQPRabbitTemplate提供了需要使用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!>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值