简介
本节主要演示使用直连接类型,将多个路由键绑定到同一个队列上。
生产者
@Test
public void produce() throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(AMQP.PROTOCOL.PORT);
factory.setUsername("guest");
factory.setPassword("guest");
Connection conn = factory.newConnection();
Channel channel = conn.createChannel();
// 交换机名
String EXCHANGE_NAME = "exchange.direct.routing";
// 路由键名数组
String[] routings = {"debug", "info", "warning", "error"};
// 循环将一个交换机绑定多个路由键
for (int i = 0; i < 20; i++) {
int random = new Random().nextInt(4);
String routingKey = routings[random];
String message = "Hello RabbitMQ---" + routingKey + "-----" + i;
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
}
channel.close();
conn.close();
}
消费者1
@Test
public void consume1() throws IOException, TimeoutException, InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(AMQP.PROTOCOL.PORT);
factory.setUsername("guest");
factory.setPassword("guest");
Connection conn = factory.newConnection();
Channel channel = conn.createChannel();
// 交换机名
String EXCHANGE_NAME = "exchange.direct.routing";
// 声明一个交换机,使用直连类型
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
// 生成一个随机的队列名
String QUEUE_NAME = channel.queueDeclare().getQueue();
// 将一个队列绑定多个路由键
String[] routingKeys = {"debug", "info"};
for (int i = 0; i < routingKeys.length; i++) {
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKeys[i]);
}
System.out.println("Consumer1 Wating Receive Message");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
System.out.println("[C] received " + message + ",处理业务中...");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
Thread.sleep(1000000);
}
消费者2
@Test
public void consume2() throws IOException, TimeoutException, InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(AMQP.PROTOCOL.PORT);
factory.setUsername("guest");
factory.setPassword("guest");
Connection conn = factory.newConnection();
Channel channel = conn.createChannel();
// 交换机名
String EXCHANGE_NAME = "exchange.direct.routing";
// 声明一个交换机,使用直连类型
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
// 生成一个随机的队列名
String QUEUE_NAME = channel.queueDeclare().getQueue();
// 将一个队列绑定多个路由键
String[] routingKeys = {"debug", "info"};
for (int i = 0; i < routingKeys.length; i++) {
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKeys[i]);
}
System.out.println("Consumer2 Wating Receive Message");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
System.out.println("[C] received " + message + ",处理业务中...");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
Thread.sleep(1000000);
}