通过RabbitMQ Management进行动态绑定交换机和队列

记一记:

在RabbitMQ Management中的交换机可以进行手动绑定,方便,记一记:

两个spring项目:

项目1:spring配置文件:(只要连接,模板,交换机)

<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/rabbit
           http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <rabbit:connection-factory id="connectionFactory" host="localhost" port="5672" virtual-host="/ywj" username="ywj" password="0"/>

    <rabbit:admin connection-factory="connectionFactory" />

    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"  exchange="topicExchange" />

    <rabbit:topic-exchange name="topicExchange" durable="true" auto-declare="true"/>

</beans>
来一个循环发送信息功能:
@Service
public class I implements InitializingBean {

    @Autowired
    private RabbitTemplate template;

    public void afterPropertiesSet() throws Exception {

        while (true){
            System.out.println("发送消息");
            template.convertAndSend("消息:"+System.currentTimeMillis());
            Thread.sleep(60000);
        }
    }
}

项目2:spring配置文件:(只要连接,队列 ,监听)

<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/rabbit
           http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <rabbit:connection-factory id="connectionFactory" host="localhost" port="5672" virtual-host="/ywj" username="ywj" password="0"/>

    <rabbit:admin connection-factory="connectionFactory" />

    <rabbit:queue name="myQueue123" auto-declare="true" durable="true"/>

    <rabbit:listener-container connection-factory="connectionFactory">
        <rabbit:listener ref="i" method="listen" queue-names="myQueue123"/>
    </rabbit:listener-container>

    <bean id="i" class="cn.cn.I"/>
</beans>

接收者:

package cn.cn;

public class I {
    public void listen(String msg){
        System.out.println("收到消息:"+msg);
    }
}

准备Ok,分别启动两个项目,接着打开RabbitMQ Management,找到项目1的交换机 【topicExchange】,


点击名称【topicExchange】后会进入新的页面,如下图:


然后在【To queue】输入项目2中的队列名【myQueue123】,【Routing key】输入相关的topic规则,最后点击下面的【Bind】, OK。当项目1发送消息时,项目2就能收到。(当然queue和routin key要符合才行),结束笔记。

扫描二维码关注公众号,回复: 3213701 查看本文章

猜你喜欢

转载自blog.csdn.net/u013845177/article/details/79748847
今日推荐