Java Spring引导未连接到RabbitMQ

Java Spring引导未连接到RabbitMQ,java,spring,spring-boot,rabbitmq,spring-amqp,Java,Spring,Spring Boot,Rabbitmq,Spring Amqp,更新:将非工作版本发布到 我正在尝试实现一些基本的RabbitMQ发送和接收功能,但似乎无法在我的两个Spring引导项目中都正常工作。虽然我已经尝试了多个示例,但到目前为止,我在实现了示例,唯一的例外是我在Application.java中用@Configuration注释的单独RabbitConfig类中定义了bean 我还尝试了与示例中完全相同的方法,但是这也不起作用 有趣的是,该实现在一个(旧的)Spring引导项目中工作,而在一个新的引导项目中不工作。在另一个项目中,运行时期间的输出向

更新:将非工作版本发布到

我正在尝试实现一些基本的RabbitMQ发送和接收功能,但似乎无法在我的两个Spring引导项目中都正常工作。虽然我已经尝试了多个示例,但到目前为止,我在实现了示例,唯一的例外是我在Application.java中用@Configuration注释的单独RabbitConfig类中定义了bean

我还尝试了与示例中完全相同的方法,但是这也不起作用

有趣的是,该实现在一个(旧的)Spring引导项目中工作,而在一个新的引导项目中不工作。在另一个项目中,运行时期间的输出向我显示到RabbitMQ的连接已成功设置:

输出工作项目的一部分

@SpringBootApplication
public class Main {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Main.class);
        app.setWebEnvironment(false);
        ConfigurableApplicationContext appContext = app.run(args);
        name.tcp.TcpServer tcpServer = new name.tcp.TcpServer();
    }

}
package name;

import name.tcp.TcpServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.IOException;

@SpringBootApplication
public class Main extends SpringBootServletInitializer {

    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(Main.class, args);
        TcpServer tcpServer = new TcpServer();
        System.out.println("starting TCP server from main");
    }

}
输出非工作项目

@SpringBootApplication
public class Main {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Main.class);
        app.setWebEnvironment(false);
        ConfigurableApplicationContext appContext = app.run(args);
        name.tcp.TcpServer tcpServer = new name.tcp.TcpServer();
    }

}
package name;

import name.tcp.TcpServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.IOException;

@SpringBootApplication
public class Main extends SpringBootServletInitializer {

    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(Main.class, args);
        TcpServer tcpServer = new TcpServer();
        System.out.println("starting TCP server from main");
    }

}
它还显示为RabbitMQ管理界面中队列的使用者

在我的另一个项目中,没有显示这些内容。但是,在这两个项目中都会找到兔子配置类并加载bean(使用检查)

两个项目共享相同的依赖项“spring boot starter amqp”。它们还共享相同的application.properties文件,其中包含连接到本地RabbitMQ服务器的正确信息。我能做些什么来找出为什么一个项目正在加载兔子 配置bean是否正确,而另一个是否正确

下面我附上了一些文件,我认为这些文件有助于更好地理解问题,但如果需要任何其他文件或输出,请让我知道。我改了一些包裹的名字 为了隐私问题

谢谢大家!

问候,, 拉尔斯95

工作项目 弹簧靴1.5.3

非工作项目 弹簧靴1.5.9

application.properties(两个项目相同,端口归属于Docker,映射到5276)

RabbitConfig.java(两个项目相同)

java(两个项目相同)

Main.java(工作项目)

java(两个项目相同)

当应用程序启动时,看到复制了该问题的方法,就会初始化,从而执行
startListen()
方法

该方法有一个无限循环,没有创建任何单独的线程,因此阻塞了启动过程的剩余部分(下面的代码摘录)


删除该组件表明RabbitMQ侦听器已经启动,因此我建议(至少)为您的
TcpServer

使用不同的
线程,因为这个故事实际上是关于Spring Boot的,您介意准备最简单的非工作状态版本并通过GitHub与我们共享吗?我们将把你的项目拉到本地,并努力找出瓶颈。谢谢,我会的!我将重命名一些包,然后再将其推送到GitHub,然后在此处共享链接。非工作项目中的
main
是否与
@Configuration
处于相同的包层次结构中?您可以显示工作包,但不显示非工作包。默认情况下,Boot只在主包和子包中查找配置。不,不是这样,但是我尝试了两种方法,因为我在另一个堆栈溢出回复中也看到了这一点。在工作项目中,它似乎还从配置包中获取了RabbitConfig。我在这里发布了一个不工作的项目(对实现细节进行了一些重命名和删除):在这个版本中,它也不在相同的包层次结构中,但即使移动它,它仍然不工作。删除
TcpServer
类使你的应用程序连接到RabbitMQ服务器。我猜
while(true)
没有帮助:)
package name.queue;

import org.springframework.stereotype.Component;

import java.util.concurrent.CountDownLatch;

@Component
public class Receiver {

    private CountDownLatch latch = new CountDownLatch(1);

    public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
        latch.countDown();
    }

    public CountDownLatch getLatch() {
        return latch;
    }
}
@SpringBootApplication
public class Main {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Main.class);
        app.setWebEnvironment(false);
        ConfigurableApplicationContext appContext = app.run(args);
        name.tcp.TcpServer tcpServer = new name.tcp.TcpServer();
    }

}
package name;

import name.tcp.TcpServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.IOException;

@SpringBootApplication
public class Main extends SpringBootServletInitializer {

    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(Main.class, args);
        TcpServer tcpServer = new TcpServer();
        System.out.println("starting TCP server from main");
    }

}
package name.queue;

import java.util.concurrent.TimeUnit;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import name.configuration.RabbitConfig;

@Component
public class Runner implements CommandLineRunner {

    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public Runner(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        rabbitTemplate.convertAndSend(RabbitConfig.queueName, "Hello from RabbitMQ!");
    }

}
public void startListen() throws IOException {
    while (true) {
        Socket clientSocket = serverSocket.accept();
        threadPool.submit(new XClientHandler(clientSocket,eventProducer));
    }
}