2. Introduction
参考文档的第一部分概述了 Spring AMQP 及其基本概念和一些代码片段,这些内容将使您尽快启动并运行。
2.1 不耐烦的快速浏览
2.1.1 Introduction
这是从 Spring AMQP 开始的 5 分钟导览。
先决条件:安装并运行 RabbitMQ 代理(http://www.rabbitmq.com/download.html)。然后获取 spring-rabbit JAR 及其所有依赖项-最简单的方法是在构建工具中声明一个依赖项,例如对于 Maven:
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.7.11.RELEASE</version>
</dependency>
对于 gradle:
compile 'org.springframework.amqp:spring-rabbit:1.7.11.RELEASE'
Compatibility
虽然默认的 Spring Framework 版本依赖性为 4.3.x,但是 Spring AMQP 通常与 Spring Framework 的早期版本兼容。但是,基于 Comments 的侦听器和RabbitMessagingTemplate
需要 Spring Framework 4.1 或更高版本。
最低amqp-client
javaClient 端库版本为 4.0.0.
非常非常快
使用简单的命令式 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 RabbitClient 端中也有一个ConnectionFactory
。我们在上面的代码中使用 Spring 抽象。我们依赖于代理中的默认交换(因为在 send 中未指定),以及所有队列通过其名称到默认交换的默认绑定(因此我们可以在发送中将队列名称用作路由键) 。这些行为在 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="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");
}
}
2.2 新增功能
2.2.1 从 1.6 开始的 1.7 更改
AMQPClient 端库
Spring AMQP 现在使用 RabbitMQ 团队提供的amqp-client
库的新 4.0.x 版本。此 Client 端默认情况下配置了自动恢复;参见“ RabbitMQ 自动连接/拓扑恢复”部分。
Note
默认情况下,4.0.xClient 端启用自动恢复;虽然与该功能兼容,但 Spring AMQP 拥有自己的恢复机制,通常不需要 Client 端恢复功能。建议禁用amqp-client
自动恢复,以避免在代理可用但连接尚未恢复时获得AutoRecoverConnectionNotCurrentlyOpenException
s。从* version 1.7.1 *开始,Spring AMQP 禁用它,除非您显式创建自己的 RabbitMQ 连接工厂并将其提供给CachingConnectionFactory
。 RabbitConnectionFactoryBean
创建的 RabbitMQ ConnectionFactory
实例默认情况下也会禁用该选项。
Log4j2 upgrade
现在,最低的 Log4j2 版本(对于AmqpAppender
)为2.7
。该框架不再与以前的版本兼容。有关更多信息,请参见第 3.2 节“记录子系统 AMQP 附加程序”。
Logback Appender
默认情况下,此附加程序不再捕获呼叫者数据(方法,行号);可以通过设置includeCallerData
配置选项来重新启用它。有关可用的日志附加程序的信息,请参见第 3.2 节“记录子系统 AMQP 附加程序”。
Spring 重试升级
现在,最低的 Spring Retry 版本是1.2
。该框架不再与以前的版本兼容。
Shutdown Behavior
现在,您可以将forceCloseChannel
设置为true
,这样,如果容器线程不响应shutdownTimeout
内的关闭,则将强制关闭通道,从而使所有未确认的消息重新排队。有关更多信息,请参见第 3.1.15 节“消息侦听器容器配置”。
FasterXML Jackson 升级
现在,最低的 Jackson 版本是2.8
。该框架不再与以前的版本兼容。
JUnit @Rules
到目前为止,框架内部使用的规则现在已在单独的 jar spring-rabbit-junit
中提供。有关更多信息,请参见第 3.4.4 节“ JUnit @Rules”。
容器有条件回滚
使用外部事务 Management 器(例如 JDBC)时,现在在为容器提供事务属性时支持基于规则的回滚。现在,在使用 Transaction 建议时,它也更加灵活。
连接命名策略
现在提供了一个新的ConnectionNameStrategy
,以从AbstractConnectionFactory
填充目标 RabbitMQ 连接的特定于应用程序的标识。有关更多信息,请参见第 3.1.2 节“连接和资源 Management”。
侦听器容器更改
Transaction 回滚行为
现在,无论是否配置了事务 Management 器,都可以将事务回滚时的消息重排队配置为一致。有关更多信息,请参见名为“有关回滚收到的邮件的 Comments”部分。
连接工厂 Bean 更改
RabbitConnectionFactoryBean
现在提供了enabaleHostnameVerification
属性;将其设置为true
以启用主机名验证。使用 Java 6 时也提供setHostnameVerifier
;有关更多信息,请参见连接工厂 javadocs。主机名验证要求将amqp-client
覆盖为 4.8.0 或更高版本。
2.2.2 早期版本
有关以前版本的更改,请参见第 A.2 节“早期版本”。