簡體   English   中英

無法使用Spring將消息發送到RabbitMQ

[英]Not able to send messages to RabbitMQ using Spring

我正在嘗試使用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>

beans.xml

<?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>

Main.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());
    }

}

HelloWorld.java

public class HelloWorld {

    private String message;

    public String getMessage() {
        return message;
    }

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

}

您需要告訴RabbitMQ如何路由消息。

由於您不是在創建交換或將隊列綁定到交換,因此可以使用隊列名稱路由到默認交換( "" )。

您需要將模板上的routingKey屬性( routing-key屬性)設置為隊列名稱,或者需要將其包括在convertAndSend調用中:

template.convertAndSend("", "helloQueue", helloWorld.getMessage());

RabbitMQ只是丟棄已成功傳遞到交換但未路由到任何隊列的消息。

請參閱RabbitMQ教程以了解交換,路由鍵等。

Spring AMQP示例存儲庫具有教程的Spring Boot版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM