前面讲到direct类型的Exchange路由规则是完全匹配binding key与routing key,但这种严格的匹配方式在很多情况下不能满足实际业务需求。topic类型的Exchange在匹配规则上进行了扩展,它与direct类型的Exchage相似,也是将消息路由到binding key与routing key相匹配的Queue中,但这里的匹配规则有些不同,它约定
routing key为一个句点号“. ”分隔的字符串(我们将被句点号“. ”分隔开的每一段独立的字符串称为一个单词如“stock.usd.nyse”、“nyse.vmw”、“quick.orange.rabbit”
binding key与routing key一样也是句点号“. ”分隔的字符串
binding key中可以存在两种特殊字符“”与“#”,用于做模糊匹配,其中“”用于匹配一个单词,“#”用于匹配多个单词(可以是零个)
package com.blj.rabbitmq.topic;
import com.blj.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* topic 生产者
*
* @author BaiLiJun on 2019/10/27 0027
*/
public class Send {
private static final String EXCHANGE_NAME = "test_exchange_topic";
public static void main(String[] args) throws IOException, TimeoutException {
Connection connetion = ConnectionUtils.getConnetion();
Channel channel = connetion.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String msg = "商品。。。";
// String routingKey = "goods.add";
String routingKey = "goods.update";
channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());
System.out.println("Send msg = " + msg);
channel.close();
connetion.close();
}
}
package com.blj.rabbitmq.topic;
import com.blj.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* topic 消费者1
*
* @author BaiLiJun on 2019/10/27 0027
*/
public class Recv1 {
private static final String EXCHANGE_NAME = "test_exchange_topic";
private static final String QUEUE_NAME = "test_queue_topic_1";
public static void main(String[] args) throws IOException, TimeoutException {
Connection connetion = ConnectionUtils.getConnetion();
Channel channel = connetion.createChannel();
//队列声明
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//将队列绑定到交换机
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "goods.add");
//消息只发送一次
channel.basicQos(1);
//声明消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
//消息到达,触发此方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, "utf-8");
System.out.println("[1] Recv msg = " + msg);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[1] done");
//手动反馈
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
//自动应答改为false
Boolean autoAck = false;
//监听队列
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
}
}
package com.blj.rabbitmq.topic;
import com.blj.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* topic 消费者2
*
* @author BaiLiJun on 2019/10/27 0027
*/
public class Recv2 {
private static final String EXCHANGE_NAME = "test_exchange_topic";
private static final String QUEUE_NAME = "test_queue_topic_2";
public static void main(String[] args) throws IOException, TimeoutException {
Connection connetion = ConnectionUtils.getConnetion();
Channel channel = connetion.createChannel();
//队列声明
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//将队列绑定到交换机
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "goods.#");
//消息只发送一次
channel.basicQos(1);
//声明消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
//消息到达,触发此方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, "utf-8");
System.out.println("[2] Recv msg = " + msg);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[2] done");
//手动反馈
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
//自动应答改为false
Boolean autoAck = false;
//监听队列
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
}
}