使用Fanout Exchange广播一条消息
- 先预备3个队列,创建队列有两种方式,第一种我们可以直接在代码里面创建好队列然后使用,也可以直接在我们的web界面里面去手动创建(但是web界面手动创建的时候要注意将我们的队列绑定到某个Exchange上,不然无法接受到消息)
创建好了队列,直接去Exchange上添加对应bind队列
- 创建一个生产者
package com.example.demo;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author echo
* @date 2021-01-14 14:35
*/
public class FanoutProductTest {
private static final String EXCHANGE_NAME = "exchange_fanout";
private static final String IP_ADDRESS = "192.168.230.131";
private static final int PORT = 5672;
public static void main(String[] args) throws IOException, TimeoutException {
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
// 设置RabbitMQ的链接参数
factory.setHost(IP_ADDRESS);
factory.setPort(PORT);
factory.setUsername("echo");
factory.setPassword("123456");
// 和RabbitMQ建立一个链接
Connection connection = factory.newConnection();
// 创建一个频道
Channel channel = connection.createChannel();
// 创建一个 type="fanout" 、持久化的、非自动删除的交换器
channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true, false, null);
// 如果没有在web界面手动创建队列,可以直接使用如下方式创建几个对应的队列
// channel.queueDeclare("queue_fanout1", false, false, false, null);
// channel.queueDeclare("queue_fanout2", false, false, false, null);
// channel.queueDeclare("queue_fanout3", false, false, false, null);
// 发送一条消息: hello world !
String message = "Hello World !";
channel.basicPublish(EXCHANGE_NAME, "", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
// 关闭资源
channel.close();
connection.close();
}
}
运行之后我们就可以在远程看到我们的消息,三个队列都有了
- 创建一个消费者
package com.example.demo;
import com.rabbitmq.client.*;
import lombok.SneakyThrows;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author echo
* @date 2021-01-14 15:05
*/
public class FanoutConsumerTest {
private static final String EXCHANGE_NAME = "exchange_fanout";
private static final String QUEUE_NAME = "queue_fanout2";
private static final String IP_ADDRESS = "192.168.230.131";
private static final int PORT = 5672;
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
Address[] addresses = new Address[]{new Address(IP_ADDRESS, PORT)};
ConnectionFactory factory = new ConnectionFactory();
// 设置RabbitMQ的链接参数
factory.setUsername("echo");
factory.setPassword("123456");
factory.setPort(PORT);
factory.setHost(IP_ADDRESS);
// 和RabbitMQ建立一个链接
Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();
//声明交换机 Fanout模式
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT, true, false, null);
//进行绑定,指定消费那个队列
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "", null);
Consumer consumer = new DefaultConsumer(channel) {
@SneakyThrows
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
System.out.println("recv message: " + new String(body));
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
channel.basicConsume(QUEUE_NAME, consumer);
//等待回调函数执行完毕之后 关闭资源
TimeUnit.SECONDS.sleep(5);
channel.close();
connection.close();
}
}
运行一下,我们就可以看到我们拿到了生产者对应的消息啦
总结
该类型可以直接广播,我们可以通过这种方式对一些有多个订阅元的需求进行应用