跳到主要内容
版本:4.1.1

快速之旅(给没耐心的人)

Doubao 中英对照 Quick Tour for the impatient

简介​

这是五分钟快速上手 Spring AMQP 的之旅。

前置条件:安装并运行 RabbitMQ broker(https://www.rabbitmq.com/download.html)。然后获取 spring-rabbit JAR 及其所有依赖——最简单的方式是在你的构建工具中声明一个依赖。例如,对于 Maven,你可以这样做:

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

对于 Gradle,你可以这样做:

implementation 'org.springframework.amqp:spring-rabbit:4.1.1'

兼容性​

最低 Spring Framework 版本依赖为 7.0.0。

最低 amqp-client Java 客户端库版本为 5.28.0。

用于流队列的最低 stream-client Java 客户端库版本为 1.5.0。

非常非常快​

本节提供最快的入门介绍。

首先,添加以下 import 语句,使本节后面的示例能够运行:

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;

下例使用普通的命令式 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 的抽象。它会缓存 channel(以及可选地缓存连接)以供复用。我们依赖 broker 中的默认交换机(因为发送时未指定交换机),以及所有队列按其名称绑定到默认交换机的默认绑定(因此,在发送时我们可以将队列名用作路由键)。这些行为都定义在 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
https://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://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 schema 中有大量选项可用于配置这些组件的属性。你可以使用 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 CachingConnectionFactory connectionFactory() {
return new CachingConnectionFactory("localhost");
}

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

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

@Bean
public Queue myQueue() {
return new Queue("myqueue");
}
}

使用 Spring Boot 自动配置和异步 POJO 监听器​

Spring Boot 会自动配置这些基础设施 bean,如下例所示:

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Bean
public ApplicationRunner runner(AmqpTemplate template) {
return args -> template.convertAndSend("myqueue", "foo");
}

@Bean
public Queue myQueue() {
return new Queue("myqueue");
}

@RabbitListener(queues = "myqueue")
public void listen(String in) {
System.out.println(in);
}

}