我无法使用RabbitMQ
将消息发送到Spring 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>
最佳答案
您缺少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 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管理员中看到的是:
当然,此队列不是事先创建的。希望对您有所帮助。