无法使用Spring Boot向RabbitMQ队列发送简单的“Hello World”消息

时间:2017-08-15 15:40:39

标签: java spring spring-boot amqp spring-rabbitmq

我无法使用RabbitMQSpring Boot服务器发送消息。我没有看到任何例外。不知道发生了什么。

我具有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$$1c012f1] 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>

1 个答案:

答案 0 :(得分:1)

您缺少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 - 所有这些bean都将被创建。它是如何完成的? RabbitAdmin组件加载所有Declarable个bean并创建例如queues from Spring beans with type Queue

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

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管理员看到了:

enter image description here

enter image description here

当然这个队列不是事先创建的。我希望它有所帮助。