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 



   
   

   
   

   
   



   
   
   


   
   
      
         
         
      
   

   
   
      
      
   


   
   
   

 

 

你可能感兴趣的:(RabbitMQ)