fanout 模型(发布/订阅模型)
生产者只能向交换机发送消息,关于交换机,一方面,它接收来自生产者的消息,另一边是将它们推送到队列中。交换机必须确切地知道如何处理它收到的消息。应该把它追加到特定的队列中吗?应该追加到许多队列中吗?或者它应该被丢弃。由交换类型定义的规则。Fanout模型非常的简单,就是交换机把消息广播给所有已知的队列。
临时队列
为什么需要创建临时队列呢?因为我们希望听到所有日志消息,而不仅仅是其中一个子集。也只对当前流动的消息感兴趣, 而不是旧消息。在 Java 客户端中,当我们不向queueDeclare() 提供参数时,我们将会创建一个非持久、独占、自动删除队列,并生成名称。
队列和交换机绑定
channel.queueBind(queueName, "logs", "");
1.开发生产者
最重要的就是声明交换机的类型:channel.exchangeDeclare(“logs”, “fanout”);
package fanout;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import util.RabbitMQUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* Created with IntelliJ IDEA
* Description:
* Author: sudi
* Date: 2020/12/29
* TIME: 10:58
*/
public class Send {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = RabbitMQUtils.getConnection("192.168.1.18", "/ems", 5672, "ems", "123");
Channel channel = connection.createChannel();
//将通道声明指定交换机
channel.exchangeDeclare("logs", "fanout");
//发送消息
channel.basicPublish("logs", "", null, "hello fanout".getBytes());
RabbitMQUtils.closeConnectionAndChannel(channel,connection);
}
}
2.开发消费者1
package fanout;
import com.rabbitmq.client.*;
import util.RabbitMQUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* Created with IntelliJ IDEA
* Description:
* Author: sudi
* Date: 2020/12/29
* TIME: 11:02
*/
public class Accept1 {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = RabbitMQUtils.getConnection("192.168.1.18", "/ems", 5672, "ems", "123");
Channel channel = connection.createChannel();
//通道绑定交换机
channel.exchangeDeclare("logs", "fanout");
//临时队列
String queueName = channel.queueDeclare().getQueue();
//绑定交换机和队列
channel.queueBind(queueName, "logs", "");
//消费消息
channel.basicConsume("", true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("Accept1" + new String(body));
}
});
}
}
3.开发消费者2
package fanout;
import com.rabbitmq.client.*;
import util.RabbitMQUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* Created with IntelliJ IDEA
* Description:
* Author: sudi
* Date: 2020/12/29
* TIME: 11:02
*/
public class Accept2 {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = RabbitMQUtils.getConnection("192.168.1.18", "/ems", 5672, "ems", "123");
Channel channel = connection.createChannel();
//通道绑定交换机
channel.exchangeDeclare("logs", "fanout");
//临时队列
String queueName = channel.queueDeclare().getQueue();
//绑定交换机和队列
channel.queueBind(queueName, "logs", "");
//消费消息
channel.basicConsume("", true, new DefaultConsumer(channel) {
@Override
public voi![在这里插入图片描述](https://img-blog.csdnimg.cn/20210102131243782.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxMTM1MDk3NTY5NA==,size_16,color_FFFFFF,t_70#pic_center)
d handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("Accept2" + new String(body));
}
});
}
}
4.执行效果