个人理解,就是路由模式的升级版,routingKey更灵活
路由模式可查看博文 [纯原生方式] RabbitMq入门-5 RabbitMq API实现 交换机Exchange Direct路由模式_通过http api创建rabbitmq交换机-CSDN博客
routing_key - 它必须是由点分隔的单词列表,如 book.add,book.select.first等
绑定支持通配符:
- ” *“表示任何一个词
- ”#“表示0或1个词
生产者核心代码
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
channel.basicPublish(exchange,"book.add",null,msg.getBytes());
消费者核心代码,新建多个消息者,同时队列的名称改下,其余的代码一样
channel.queueBind(queue_name, EXCHANGE_NAME,"book.*");
1 开发生产者的代码
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import util.RabbitmqConnection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Send {
private static final String EXCHANGE_NAME ="test_exchange_topic";
public static void main(String[] args) throws IOException, TimeoutException {
//获取一个连接
Connection connection = RabbitmqConnection.getConnection();
//从连接中获取一个通道
Channel channel = connection.createChannel();
//声明交换机(topic 主题模式)
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
//每次只向消费者发送一条消息,消费者使用后,手动确认后,才会发送另外一条
channel.basicQos(1);
for (int i = 0; i <10 ; i++) {
String msg="消息-"+i;
//默认情况下代理服务器端是存在一个""名字的exchange的,
String exchange= EXCHANGE_NAME;
//路由键
String routingKey="book.aa.bb";
AMQP.BasicProperties props=null;
//发送消息
channel.basicPublish(exchange,routingKey,props,msg.getBytes());
}
channel.close();
connection.close();
}
}
2 开发消费者1的代码
import com.rabbitmq.client.*;
import util.RabbitmqConnection;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeoutException;
public class Recv_1 {
private static final String queue_name="queue_topic_email";
private static final String EXCHANGE_NAME ="test_exchange_topic";
public static void main(String[] args) throws IOException, TimeoutException {
// 获取连接
Connection connection = RabbitmqConnection.getConnection();
// 创建通道
final Channel channel = connection.createChannel();
//是否持久化
Boolean durable=false;
//是否排外的
Boolean exclusive=false;
//是否自动删除,当最后一个消费者断开连接之后队列是否自动被删除
Boolean autoDelete=false;
Map<String, Object> arguments=null;
//创建队列声明,主要为了防止消息接收者先运行此程序,队列还不存在时创建队列。
channel.queueDeclare(queue_name,durable,exclusive,autoDelete,arguments);
//声明交换机(topic 主题模式)
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
//将队列绑定到交换机
channel.queueBind(queue_name, EXCHANGE_NAME,"book.*");
channel.basicQos(1);
//定义消费者
DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
//获取到达消息
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(new String(body, "utf-8"));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
//手动确认,
// false表示只确认当前这条消息已收到,
// ture表示在当前这条消息及之前(小于 DelivertTag )的所有未确认的消息都已收到.
channel.basicAck(envelope.getDeliveryTag(),false);
}
}
};
//消费者确认,
// false:表示手动确认,消费者获取消息后,服务器会将该消息标记为不可用状态,等待消费者的反馈,
boolean autoAck=false;
//监听队列
channel.basicConsume(queue_name,autoAck,defaultConsumer);
}
}
3 消费者2的代码
import com.rabbitmq.client.*;
import util.RabbitmqConnection;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeoutException;
public class Recv_2 {
private static final String queue_name="queue_topic_phone";
private static final String EXCHANGE_NAME ="test_exchange_topic";
public static void main(String[] args) throws IOException, TimeoutException {
// 获取连接
Connection connection = RabbitmqConnection.getConnection();
// 创建通道
final Channel channel = connection.createChannel();
//是否持久化
Boolean durable=false;
//是否排外的
Boolean exclusive=false;
//是否自动删除,当最后一个消费者断开连接之后队列是否自动被删除
Boolean autoDelete=false;
Map<String, Object> arguments=null;
//创建队列声明,主要为了防止消息接收者先运行此程序,队列还不存在时创建队列。
channel.queueDeclare(queue_name,durable,exclusive,autoDelete,arguments);
//声明交换机(topic 主题模式)
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
//将队列绑定到交换机
channel.queueBind(queue_name, EXCHANGE_NAME,"book.#");
channel.basicQos(1);
//定义消费者
DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
//获取到达消息
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(new String(body, "utf-8"));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
//手动确认,
// false表示只确认当前这条消息已收到,
// ture表示在当前这条消息及之前(小于 DelivertTag )的所有未确认的消息都已收到.
channel.basicAck(envelope.getDeliveryTag(),false);
}
}
};
//消费者确认,
// false:表示手动确认,消费者获取消息后,服务器会将该消息标记为不可用状态,等待消费者的反馈,
boolean autoAck=false;
//监听队列
channel.basicConsume(queue_name,autoAck,defaultConsumer);
}
}