RabbitMQ整合Spring Booot【Exchange-Topics模式】

1. “ * ”的使用:

生产者:

package com.toov5.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.toov5.utils.MQConnectionUtils;

//生产者 交换机类型 producerFanout类型
public class TopicProducer {
    //交换机名称
     private static final String EXCHANGE_NAME = "my_topic"; 
     public static void main(String[] args) throws IOException, TimeoutException {
        //建立MQ连接
         Connection connection = MQConnectionUtils.newConnection();
        //创建通道
          Channel channel = connection.createChannel();
          //生产者绑定交换机
          channel.exchangeDeclare(EXCHANGE_NAME, "topic");  //交换机名称  交换机类型
          String routingKey="log.email";    //消息只会给邮件类型的
          //创建对应的消息 
          String msString = "my_Routing_destination_msg"+routingKey;
          //通过频道 发送消息
          System.out.println("生产者投递消息:"+msString);
          channel.basicPublish(EXCHANGE_NAME, routingKey, null, msString.getBytes());
          //关闭通道 和 连接
          channel.close();
          connection.close();
    }
    
} 

消费者:

package com.toov5.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.toov5.utils.MQConnectionUtils;

//邮件消费者
public class ConsumerSMSTopic {
    private static final String SMS_QUEUE ="sms_queue_topic";
    //交换机名称
   private static final String EXCHANGE_NAME = "my_topic"; 
     public static void main(String[] args) throws IOException, TimeoutException {
         System.out.println("短信消费者启动");
        //建立MQ连接
         Connection connection = MQConnectionUtils.newConnection(); 
        //创建通道
          Channel channel = connection.createChannel();
          
        //消费者声明队列
          channel.queueDeclare(SMS_QUEUE, false, false, false, null);
         //消费者队列绑定 路由
         channel.queueBind(SMS_QUEUE, EXCHANGE_NAME, "log.*");
          //消费者监听消息
      DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
              //重写监听方法
             @Override
             public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
                     throws IOException {             
                 String msg = new String(body,"UTF-8");
                 System.out.println("短信消费者获取生产者消息"+msg);
             }
         };
         channel.basicConsume(SMS_QUEUE,true, defaultConsumer);   //绑定队列 事件监听
            
    }
}
package com.toov5.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.toov5.utils.MQConnectionUtils;

//邮件消费者
public class ConsumerEmailTopic {
    private static final String EMAIL_QUEUE ="email_queue_topic";
    //交换机名称
   private static final String EXCHANGE_NAME = "my_topic"; 
     public static void main(String[] args) throws IOException, TimeoutException {
         System.out.println("邮件消费者启动");
        //建立MQ连接
         Connection connection = MQConnectionUtils.newConnection(); 
        //创建通道
          Channel channel = connection.createChannel();
          
        //消费者声明队列
          channel.queueDeclare(EMAIL_QUEUE, false, false, false, null);
         //消费者队列绑定 路由
          channel.queueBind(EMAIL_QUEUE, EXCHANGE_NAME, "log.email");
          //消费者监听消息
      DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
              //重写监听方法
             @Override
             public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
                     throws IOException {             
                 String msg = new String(body,"UTF-8");
                 System.out.println("邮件消费者获取生产者消息"+msg);
             }
         };
         channel.basicConsume(EMAIL_QUEUE,true, defaultConsumer);   //绑定队列 事件监听
            
    }
}

可以看到两个消费者都可以接收到

 

2.换成 “#”

生产者:

package com.toov5.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.toov5.utils.MQConnectionUtils;

//生产者 交换机类型 producerFanout类型
public class TopicProducer {
    //交换机名称
     private static final String EXCHANGE_NAME = "my_topic"; 
     public static void main(String[] args) throws IOException, TimeoutException {
        //建立MQ连接
         Connection connection = MQConnectionUtils.newConnection();
        //创建通道
          Channel channel = connection.createChannel();
          //生产者绑定交换机
          channel.exchangeDeclare(EXCHANGE_NAME, "topic");  //交换机名称  交换机类型
          String routingKey="log.email.sms";    //消息只会给邮件类型的
          //创建对应的消息 
          String msString = "my_Routing_destination_msg"+routingKey;
          //通过频道 发送消息
          System.out.println("生产者投递消息:"+msString);
          channel.basicPublish(EXCHANGE_NAME, routingKey, null, msString.getBytes());
          //关闭通道 和 连接
          channel.close();
          connection.close();
    }
    
} 

消费者:

package com.toov5.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.toov5.utils.MQConnectionUtils;

//邮件消费者
public class ConsumerSMSTopic {
    private static final String SMS_QUEUE ="sms_queue_topic";
    //交换机名称
   private static final String EXCHANGE_NAME = "my_topic"; 
     public static void main(String[] args) throws IOException, TimeoutException {
         System.out.println("短信消费者启动");
        //建立MQ连接
         Connection connection = MQConnectionUtils.newConnection(); 
        //创建通道
          Channel channel = connection.createChannel();
          
        //消费者声明队列
          channel.queueDeclare(SMS_QUEUE, false, false, false, null);
         //消费者队列绑定 路由
         channel.queueBind(SMS_QUEUE, EXCHANGE_NAME, "log.#");
          //消费者监听消息
      DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
              //重写监听方法
             @Override
             public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
                     throws IOException {             
                 String msg = new String(body,"UTF-8");
                 System.out.println("短信消费者获取生产者消息"+msg);
             }
         };
         channel.basicConsume(SMS_QUEUE,true, defaultConsumer);   //绑定队列 事件监听
            
    }
}
package com.toov5.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.toov5.utils.MQConnectionUtils;

//邮件消费者
public class ConsumerEmailTopic {
    private static final String EMAIL_QUEUE ="email_queue_topic";
    //交换机名称
   private static final String EXCHANGE_NAME = "my_topic"; 
     public static void main(String[] args) throws IOException, TimeoutException {
         System.out.println("邮件消费者启动");
        //建立MQ连接
         Connection connection = MQConnectionUtils.newConnection(); 
        //创建通道
          Channel channel = connection.createChannel();
          
        //消费者声明队列
          channel.queueDeclare(EMAIL_QUEUE, false, false, false, null);
         //消费者队列绑定 路由
          channel.queueBind(EMAIL_QUEUE, EXCHANGE_NAME, "log.email");
          //消费者监听消息
      DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
              //重写监听方法
             @Override
             public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
                     throws IOException {             
                 String msg = new String(body,"UTF-8");
                 System.out.println("邮件消费者获取生产者消息"+msg);
             }
         };
         channel.basicConsume(EMAIL_QUEUE,true, defaultConsumer);   //绑定队列 事件监听
            
    }
}

总结 “*” 匹配一个词   “#”匹配多个词

 

posted @ 2018-11-11 23:43  toov5  阅读(714)  评论(0编辑  收藏  举报