Spring AMQP Project将Spring的核心观念应用于基于AMQP协议的消息解决方案,它包括两部分:spring-amqp是基础抽象,spring-rabbit是基于RabbitMQ的实现。
特征:
1)提供了一个”模板”RabbitTemplate来发送和接收消息
2)使用Listener Container用于异步处理进来的消息
3)使用RabbitAdmin自动声明Queues、Exchanges和Bindings
下面结合Spring amqp官方的Quick Start(http://projects.spring.io/spring-amqp/#quick-start)解释其用法。
1.在pom.xml (maven)中引入spring-rabbit dependency
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
2.在对应project的src/main/resources/目录下添加spring-rabbit.xml(我本机目录是src/main/resources/spring/spring-rabbitmq.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
<rabbit:connection-factory id="connectionFactory" host="192.168.73.128" port="5672"
username="tony" password="123" channel-cache-size="25"/>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
exchange="myExchange" routing-key="foo.bar"/>
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="myQueue"/>