💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[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"); } } ~~~