Spring AMQP RabbitMq快速启动

半兽人 发表于: 2017-11-22   最后更新时间: 2018-05-08 10:53:30  
{{totalSubscript}} 订阅, 3,369 游览

2. 介绍

第一部分是Spring AMQP的高级概述,一些基本概念和一些代码片段,并快速运行。

2.1.1 引入

这是Spring AMQP开始的5分钟的旅程。

先决条件:安装并运行RabbitMQ broker(https://www.rabbitmq.com/download.html ) 获取spring-rabbit JAR及其所有依赖关系 - 最简单的方法就是在构建中声明依赖关系,例如Maven:

<dependency>
  <groupId>org.springframework.amqp</groupId>
  <artifactId>spring-rabbit</artifactId>
  <version>2.0.0.RELEASE</version>
</dependency>

gradle的:

compile 'org.springframework.amqp:spring-rabbit:2.0.0.RELEASE'

兼容性

  • 最小的Spring Framework版本依赖是5.0.x.

  • 最小的amqp-client java客户端库版本是4.1.0。

  • 注意这是指java客户端库; 一般来说,它与老的broker版本兼容。

非常,非常快

使用简单的命令式Java来发送和接收消息:

ConnectionFactory connectionFactory = new CachingConnectionFactory();
AmqpAdmin admin = new RabbitAdmin(connectionFactory);
admin.declareQueue(new Queue("myqueue"));
AmqpTemplate template = new RabbitTemplate(connectionFactory);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");

请注意,本地Java Rabbit客户端中也有一个ConnectionFactory。我们在其之上,使用Spring抽象。我们依靠broker中的默认交换(因为没有在发送中指定)以及所有队列默认绑定到默认交换的名称(因此我们可以使用队列名称作为发送中的路由key)。这些都在AMQP规范中定义。

使用XML配置

与上面的例子相同,但将资源配置到外部的XML中:

ApplicationContext context =
    new GenericXmlApplicationContext("classpath:/rabbit-context.xml");
AmqpTemplate template = context.getBean(AmqpTemplate.class);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xmlns:rabbit="https://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="https://www.springframework.org/schema/rabbit
           https://www.springframework.org/schema/rabbit/spring-rabbit.xsd
           https://www.springframework.org/schema/beans
           https://www.springframework.org/schema/beans/spring-beans.xsd">

    <rabbit:connection-factory id="connectionFactory"/>

    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>

    <rabbit:admin connection-factory="connectionFactory"/>

    <rabbit:queue name="myqueue"/>

</beans>

默认情况下,<rabbit:admin/>声明会自动查找Queue,Exchange和Binding类型的bean,并代表用户将它们声明给broker,因此不需要在简单的Java驱动程序中显式使用该bean。有很多选项可以配置XML模式中组件的属性 - 你可以使用XML编辑器的自动完成功能来浏览它们并查看其文档。

使用Java配置

Java中的外部配置也是同样的例子:

ApplicationContext context =
    new AnnotationConfigApplicationContext(RabbitConfiguration.class);
AmqpTemplate template = context.getBean(AmqpTemplate.class);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");

........

@Configuration
public class RabbitConfiguration {

    @Bean
    public ConnectionFactory connectionFactory() {
        return new CachingConnectionFactory("localhost");
    }

    @Bean
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(connectionFactory());
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory());
    }

    @Bean
    public Queue myQueue() {
       return new Queue("myqueue");
    }
}
更新于 2018-05-08
在线,11分钟前登录

查看RabbitMq更多相关的文章或提一个关于RabbitMq的问题,也可以与我们一起分享文章