资讯专栏INFORMATION COLUMN

SpringBoot非官方教程 | 第十五篇:Springboot整合RabbitMQ

HollisChuang / 2609人阅读

摘要:创建消息监听,并发送一条消息在程序中,提供了发送消息和接收消息的所有方法。

这篇文章带你了解怎么整合RabbitMQ服务器,并且通过它怎么去发送和接收消息。我将构建一个springboot工程,通过RabbitTemplate去通过MessageListenerAdapter去订阅一个POJO类型的消息。

准备工作
15min
IDEA
maven 3.0

在开始构建项目之前,机器需要安装rabbitmq,你可以去官网下载,http://www.rabbitmq.com/downl... ,如果你是用的Mac(程序员都应该用mac吧),你可以这样下载:

brew install rabbitmq

安装完成后开启服务器:

rabbitmq-server

开启服务器成功,你可以看到以下信息:

    

    RabbitMQ 3.1.3. Copyright (C) 2007-2013 VMware, Inc.
##  ##      Licensed under the MPL.  See http://www.rabbitmq.com/
##  ##
##########  Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log
######  ##        /usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log
##########
            Starting broker... completed with 6 plugins.
构建工程

构架一个SpringBoot工程,其pom文件依赖加上spring-boot-starter-amqp的起步依赖:


    org.springframework.boot
    spring-boot-starter-amqp

创建消息接收者

在任何的消息队列程序中,你需要创建一个消息接收者,用于响应发送的消息。

@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;
    }

}

消息接收者是一个简单的POJO类,它定义了一个方法去接收消息,当你注册它去接收消息,你可以给它取任何的名字。其中,它有CountDownLatch这样的一个类,它是用于告诉发送者消息已经收到了,你不需要在应用程序中具体实现它,只需要latch.countDown()就行了。
创建消息监听,并发送一条消息

在spring程序中,RabbitTemplate提供了发送消息和接收消息的所有方法。你只需简单的配置下就行了:

需要一个消息监听容器
声明一个quene,一个exchange,并且绑定它们
一个组件去发送消息

代码清单如下:

package com.forezp;

import com.forezp.message.Receiver;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;


@SpringBootApplication
public class SpringbootRabbitmqApplication {

     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) {
        SpringApplication.run(SpringbootRabbitmqApplication.class, args);
    }
}

创建一个测试方法:

@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(Application.queueName, "Hello from RabbitMQ!");
        receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
        context.close();
    }

}

启动程序,你会发现控制台打印:

Sending message...
Received 
总结

恭喜!你刚才已经学会了如何通过spring raabitmq去构建一个消息发送和订阅的程序。 这仅仅是一个好的开始,你可以通过spring-rabbitmq做更多的事,点击这里。

源码下载:https://github.com/forezp/Spr...
参考资料

https://spring.io/guides/gs/m...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/68035.html

相关文章

  • SpringBoot官方教程 | 第二十五篇:2小时学会springboot

    摘要:一什么是摘自官网翻译采纳了建立生产就绪应用程序的观点。优先于配置的惯例,旨在让您尽快启动和运行。致力于简洁,让开发者写更少的配置,程序能够更快的运行和启动。二搭建第一个程序可以在上建项目,也可以用构建。已经凌晨了,我要睡了源码 一.什么是spring boot Takes an opinionated view of building production-ready Spring a...

    baukh789 评论0 收藏0
  • SpringBoot官方教程 | 第五篇SpringBoot整合 beatlsql

    摘要:整合阶段由于没有对的快速启动装配,所以需要我自己导入相关的,包括数据源,包扫描,事物管理器等。另外它的中文文档比较友好。源码下载参考资料中文文档 BeetSql是一个全功能DAO工具, 同时具有Hibernate 优点 & Mybatis优点功能,适用于承认以SQL为中心,同时又需求工具能自动能生成大量常用的SQL的应用。 beatlsql 优点 开发效率 无需注解,自动使用大...

    microelec 评论0 收藏0
  • 一起来学SpringBoot | 第十二篇:初探RabbitMQ消息队列

    摘要:用于控制活动人数,将超过此一定阀值的订单直接丢弃。缓解短时间的高流量压垮应用。目前比较推荐的就是我们手动然后将消费错误的消息转移到其它的消息队列中,做补偿处理消费者该方案是默认的方式不太推荐。 SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相...

    Baoyuan 评论0 收藏0
  • SpringBoot RabbitMQ 整合使用

    摘要:可以在地址看到如何使用讲解下上面命令行表示控制台端口号,可以在浏览器中通过控制台来执行的相关操作。同时从控制台可以看到发送的速率多线程测试性能开了个线程,每个线程发送条消息。 showImg(http://ww2.sinaimg.cn/large/006tNc79ly1g5jjb62t88j30u00gwdi2.jpg); 前提 上次写了篇文章,《SpringBoot Kafka 整合...

    yuanxin 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<