Spring amqp不向队列发布消息,而是向Exchange发布消息

Sum*_*itk 3 spring rabbitmq spring-rabbit spring-amqp spring-boot

我正在尝试使用多个队列测试和测试RabbitMQ的spring-amqp,因此我为每个队列创建了兔子模板并使用它来发送消息.发送的消息成功,我可以在交换机中看到一条消息,但我在队列中看不到任何内容.我猜这是非常小的设置,但无法弄明白.

这是我的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"/>
Run Code Online (Sandbox Code Playgroud)

这是我使用benchmarkAmqpTemplate发布到队列的代码.

public class publishMessage {
    @Autowired
    private RabbitTemplate benchmarkAmqpTemplate;

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

    }
}
Run Code Online (Sandbox Code Playgroud)

当我使用HelloWorld 示例时,它确实在队列中发布了一条消息,所以想知道我是否做错了什么. 更新 我能够通过direct-exchange在我的上下文xml中添加标记来解决这个问题.我的完整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>
Run Code Online (Sandbox Code Playgroud)

Art*_*lan 7

对不起,但看起来你误解了AMQP协议.

消息Exchange将以适当的方式发布routingKey.publisher(RabbitTemplate)根本不需要知道队列.

queue是接收器的一部分,是接收器的用户queue.

中间还有一个功能 - binding.这queue是必然ExchangeroutingKey.一个队列可以绑定到具有不同路由密钥的多个交换机.默认情况下,所有队列都绑定到默认的exchange(""),其中routingKeys等于它们的names.

请参阅RabbitMQ网站了解更多信息.

  • 要在Broker上声明它们,你应该"AmqpAdmin":http://docs.spring.io/spring-amqp/docs/latest-ga/reference/html/amqp.html#broker-configuration (2认同)