1.模型

2.创建生产者
package com.dwz.rabbitmq.exchange.fanout;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.dwz.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
public class Producer {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
String exchangeName = "test_fanout_exchange";
String msg = "hello rabbitmq fanout message successs!--";
for(int i = 0; i < 50; i++) {
channel.basicPublish(exchangeName, "", null, (msg + i).getBytes());
}
channel.close();
connection.close();
}
}
3.创建消费者1
package com.dwz.rabbitmq.exchange.fanout;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.dwz.rabbitmq.util.ConnectionUtils;
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;
public class Consumer {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
//交换机名称
String exchangeName = "test_fanout_exchange";
//交换机类型
String exchangeType = "fanout";
String queueName = "test_fanout_queue_1";
//路由键
String routingKey = "";
//交换机声明
channel.exchangeDeclare(exchangeName, exchangeType, true, false, null);
/*
* durable:持久化,服务器重启,数据仍然存在
* exclusive:独占一个channel,用于顺序消费,类似于加了一把锁
* autoDelete:如果一个队列脱离了exchange会自动删除
*/
//队列声明
channel.queueDeclare(queueName, false, false, false, null);
//队列绑定交换机
channel.queueBind(queueName, exchangeName, routingKey);
//定义消费者
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("rec1--message:" + msg);
}
};
//设置消费者
channel.basicConsume(queueName, true, consumer);
}
}
4.创建消费者2
package com.dwz.rabbitmq.exchange.fanout;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.dwz.rabbitmq.util.ConnectionUtils;
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;
public class Consumer2 {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
String exchangeName = "test_fanout_exchange";
String exchangeType = "fanout";
String queueName = "test_fanout_queue_2";
String routingKey = "";
channel.exchangeDeclare(exchangeName, exchangeType, true, false, null);
/*
* durable:持久化,服务器重启,数据仍然存在
* exclusive:独占一个channel,用于顺序消费,类似于加了一把锁
* autoDelete:如果一个队列脱离了exchange会自动删除
*/
channel.queueDeclare(queueName, false, false, false, null);
channel.queueBind(queueName, exchangeName, routingKey);
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("rec2--message:" + msg);
}
};
channel.basicConsume(queueName, true, consumer);
}
}
5.运行代码
先启动两个消费者,再启动生产者,运行结果:
两个消费者都拿到exchange(交换机)的全部信息
注:由于exchange只是做一个消息转发,本身是不具备存储数据的能力,如果先启动生产者,信息是到达不了队列的,队列有存储数据的功能