[toc]
這是開始使用Spring AMQP的5分鐘之旅。
先決條件:安裝并運行RabbitMQ代理(http://www.rabbitmq.com/download.html)。 然后抓住spring-rabbit JAR及其所有依賴項 - 最簡單的方法是在構建工具中聲明依賴項,例如: 對于Maven:
~~~maven
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
~~~
## 兼容性
Spring Framework的最小版本依賴性是5.0.x.
最小的`amqp-client `java客戶端庫版本是5.0.0。
## 簡單使用
使用簡單的命令式Java來發送和接收消息:
~~~ java
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
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抽象。 我們依賴于代理中的默認交換機(因為在發送中沒有指定),并且默認所有隊列按名稱綁定默認交換機(因此我們可以使用隊列名作為發送中的路由鍵)。 這些行為在AMQP規范中定義。
## 使用xml配置
~~~java
ApplicationContext context =
new GenericXmlApplicationContext("classpath:/rabbit-context.xml");
AmqpTemplate template = context.getBean(AmqpTemplate.class);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");
~~~
~~~xml
<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/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/beans
http://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,并代表用戶向代理聲明它們,因此不需要在簡單的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");
}
}
~~~
- 1.前言
- 2.介紹
- 2.1 快速瀏覽
- 3.參考
- 3.1 使用spring amqp
- 3.1.1 AMQP抽象
- 3.1.2 資源的連接和管理
- 介紹
- 配置底層客戶端連接工廠
- RabbitConnectionFactoryBean和配置SSL
- 路由連接工廠
- 隊列親和力和LocalizedQueueConnectionFactory
- 發送確認和返回
- 3.1.3 添加自定義客戶端連接屬性
- 3.1.4 AmqpTemplate
- 介紹
- 添加重試功能
- 發送消息是異步的 - 如何檢測成功和失敗
- 發布的確認和返回
- 3.1.5 發送消息
- 介紹
- 消息構建 API
- 發布的返回
- 3.1.6 接收消息
- 介紹
- 輪詢消費者
- 異步消費者