Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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_Jms_Rabbitmq_Spring Jms - Fatal编程技术网

Java 无法使用Spring向RabbitMQ发送消息

Java 无法使用Spring向RabbitMQ发送消息,java,spring,jms,rabbitmq,spring-jms,Java,Spring,Jms,Rabbitmq,Spring Jms,我正试图使用Spring 4向安装在我的localhost上的RabbitMQ发送消息,但由于某种原因,消息没有被发送,我也没有收到任何错误。在我看来,我的Spring配置(beans.xml)不正确 请导游 pom.xml <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-c

我正试图使用
Spring 4
向安装在我的
localhost
上的
RabbitMQ
发送消息,但由于某种原因,消息没有被发送,我也没有收到任何错误。在我看来,我的Spring配置(
beans.xml
)不正确

请导游

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>4.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.2.RELEASE</version>
    </dependency>
    <!-- RabbitMQ -->
    <dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-rabbit</artifactId>
        <version>1.5.5.RELEASE</version>
    </dependency>
</dependencies>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:flow="http://www.springframework.org/schema/webflow-config"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
          http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
          http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.5.xsd">

    <bean id="helloWorld" class="com.study.jms.HelloWorld">
        <property name="message" value="Hello World (Spring-RabbitMQ)!" />
    </bean>

    <rabbit:connection-factory id="rabbitConnectionFactory" host="localhost" username="guest" password="guest" port="5672"/>
    <rabbit:template id="amqpTemplate" connection-factory="rabbitConnectionFactory"/>
    <rabbit:admin connection-factory="rabbitConnectionFactory"/>
    <rabbit:queue name="helloQueue"/>

</beans>
HelloWorld.java

public class Main {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");

        //send message
        System.out.println("Sending message....");
        AmqpTemplate template = context.getBean(AmqpTemplate.class);
        template.convertAndSend(helloWorld.getMessage());
    }

}
public class HelloWorld {

    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

您需要告诉RabbitMQ如何路由消息

由于您没有创建exchange或将队列绑定到exchange,因此可以使用队列名称路由到默认exchange(
“”

您需要将模板上的
routingKey
属性(
routing key
属性)设置为队列名称,或者需要将其包含在
convertAndSend
调用中:

template.convertAndSend("", "helloQueue", helloWorld.getMessage());
RabbitMQ只是删除成功传递到exchange但未路由到任何队列的消息

请参阅以了解交换、路由密钥等


有Spring引导版本的教程。

您需要告诉RabbitMQ如何路由消息

由于您没有创建exchange或将队列绑定到exchange,因此可以使用队列名称路由到默认exchange(
“”

您需要将模板上的
routingKey
属性(
routing key
属性)设置为队列名称,或者需要将其包含在
convertAndSend
调用中:

template.convertAndSend("", "helloQueue", helloWorld.getMessage());
RabbitMQ只是删除成功传递到exchange但未路由到任何队列的消息

请参阅以了解交换、路由密钥等


有Spring Boot版本的教程。

有日志记录吗?有日志记录吗?