主题模式
主题模式(Topics):发送的消息以及与交换机与队列绑定的关系都需设置自属的路由键,并且绑定(Binding)的路由键支持通配符,生产者向交换机发送消息之后,交换机会在与之绑定的队列中寻找路由键与消息的路由键能匹配上的队列推送该消息。若没有相匹配的,则该消息丢失。
通配符说明:
*:匹配一个字符
#:匹配一个或多个字符
注意:
1、若队列与交换机的Binding中Routing Key不包含*和#,则表示相等队列推送,类似于直连交换机(Direct Exchange);
2、若队列与交换机的Binding中Routing Key为#或#.#,则表示全部队列推送,类似于扇形交换机(Fanout Exchange)。
注:路由模式( Routing)需将交换机(Exchange)类型定义为"Topic"。
生产者:
package com.producer;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.utils.MQConnectionUtils;
public class ToicpProducer {
//交换机名称
private static final String DESTINATION_NAME="topic_destination";
public static void main(String[] args) throws IOException, TimeoutException {
//1.建立mq连接
Connection connection = MQConnectionUtils.newConnection();
//2.创建通道
Channel channel = connection.createChannel();
//3.生产者绑定交换机 参数1 交换机名称 参数2交换机类型
channel.exchangeDeclare(DESTINATION_NAME,"topic");
//4.创建对应的消息
String msg = "my_fanout_destination_msg,lalla";
// 5. 发送消息 参数1 交换机名称 参数2是 routingkey
channel.basicPublish(DESTINATION_NAME, "sms.if", null, msg.getBytes());
System.out.println("发送消息成功:"+msg);
// 6.关闭通道,连接
channel.close();
connection.close();
}
}
消费者:
package com.topicconsumer;
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.utils.MQConnectionUtils;
public class ConsumerEmailFanout {
private static final String EAMIL_QUEUE = "eamil_topic";
//交换机名称
private static final String DESTINATION_NAME="topic_destination";
public static void main(String[] args) throws IOException, TimeoutException {
//1.建立mq连接
Connection connection = MQConnectionUtils.newConnection();
//2.创建通道
Channel channel = connection.createChannel();
//消费者声明队列
channel.queueDeclare(EAMIL_QUEUE, false, false, false, null);
//4.消费者队列绑定交换机 第一个参数是队列,第二个是交换机名称 第三个是routingkey
channel.queueBind(EAMIL_QUEUE, DESTINATION_NAME, "email.*");
// 5.消费者监听消息
DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body,"GBK");
System.out.println("邮件消费者获取生产消息:"+msg);
}
};
channel.basicConsume(EAMIL_QUEUE,true, defaultConsumer);
}
}
消费者:
package com.topicconsumer;
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.utils.MQConnectionUtils;
public class ConsumerSmslFanout {
private static final String SMS_QUEUE = "sms_routing_fanout";
//交换机名称
private static final String DESTINATION_NAME="topic_destination";
public static void main(String[] args) throws IOException, TimeoutException {
//1.建立mq连接
Connection connection = MQConnectionUtils.newConnection();
//2.创建通道
Channel channel = connection.createChannel();
//消费者声明队列
channel.queueDeclare(SMS_QUEUE, false, false, false, null);
//4.消费者队列绑定交换机
channel.queueBind(SMS_QUEUE, DESTINATION_NAME, "sms.#");
// 5.消费者监听消息
DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body,"GBK");
System.out.println("短信发送的消息:"+msg);
}
};
//设置自动签收
channel.basicConsume(SMS_QUEUE,true, defaultConsumer);
}
}