消息发送端发送消息到交换机,交换机并不存储消息,只是转发消息给队列,当交换机的类型是fanout类型时,消息会被转发到所有与交换机绑定的队列中。
package com.tech.rabbitmq.nospring.pub;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.nio.charset.StandardCharsets;
public class Send {
private final static String EXCHANGE_NAME = "exchange_fanout";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.50.134");
factory.setPort(5672);
factory.setVirtualHost("/dev");
factory.setUsername("tech");
factory.setPassword("tech");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
//绑定交换机
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
String msg = "fanout交换机测试";
channel.basicPublish(EXCHANGE_NAME, "", null, msg.getBytes(StandardCharsets.UTF_8));
System.out.println("广播消息发送成功");
}
}
}
package com.tech.rabbitmq.nospring.pub;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class Recv1 {
private final static String EXCHANGE_NAME = "exchange_fanout";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.50.134");
factory.setPort(5672);
factory.setVirtualHost("/dev");
factory.setUsername("tech");
factory.setPassword("tech");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//绑定交换机
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
String queueName = channel.queueDeclare().getQueue();
//绑定交换机和队列,fanout交换机不需要routingkey
channel.queueBind(queueName, EXCHANGE_NAME, "");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("body=" + new String(body, "utf-8"));
//手动确认消息,不是多条确认
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
//false 关闭自动确认
channel.basicConsume(queueName, false, consumer);
}
}
package com.tech.rabbitmq.nospring.pub;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Recv2 {
private final static String EXCHANGE_NAME = "exchange_fanout";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.50.134");
factory.setPort(5672);
factory.setVirtualHost("/dev");
factory.setUsername("tech");
factory.setPassword("tech");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//绑定交换机
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
String queueName = channel.queueDeclare().getQueue();
//绑定交换机和队列,fanout交换机不需要routingkey
channel.queueBind(queueName, EXCHANGE_NAME, "");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("body=" + new String(body, "utf-8"));
//手动确认消息,不是多条确认
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
//false 关闭自动确认
channel.basicConsume(queueName, false, consumer);
}
}
所有的接收端都能收到消息。



