
package com.xzp.rabbitmq.topics;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.xzp.rabbitmq.util.ConnectionUtil;
/**
* topic
* 交换机指定采用-》通配符路由(模式)
* 该消息先发送到交换机, 再由交换机发送到匹配队里中, 根据匹配法则, 对应则获取到消息, 不对应则获取不到消息。
* 规则如下:
* * 符号代表, 匹配一个消息
* # 符号代表, 匹配一个或多个消息
*
*/
public class Send {
//交换机名称
private final static String EXCHANGE_NAME = "exchange_topic";
public static void main(String[] args) throws Exception {
//获取链接
Connection connection = ConnectionUtil.getConnection();
//创建通道
Channel channel = connection.createChannel();
//声明并绑定交换机
//交换机类型
// fanout: 代表 "订阅模式" 《订阅就接收到消息》
// direct: 代表 "路由模式" 《可选择性接收消息, 指定路由Key》
// topic: 代表 "通配符模式"《可选择性接收消息, 采用通配符模式 * 符号代表1个, # 符号代表一个或多个》
//headers: 代表 ""
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
// 消息内容
String message = "Hello World! 最新消息哦...";
channel.basicPublish(EXCHANGE_NAME, "wh.public.news", null, message.getBytes());
System.out.println("Send '" + message + "'");
channel.close();
connection.close();
}
}
package com.xzp.rabbitmq.topics;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.xzp.rabbitmq.util.ConnectionUtil;
/**
* topic
* 消息接收者1
* 交换机指定采用-》路由通配符(模式)
* 该消息先发送到交换机, 再由交换机发送到匹配队里中, 根据匹配法则, 对应则获取到消息, 不对应则获取不到消息。
* 规则如下:
* * 符号代表, 匹配一个消息
* # 符号代表, 匹配一个或多个消息息
*
*/
public class Recv1 {
//交换机名称
private final static String EXCHANGE_NAME = "exchange_topic";
//队列1名称
private final static String QUEUE_NAME = "exchange_topic_queue_1";
public static void main(String[] argv) throws Exception {
// 获取到连接以及mq通道
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 绑定队列到交换机
//可以接收
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "#");
//可以接收
//channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "#.*");
//不能接收
//channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "*");
//可以接收
//channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "*.*.*");
// 同一时刻服务器只会发一条消息给消费者
channel.basicQos(1);
// 定义队列的消费者
QueueingConsumer consumer = new QueueingConsumer(channel);
// 监听队列,手动返回完成
channel.basicConsume(QUEUE_NAME, false, consumer);
// 获取消息
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println("Topic Recv1 received '" + message + "'");
Thread.sleep(1);
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
}
}
package com.xzp.rabbitmq.topics;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.xzp.rabbitmq.util.ConnectionUtil;
/**
* topic
* 消息接收者2
* 交换机指定采用-》路由通配符(模式)
* 该消息先发送到交换机, 再由交换机发送到匹配队里中, 根据匹配法则, 对应则获取到消息, 不对应则获取不到消息。
* 规则如下:
* * 符号代表, 匹配一个消息
* # 符号代表, 匹配一个或多个消息息
*
*/
public class Recv2 {
//交换机名称
private final static String EXCHANGE_NAME = "exchange_topic";
//队列2名称
private final static String QUEUE_NAME = "exchange_topic_queue_2";
public static void main(String[] argv) throws Exception {
// 获取到连接以及mq通道
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 绑定队列到交换机
//可以接收
//channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "#");
//可以接收
//channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "#.*");
//不能接收
//channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "*");
//可以接收
//channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "*.*.*");
//可以接收
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "wh.public.news");
// 同一时刻服务器只会发一条消息给消费者
channel.basicQos(1);
// 定义队列的消费者
QueueingConsumer consumer = new QueueingConsumer(channel);
// 监听队列,手动返回完成
channel.basicConsume(QUEUE_NAME, false, consumer);
// 获取消息
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println("Topic Recv2 received '" + message + "'");
Thread.sleep(100);
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
}
}
package com.xzp.rabbitmq.util;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
* Rabbit获取链接工具类
*/
public class ConnectionUtil {
public static Connection getConnection() throws Exception {
//定义连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置服务地址
factory.setHost("localhost");
//端口
factory.setPort(5672);
//设置账号信息,用户名、密码、vhost
factory.setVirtualHost("/baseup");
factory.setUsername("baseup");
factory.setPassword("wukong@123");
// 通过工程获取连接
Connection connection = factory.newConnection();
return connection;
}
}