之前我們已經學習了2種交換機類型了,今天我們再來學習一下主題交換機類型,主題交換機類型的核心思想就是可以通過正則表達式的方式,將queue 和 exchange綁定。我們直接代碼演示:
package com.lwl.rabbitmq.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.lwl.rabbitmq.constant.Constants;
/**
* 發送消息 配置發送消息的隊列queue
* @author lwl
* @create 2018年8月10日 下午2:37:38
* @version 1.0
*/
@Configuration
public class SendMessageConfig {
@Bean
public Queue topicQueue() {
return new Queue(Constants.TOPIC_QUEUE);
}
@Bean
public Queue topicQueue2() {
return new Queue(Constants.TOPIC_QUEUE_TWO);
}
@Bean
TopicExchange exchange() {
return new TopicExchange(Constants.TOPIC_NAME);
}
/**
* 使用主題交換機,
* 將隊列Constants.TOPIC_QUEUE與exchange綁定,binding_key爲topic.queue.key,就是完全匹配
* @param topicQueue
* @param exchange
* @return
* @author lwl
* @create 2019年6月14日 上午10:51:21
*/
@Bean
Binding bindingExchangeMessage(Queue topicQueue, TopicExchange exchange) {
return BindingBuilder.bind(topicQueue).to(exchange).with(Constants.ROUTING_KEY);
}
/**
* 使用主題交換機,routekey
* 將隊列Constants.TOPIC_QUEUE_TWO與exchange綁定,binding_key爲topic.*.#,模糊匹配
* @param topicQueue2
* @param exchange
* @return
* @author lwl
* @create 2019年6月14日 上午10:51:21
*/
@Bean
Binding bindingExchangeMessage2(Queue topicQueue2, TopicExchange exchange) {
return BindingBuilder.bind(topicQueue2).to(exchange).with(Constants.ROUTING_KEY_TWO);
}
}
這裏我們有2個隊列名稱,一個是通過完全匹配的方式,綁到exchange上,一個是通過模糊 也就是正則表達式方式綁到exchange上。
看一下生產者:
package com.lwl.rabbitmq.producer;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.lwl.rabbitmq.constant.Constants;
/**
* 生成者
* @author lwl
* @create 2019年6月14日 上午10:56:41
* @version 1.0
*/
@Component
public class Producer {
@Autowired
private AmqpTemplate template;
/**
* 使用主題交換機
* @param message
* @author lwl
* @create 2019年6月14日 上午10:54:54
*/
public void send(Object message){
template.convertAndSend(Constants.TOPIC_NAME,Constants.ROUTING_KEY,message);
}
/**
* 如果routingKey 推送到對應的queue中
* @param routingKey
* @param message
* @author lwl
* @create 2019年6月14日 下午2:56:39
*/
public void send(String routingKey,Object message){
template.convertAndSend(Constants.TOPIC_NAME,routingKey,message);
}
}
第一個推送的方法,使用的routingKey是完全匹配模式,第二個可以是自定義模式
看一下消費者:
package com.lwl.rabbitmq.consumer;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import com.lwl.rabbitmq.constant.Constants;
/**
* 消費者 使用模糊匹配模式
* @author lwl
* @create 2019年6月14日 上午10:57:11
* @version 1.0
*/
@Component
@RabbitListener(queues = Constants.TOPIC_QUEUE_TWO)
public class TopicConsumer {
@RabbitHandler
public void process(String hello) {
System.out.println();
System.out.println("-----------------------客戶端 1 收到數據 -----------------------");
System.out.println(Constants.TOPIC_QUEUE_TWO+ " --> Receiver1 : " + hello);
System.out.println();
}
}
package com.lwl.rabbitmq.consumer;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import com.lwl.rabbitmq.constant.Constants;
/**
* 消費者 使用完全匹配模式
* @author lwl
* @create 2019年6月14日 上午10:57:11
* @version 1.0
*/
@Component
@RabbitListener(queues = Constants.TOPIC_QUEUE)
public class TopicConsumer3 {
@RabbitHandler
public void process(String hello) {
System.out.println();
System.out.println("-----------------------客戶端 3 收到數據 -----------------------");
System.out.println(Constants.TOPIC_QUEUE+ " --> Receiver3 : " + hello);
System.out.println();
}
}
接下來我們看一下測試類:
@Test
public void send() {
String message = "topic send message ";
producer.send(message);
}
結果分析:
-----------------------客戶端 3 收到數據 -----------------------
topic_queue --> Receiver3 : topic send message
-----------------------客戶端 1 收到數據 -----------------------
topic_queue_2 --> Receiver1 : topic send message
由於調用的方法的路由routingkey是:topic.queue.key,那麼綁定主題交換機對應的queue有2個,
一個是topic_queue (完全匹配),另一個是topic_queue_2(他的路由key是:topic.*.#)模糊匹配上了
測試方法2:
@Test
public void send2() {
String message = "topic send message 2222222222";
String routingKey = "topic.queue.two";
producer.send(routingKey , message);
}
測試結果分析:
-----------------------客戶端 1 收到數據 -----------------------
topic_queue_2 --> Receiver1 : topic send message 2222222222
由於發送的路由key是:topic.queue.two,不能匹配到topic.queue.key,所以隊列topic_queue收不到
而topic_queue_2(他的路由key是:topic.*.#)模糊匹配上了