2017-08-15 631 views
1

我无法使用Spring BootRabbitMQ服务器发送消息。我没有看到任何异常。不知道发生了什么事。无法使用Spring Boot向RabbitMQ队列发送简单的“Hello World”消息

我对管理控制台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.spring[email protected]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/[email protected] [delegate=amqp://[email protected]: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> 
+0

您可以尝试[this](http://docs.spring.io/spring-amqp/reference/htmlsingle/#management-template),看看您是否能够获得所有已声明的队列? –

+0

你跟着这个https://spring.io/guides/gs/messaging-rabbitmq/? – Barath

+0

@Darshan - 是的,我确实收到200个OK队列名称的响应 – user2325154

回答

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类型的多个豆 - 所有的人都将被创建。它是如何完成的? RabbitAdmin组件加载全部Declarable豆,并创建例如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管理:

enter image description here

enter image description here

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

+0

好的,这是'Barath'发回几分钟的链接。我会研究这一点。但是我有两个问题:1)我当前的程序来自SpringBoot的''ActiveMQ''例子之一。有效!。因此,我修改了原来的程序,做了两处修改:a)使用'RabbitTemplate'而不是'JmsTemplate',并指向'RabbitMQ'服务器而不是'ActiveMQ'服务器b)我没有创建任何连接工厂或其中的任何东西它仍然有效。 2)上面列出的程序包含一个消息监听器。但是如果我只是一个出版商呢?我还需要一个吗? – user2325154

+0

@ user2325154感谢您获取更多详细信息。我已经更新了我的答案,以遵循您的特定用例。在这种情况下,你所要做的就是定义'Queue' bean,它将在初始化期间由'RabbitAdmin'组件注册。 –

+0

感谢您的更新。这工作! – user2325154

相关问题