Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法发送简单的“文件”;“你好,世界”;使用Spring引导发送到RabbitMQ队列的消息_Java_Spring_Spring Boot_Amqp_Spring Rabbit - Fatal编程技术网

Java 无法发送简单的“文件”;“你好,世界”;使用Spring引导发送到RabbitMQ队列的消息

Java 无法发送简单的“文件”;“你好,世界”;使用Spring引导发送到RabbitMQ队列的消息,java,spring,spring-boot,amqp,spring-rabbit,Java,Spring,Spring Boot,Amqp,Spring Rabbit,我无法使用Spring Boot向RabbitMQ服务器发送消息。我看不出有任何例外。不知道发生了什么事 我拥有对RabbitMQ管理控制台的管理员级访问权限,可以查看队列是否正在创建。但我没有看到创建任何队列。此外,在控制台日志中,我看到的只是这个 控制台日志: 2017-08-15 11:32:17.015 INFO 8256 --- [ main] com.study.jms.BasicApplication : Starting BasicApp

我无法使用
Spring Boot
RabbitMQ
服务器发送消息。我看不出有任何例外。不知道发生了什么事

我拥有对
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 !");
    }

}
应用程序属性

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>

org.springframework.boot
spring启动程序父级
1.5.6.1发布
UTF-8
UTF-8
1.8
org.springframework.boot
弹簧启动机amqp
org.springframework.boot
弹簧起动试验
测验

您缺少RabbitMQ
队列
bean定义:

@Bean
public Queue queue() {
    return new Queue("helloworld.q", false);
}
RabbitMQ要求在向队列发布任何消息之前创建队列。在SpringBoot中实现这一点的一种方法是定义
Queue
bean,SpringBoot将处理它的创建。添加时,您将在控制台日志中看到类似的内容:

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组件加载所有
可声明的
bean并创建例如

我刚刚测试了以下简单的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 admin中看到:


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

你能试试看你是否能得到所有声明的队列吗?你遵循了吗?@Darshan-是的,我得到了200 OK的队列响应names@Barath-我已经通过了这个在线示例,我确信如果我遵循它,它会起作用。但我关心的是我的程序出了什么问题。好吧,这是几分钟前发布的
Barath
的同一个链接。我会调查的。但我有两个问题:1)我当前的程序取自
SpringBoot
ActiveMQ
示例之一。成功了!。因此,我修改了原始程序,对其进行了两次更改a)使用
rabbitmplate
而不是
JmsTemplate
,并将其指向
RabbitMQ
服务器,而不是
ActiveMQ
服务器b)我没有创建任何连接工厂或其中的任何内容,它仍然工作。2) 上面列出的程序包含一个消息侦听器。但如果我只是一个出版商呢。我还需要吗?@user2325154谢谢你提供更多详细信息。我已经更新了我的答案,以遵循您的特定用例。在这种情况下,您所要做的就是定义
Queue
bean,该bean将在初始化期间由
RabbitAdmin
组件注册。@user2325154很酷,很高兴我能帮助您:)