Spring boot 2.3.1 spring-boot-starter-amqp
If I run a spring boot app as a java application by running the main method (Intellij - Create a Run Configuration of type Application and provide the main class DemoApplication), RabbitMQ connection is not created at startup. However when I run as a Spring Boot app (Intellij - Create a Run Configuration of type Spring Boot and provide the main class DemoApplication) RabbitMQ connection is created during the startup.
What is the difference between the two? Why can't RabbitMQ create a connection on startup when the main method is run as java application? This line does not appear.
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>
application.yaml
spring:
rabbitmq:
host: ${RABBITMQ_HOST:localhost}
port: ${RABBITMQ_PORT:5672}
username: ${RABBITMQ_USERNAME:guest}
password: ${RABBITMQ_PASSWORD:guest}
Main class
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 - Create a Run Configuration of type Application and provide the main class (DemoApplication)
You have nothing in your code that opens a connection (e.g. @RabbitListener
).
Perhaps the actuator (RabbitHealthIndicator
) is opening a connection - not sure why it would make a difference how you launch it though.
I don't see a connection running either way (with STS).
If you add a @RabbitListener
to your app; you will see a connection either way.