【发布时间】:2020-06-19 10:39:18
【问题描述】:
春季启动 2.3.1 spring-boot-starter-amqp
如果我通过运行 main 方法(Intellij - 创建 type Application 的运行配置并提供主类 DemoApplication)将 Spring Boot 应用程序作为 java 应用程序运行,则不会在以下位置创建 RabbitMQ 连接启动。但是,当我作为 Spring Boot 应用程序运行时(Intellij - 创建 type 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)
【问题讨论】:
-
我看不出有什么不同;无论我如何启动它,我总是能获得连接;显示您的代码。
-
添加示例代码
-
另外 mvn spring-boot:run 不会在启动时创建连接。我看到 Spring Boot Run Configuration 有 Enable JMX 复选框,如果未选中,即使 Spring Boot Run 在启动期间也不会创建连接。我尝试将这些 JMX 选项添加到 Application Run 类型(主)但没有运气: -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true
标签: spring-boot spring-amqp spring-rabbit