RabbitMQ - Spring配置 - direct《路由模式》

 SpringMain_Direct.java

package com.xzp.rabbitmq.spring.direct;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * main程序
 * 交换机为- 》direct:  代表 "路由模式"  《可选择性接收消息, 指定路由Key》
 */
public class SpringMain_Direct {

    public static void main(final String... args) throws Exception {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/rabbitmq-context-fanout.xml");
        //RabbitMQ模板
        //RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
        RabbitTemplate amqpTemplate_fanout = (RabbitTemplate) ctx.getBean("amqpTemplate_fanout");
        RabbitTemplate amqpTemplate_direct = (RabbitTemplate) ctx.getBean("amqpTemplate_direct");

        //发送消息
        String str01 = "Hello, world!, This  exchange is fanout !!";
        amqpTemplate_fanout.convertAndSend(str01);
        System.out.println();
        System.out.println("发送消息: " + str01);
        System.out.println();

        //发送消息
        String str02 = "Hello, world!, This  exchange is direct !!";
        amqpTemplate_direct.convertAndSend(str02);
        System.out.println();
        System.out.println("发送消息: " + str02);
        System.out.println();

        // 休眠1秒
        Thread.sleep(1000);
        //容器销毁
        ctx.destroy();
    }
}

 User01.java

package com.xzp.rabbitmq.spring.direct;

/**
 * 消费者1
 * 交换机为- 》direct:  代表 "路由模式"  《可选择性接收消息, 指定路由Key》
 */
public class User01 {

    //具体执行业务的方法
    public void listen(String foo) {

        System.out.println();
        System.out.println("direct 消费者 User01: " + foo);
        System.out.println();

    }
}

 User02.java

package com.xzp.rabbitmq.spring.direct;

/**
 * 消费者2
 * 交换机为- 》direct:  代表 "路由模式"  《可选择性接收消息, 指定路由Key》
 */
public class User02 {

    //具体执行业务的方法
    public void listen(String foo) {

        System.out.println();
        System.out.println("direct 消费者 User02: " + foo);
        System.out.println();

    }
}

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

   <!--定义连接工厂-->
   <rabbit:connection-factory id="connectionFactory" host="127.0.0.1" port="5672" username="baseup" password="wukong@123" virtual-host="/baseup"  />

   <!-- MQ的管理,包括队列、交换器等 -->
   <rabbit:admin id="rabbitAdmin_direct" connection-factory="connectionFactory" />

   <!-- 定义Rabbit模板,指定连接工厂以及定义exchange -->
   <rabbit:template id="amqpTemplate_direct" connection-factory="connectionFactory" exchange="exchange_setting_direct" routing-key="wh.public" />



   <!-- 定义队列,自动声明 -->
   <rabbit:queue name="exchange_setting_direct_queue01" auto-declare="true" declared-by="rabbitAdmin_direct" />
   <rabbit:queue name="exchange_setting_direct_queue02" auto-declare="true" declared-by="rabbitAdmin_direct" />


   <!-- 定义交换器,自动声明 -->
   <rabbit:direct-exchange name="exchange_setting_direct" auto-declare="true" declared-by="rabbitAdmin_direct">
      <rabbit:bindings>
         <rabbit:binding queue="exchange_setting_direct_queue01" key="wh.public" />
         <rabbit:binding queue="exchange_setting_direct_queue02" key="wh.public" />
      </rabbit:bindings>
   </rabbit:direct-exchange>

   <!-- 队列监听 -->
   <rabbit:listener-container connection-factory="connectionFactory">
      <rabbit:listener ref="user01" method="listen" queue-names="exchange_setting_direct_queue01" />
      <rabbit:listener ref="user02" method="listen" queue-names="exchange_setting_direct_queue02"  />
   </rabbit:listener-container>


   <!--定义消费Bean-->
   <bean id="user01" class="com.xzp.rabbitmq.spring.direct.User01" />
   <bean id="user02" class="com.xzp.rabbitmq.spring.direct.User02" />

</beans>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值