spring-boot - Spring Boot AMQP - 启动时的连接创建/队列声明
问题描述
Spring Boot 2.3.1 spring-boot-starter-amqp
如果我通过运行 main 方法(Intellij - 创建Application 类型的运行配置并提供主类 DemoApplication)将 Spring Boot 应用程序作为 Java 应用程序运行,则不会在启动时创建 RabbitMQ 连接。但是,当我作为 Spring Boot 应用程序运行(Intellij - 创建Spring Boot 类型的运行配置并提供主类 DemoApplication)时,会在启动期间创建 RabbitMQ 连接。
两者有什么区别?当 main 方法作为 java 应用程序运行时,为什么 RabbitMQ 不能在启动时创建连接?该行不出现。
INFO 32385 --- [*.*.*.*] o.s.a.r.c.CachingConnectionFactory : Created new connection: rabbitConnectionFactory.publisher#3f499c2f:0/SimpleConnection@4e5ef6a0 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 62061]
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>14</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
应用程序.yaml
spring:
rabbitmq:
host: ${RABBITMQ_HOST:localhost}
port: ${RABBITMQ_PORT:5672}
username: ${RABBITMQ_USERNAME:guest}
password: ${RABBITMQ_PASSWORD:guest}
主班
package com.example.demo;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
DirectExchange demoExchange() {
return new DirectExchange("demo-exchange");
}
@Bean
Queue demoQueue() {
return new Queue("demo-queue");
}
@Bean
Binding demoBinding() {
return BindingBuilder.bind(demoQueue()).to(demoExchange()).with("demo-routing");
}
}
Intellij - 创建 Application 类型的运行配置并提供主类 (DemoApplication)
解决方案
您的代码中没有任何内容可以打开连接(例如@RabbitListener
)。
也许执行器 ( RabbitHealthIndicator
) 正在打开一个连接 - 不知道为什么它会影响你如何启动它。
我没有看到任何一种连接方式(使用 STS)。
如果您将 a 添加@RabbitListener
到您的应用中;无论哪种方式,您都会看到连接。
推荐阅读
- python - 我怎样才能从这个函数中得到归一化矩阵?
- sql-server - 如何使用 SQL Server 和 AWS RSD 读取 XML 文件?
- sql - 在配置单元表中使用可为空参数的查询
- c# - 为什么在重新加载游戏场景时开始一个新游戏时它还记得一个标志状态为真?
- javascript - 添加事件监听器不是函数
- python - 当我在 Python 中写入 CSV 文件时,它会复制最后一行的一部分并将其写入文件底部
- python - Numpy数组在for循环中获得“ValueError:设置带有序列的数组元素”?
- r - 重置闪亮的单选按钮,updateRadioButtons(selected = character(0)) 不起作用
- typescript - 打字稿-对象可能未定义-“修复”类型定义的正确方法
- arrays - 将数据帧数据类型从对象转换为 float64 的问题