下面我们要开始学习rabbit的订阅模式,首先先上一个订阅模式的图
上图就是订阅模式的原理图,他与上面的轮询是不一样的。
解读:
1、一个生产者,多个消费者
2、每一个消费者都有自己的队列
3、生产者没有直接把消息发送到队列上,而是发到了交换机 转发器 exchange
4、每个队列都要绑定到交换机上
5、生产者发送的消息,经过交换机 达到队列就能实现一个消息被多个消费者消费。
下面我们直接上代码,下面的代码是我们的生产者,
private static final String EXCHANGE_NAME = "test_exchange_fanout";
public static void main(String[] s) throws Exception{
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
//声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); //分发
//发送消息
String msg = "hello ps";
channel.basicPublish(EXCHANGE_NAME, "", null, msg.getBytes());
System.out.println("Send:"+msg);
channel.close();
connection.close();
}
然后我们运行它后在rabbit管理页面可以看到我们的交换器。
证明我们的交换器声明成功了。
但是队列管理却没有出现队列
那么消息去哪了??丢失了!!因为交换机没有存储的能力,在rabbitmq里面只有队列有存储能力,因为这个时候还没有队列绑定到这个交换机,所以数据丢失了。
接下来我们写入消费者代码,下图是消费者一
public class Recv {
private static final String EXCHANGE_NAME = "test_exchange_fanout";
private static final String QUEUE_NAME = "test_queue_fanout_sms";
public static void main(String[] s) throws Exception{
Connection connection = ConnectionUtil.getConnection();
final Channel channel = connection.createChannel();
//队列声明
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//绑定队列到交换机转发器
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
channel.basicQos(1); //保证一次只分发一个
Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body,"UTF-8");
System.out.println("[Recv msg]:"+msg);
try{
Thread.sleep(2000);
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("[1] done");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
boolean autoAck = false;
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
}
}
小图是消费者二
public class Recv2 {
private static final String EXCHANGE_NAME = "test_exchange_fanout";
private static final String QUEUE_NAME = "test_queue_fanout_email";
public static void main(String[] s) throws Exception{
Connection connection = ConnectionUtil.getConnection();
final Channel channel = connection.createChannel();
//队列声明
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//绑定队列到交换机转发器
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
channel.basicQos(1); //保证一次只分发一个
Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body,"UTF-8");
System.out.println("[Recv2 msg]:"+msg);
try{
Thread.sleep(2000);
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("[2] done");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
boolean autoAck = false;
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
}
}
当我们吧消费者都开启后,会发现rabbitmq控制台中的交换机会绑定了两个队列。
当运行生产者后,两个消费者也接收到了信息。