实现功能:一条消息发送给多个消费者
交换机模式:direct
相比于之前的fanout模式,可以进一步的筛选获取消息的消费者。
fanout模式下,只要消费者监听的队列,已经与接收生产者消息的交换机绑定,那消费者就能收到消息。
direct模式下,消费者监听的队列,不仅需要已经与接收生产者消息的交换机进行绑定,而且绑定交换机时指定的routingKey要匹配生产者发送消息时指定的routingKey(完全匹配)。
生产者:
package com.example.demo.queue.exchangeToQueue.direct;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.example.demo.utils.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
public class Producer {
private static final String EXCHANGE_NAME = "exchange_direct";
public static void main(String[] args) {
Connection connection = null;
Channel channel = null;
try {
// 获取连接
connection = ConnectionUtil.getConnection();
// 创建通道
channel = connection.createChannel();
// 声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
String routingKey = "debug";
// 生产者发送的信息
String msg = "routingKey="+routingKey;
System.out.println("send msg : "+msg);
// 发送信息
channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭通道
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
// 关闭连接
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
消费者1:
package com.example.demo.queue.exchangeToQueue.direct;
import java.io.IOException;
import com.example.demo.utils.ConnectionUtil;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
public class Consumer01 {
private static final String EXCHANGE_NAME = "exchange_direct";
private static final String QUEUE_NAME = "direct_exchange_to_queue_01";
public static void main(String[] args) {
try {
// 获取连接
Connection connection = ConnectionUtil.getConnection();
// 创建通道
final Channel channel = connection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
// 绑定队列到交换机
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body,"UTF-8");
System.out.println("[1]:receive msg:"+msg);
System.out.println("[1]:deal msg successful.");
}
};
// 接收信息
channel.basicConsume(QUEUE_NAME, true, consumer);
} catch (Exception e) {
e.printStackTrace();
}
}
}
消费者2:
package com.example.demo.queue.exchangeToQueue.direct;
import java.io.IOException;
import com.example.demo.utils.ConnectionUtil;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
public class Consumer02 {
private static final String EXCHANGE_NAME = "exchange_direct";
private static final String QUEUE_NAME = "direct_exchange_to_queue_02";
public static void main(String[] args) {
try {
// 获取连接
Connection connection = ConnectionUtil.getConnection();
// 创建通道
final Channel channel = connection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
// 绑定队列到交换机
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body,"UTF-8");
System.out.println("[2]:receive msg:"+msg);
System.out.println("[2]:deal msg successful.");
}
};
// 接收信息
channel.basicConsume(QUEUE_NAME, true, consumer);
} catch (Exception e) {
e.printStackTrace();
}
}
}
消费者3:
package com.example.demo.queue.exchangeToQueue.direct;
import java.io.IOException;
import com.example.demo.utils.ConnectionUtil;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
public class Consumer03 {
private static final String EXCHANGE_NAME = "exchange_direct";
private static final String QUEUE_NAME = "direct_exchange_to_queue_03";
public static void main(String[] args) {
try {
// 获取连接
Connection connection = ConnectionUtil.getConnection();
// 创建通道
final Channel channel = connection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
// 绑定队列到交换机
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "debug");
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body,"UTF-8");
System.out.println("[3]:receive msg:"+msg);
System.out.println("[3]:deal msg successful.");
// 反馈消息处理完毕
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
// 接收信息
channel.basicConsume(QUEUE_NAME, true, consumer);
} catch (Exception e) {
e.printStackTrace();
}
}
}
和之前一样,涉及到了交换机,而在消费者类中中并没有声明交换机,所以需要先执行生产者类的main方法
接下来,我们就可以依次执行三个消费者类的main方法,之后再执行生产者类的main方法。
上述生产者类指定的routingKey是debug,采用的是完全匹配,理论上是只有消费者3能收到消息
执行生产者类的main方法得到的结果如下
消费者1终端:
消费者2终端:
消费者3终端:
结果与理论一致。
来源:oschina
链接:https://my.oschina.net/u/3229807/blog/1860332