ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-08-24 17:20:42  阅读:269  来源: 互联网

标签:spring-amqp spring spring-boot rabbitmq spring-rabbit


我正在尝试测试& 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"/>

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

public class publishMessage {
    @Autowired
    private RabbitTemplate benchmarkAmqpTemplate;

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

    }
}

当我使用HelloWorld example时,它确实在队列中发布了一条消息,所以想知道我是否做错了什么.
UPDATE
我能够通过在我的上下文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协议.

邮件将使用正确的routingKey发布到Exchange.
发布者(RabbitTemplate)根本不需要知道队列.

队列是接收器的一部分,是队列的订户.

中间还有一个功能 – 绑定.队列在适当的routingKey下绑定到Exchange.一个队列可以绑定到具有不同路由密钥的多个交换机.默认情况下,所有队列都绑定到默认交换(“”),其中routingKeys等于其名称.

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

标签:spring-amqp,spring,spring-boot,rabbitmq,spring-rabbit
来源: https://codeday.me/bug/20190824/1710354.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有