看一幅图, 很清楚的讲述了topic
package top_exchange;
import com.rabbitmq.client.*;
public class ConsumerTopicExchange {
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
connectionFactory.setVirtualHost("/");
// 2通过连接工厂创建连接
Connection connection = connectionFactory.newConnection();
// 3通过connection 创建一个Channel
Channel channel = connection.createChannel();
// 4 声明
String exchangeName = "test_topic_exchange";
String exchangeType = "topic";
String queueName = "test_topic_queue";
// * 只匹配一个单词, user.name
// String routingKey = "user.*";
// # 匹配多个单词, user.name.address ...
String routingKey = "user.#";
// 1. 声明交换机
channel.exchangeDeclare(exchangeName, exchangeType, true, false, null);
// 2,声名一个队列
channel.queueDeclare(queueName, false, false, false, null);
// 3、建立绑定关系
channel.queueBind(queueName,exchangeName, routingKey);
// 5.消息是否持久化
QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
// 6 设置channel
channel.basicConsume(queueName, true, queueingConsumer);
while (true) {
//nextDelivery() 一个有参数,一个无参数 , 无参数会一直阻塞
// 7 获取消息
QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
String msg = new String(delivery.getBody());
System.out.println("消费端: " + msg);
Envelope envelope = delivery.getEnvelope();
}
}
}
生产者
package top_exchange;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class ProcuderTopicExchange {
public static void main(String[] args) throws IOException, TimeoutException {
// 创建一个ConnectionFactory 工厂, 并进行配置
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("localhost");
connectionFactory.setPort(5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
connectionFactory.setVirtualHost("/");
Connection connection = null;
Channel channel = null;
try {
// 2通过连接工厂创建连接
connection = connectionFactory.newConnection();
// 3通过connection 创建一个Channel
channel = connection.createChannel();
//4、声名
String exchangeName = "test_topic_exchange";
String routingKey1 = "user.save";
String routingKey2 = "user.update";
String routingKey3 = "user.delete.abc";
// 5 、发送
String msg = "hello World RabbitMq 4 Topic Exchange Message ...";
channel.basicPublish(exchangeName,routingKey1, null, msg.getBytes());
channel.basicPublish(exchangeName,routingKey2, null, msg.getBytes());
channel.basicPublish(exchangeName,routingKey3, null, msg.getBytes());
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} finally {
// 记得关闭相关连接
if (channel != null) {
channel.close();
}
if (connection != null) {
connection.close();
}
}
}
}
如果有之前设置的路由为 #, 之后测试,* , 会发现,还可以接受三条信息,是因为之前的# 号绑定的,没有解绑,解绑一下就好了, 通过图形化管理界面也可以;
channel.queueUnbind(queueName, exchangeName, routingKey);