Spring amqp 不将消息发布到队列而是发布到 Exchange

Spring amqp not publishing message to the queue but to Exchange

我正在尝试为具有多个队列的 RabbitMQ 测试和基准测试 spring-amqp,因此我为每个队列创建了 rabbit 模板并使用它来发送消息。消息发送成功,我可以看到在交换中发布了一条消息,但我在队列中没有看到任何消息。我猜这是非常小的设置,但无法弄清楚。

这是我的applicationContext.xml

<bean id="banchmarkConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
        <constructor-arg ref="benchmarkAmqpHost"/>
        <property name="username" ref="benchmarkAmqpUser"/>
        <property name="password" ref="benchmarkAmqpPass"/>
        <property name="virtualHost" ref="benchmarkAmqpVHost"/>
        <property name="channelCacheSize" value="10"/>
</bean>
<rabbit:template id="benchmarkAmqpTemplate"
        connection-factory="banchmarkConnectionFactory"
        exchange="my_exchange"
        queue="BenchmarkQueue"
        routing-key="BenchmarkQueue" />
<rabbit:admin connection-factory="banchmarkConnectionFactory"/>
<rabbit:queue name="BenchmarkQueue" auto-delete="true" durable="false" auto-declare="true"/>

这是我的代码,它使用 benchmarkAmqpTemplate 发布到队列。

public class publishMessage {
    @Autowired
    private RabbitTemplate benchmarkAmqpTemplate;

    protected void publish(String payload) {
        benchmarkAmqpTemplate.setQueue("BenchmarkQueue");
        benchmarkAmqpTemplate.convertAndSend("my_exchange", "BenchmarkQueue", payload);

    }
}

当我使用 HelloWorld example 时,它确实在队列中发布了一条消息,所以想知道我是否做错了什么。 更新 我能够通过在我的上下文 xml 中添加 direct-exchange 标签来解决这个问题。我的完整 xml 看起来像这样:

    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

<bean id="banchmarkConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
            <constructor-arg ref="benchmarkAmqpHost"/>
            <property name="username" ref="benchmarkAmqpUser"/>
            <property name="password" ref="benchmarkAmqpPass"/>
            <property name="virtualHost" ref="benchmarkAmqpVHost"/>
            <property name="channelCacheSize" value="10"/>
    </bean>
    <rabbit:template id="benchmarkAmqpTemplate"
            connection-factory="banchmarkConnectionFactory"
            exchange="my_exchange"
            queue="BenchmarkQueue"
            routing-key="BenchmarkQueue" />
    <rabbit:admin connection-factory="banchmarkConnectionFactory"/>
    <rabbit:queue name="BenchmarkQueue" auto-delete="true" durable="false" auto-declare="true"/>
    <rabbit:direct-exchange name="my_exchange">
        <rabbit:bindings>
            <rabbit:binding queue="BenchmarkQueue" key="BenchmarkQueue" />
        </rabbit:bindings>
    </rabbit:direct-exchange>
</beans>

抱歉,您似乎对 AMQP 协议有点误解。

消息发布到 Exchange 并带有正确的 routingKey。 发布者 (RabbitTemplate) 根本不需要了解队列。

queue 是接收者的一部分,queue 的订阅者。

中间还有一项功能 - bindingqueue 绑定到适当 routingKey 下的 Exchange。一个队列可以绑定到多个具有不同路由键的交换机。默认情况下,所有队列都绑定到默认交换器 (""),routingKeys 等于它们的 names。

请参阅 RabbitMQ 站点以获取更多信息。