无法使用 Spring 引导将简单的 "Hello World" 消息发送到 RabbitMQ 队列

Not able to send a simple "Hello World" message to RabbitMQ queue using Spring Boot

我无法使用 Spring BootRabbitMQ 服务器发送消息。我没有看到任何例外。不确定发生了什么。

我对 RabbitMQ 的管理控制台具有管理员级别的访问权限,可以查看队列是否正在创建。但我没有看到任何队列被创建。此外,在控制台日志中,我只看到了这个。

控制台日志:

2017-08-15 11:32:17.015  INFO 8256 --- [           main] com.study.jms.BasicApplication           : Starting BasicApplication on KOP-DBT0J12 with PID 8256 (C:\NITAL\MY-DATA\CODE-SAMPLES\SPRINGBOOT-MESSAGING-SAMPLES\rabbitmq\rabbitmq-helloworld-producer-demo\target\classes started by chandeln in C:\NITAL\MY-DATA\CODE-SAMPLES\SPRINGBOOT-MESSAGING-SAMPLES\rabbitmq\rabbitmq-helloworld-producer-demo)
2017-08-15 11:32:17.020  INFO 8256 --- [           main] com.study.jms.BasicApplication           : No active profile set, falling back to default profiles: default
2017-08-15 11:32:17.112  INFO 8256 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@73d4cc9e: startup date [Tue Aug 15 11:32:17 EDT 2017]; root of context hierarchy
2017-08-15 11:32:17.915  INFO 8256 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$c012f1] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-08-15 11:32:18.739  INFO 8256 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-08-15 11:32:18.749  INFO 8256 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'rabbitConnectionFactory' has been autodetected for JMX exposure
2017-08-15 11:32:18.749  INFO 8256 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'rabbitConnectionFactory': registering with JMX server as MBean [org.springframework.amqp.rabbit.connection:name=rabbitConnectionFactory,type=CachingConnectionFactory]
2017-08-15 11:32:18.769  INFO 8256 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase -2147482648
2017-08-15 11:32:18.779  INFO 8256 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
2017-08-15 11:32:18.839  INFO 8256 --- [           main] com.study.jms.BasicApplication           : Started BasicApplication in 2.208 seconds (JVM running for 2.668)
2017-08-15 11:32:18.883  INFO 8256 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory#2cc44ad:0/SimpleConnection@61f05988 [delegate=amqp://admin@172.25.20.43:5672/, localPort= 65025]

BasicApplication.java

@SpringBootApplication
public class BasicApplication {

    private static RabbitTemplate rabbitTemplate;

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(BasicApplication.class, args);
        rabbitTemplate = ctx.getBean(RabbitTemplate.class);
        rabbitTemplate.convertAndSend("helloworld.q", "Hello World !");
    }

}

application.properties

spring.rabbitmq.host=gsi-547576
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</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-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

您缺少 RabbitMQ Queue bean 定义:

@Bean
public Queue queue() {
    return new Queue("helloworld.q", false);
}

RabbitMQ 要求 在向它们发布任何消息之前创建队列。在 Spring Boot 中执行此操作的一种方法是定义 Queue bean,然后 Spring Boot 将处理它的创建。当你添加它时,你会在控制台日志中看到类似这样的内容:

2017-08-15 18:32:16.929  INFO 21163 --- [           main] o.s.amqp.rabbit.core.RabbitAdmin         : Auto-declaring a non-durable, auto-delete, or exclusive Queue (helloworld.q) durable:false, auto-delete:false, exclusive:false. It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.

当然你可以定义多个Queue类型的bean——它们都会被创建。它是怎么做到的? RabbitAdmin 组件加载所有 Declarable bean 并创建例如queues from Spring beans with type Queue.

我刚刚测试了以下简单的 Spring 启动应用程序:

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

    @Bean
    Queue queue() {
        return new Queue("helloworld.q", false);
    }

    public static void main(String[] args) throws InterruptedException {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        RabbitTemplate rabbitTemplate = ctx.getBean(RabbitTemplate.class);
        rabbitTemplate.convertAndSend("helloworld.q", "Hello World !");
    }
}

在 运行 之后,这是我在 RabbitMQ 管理中看到的:

当然这个队列不是事先创建的。希望对你有帮助。