代码地址:https://github.com/heidaodageshiwo/ZqRabbitMq
发送消息之后都能收到消息:
生产者:
package org.example.Fanout;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import java.io.IOException;
import org.example.RabbitMQUtils;
import org.junit.Test;
/**
* java类简单作用描述
*
* @ProjectName: rabbitmq
* @Package: org.example.Fanout
* @ClassName: Provider2
* @Description: java类作用描述
* @Author: zhangq
* @CreateDate: 2020-12-10 18:33
* @UpdateUser: zhangq
* @UpdateDate: 2020-12-10 18:33
* @UpdateRemark: The modified content
* @Version: 1.0 *
*/
public class Provider2 {
@Test
public void sendmessage() throws IOException {
//获取连接对象
Connection connection = RabbitMQUtils.getConnection();
//获取连接中通道
Channel channel = connection.createChannel();
//参数1 交换机名称 参数2 类型
channel.exchangeDeclare("logs", "fanout");
//发布消息
channel.basicPublish("logs", "", MessageProperties.PERSISTENT_TEXT_PLAIN,
("张强测试fanout:").getBytes());
RabbitMQUtils.closeConnectionFactory(channel, connection);
}
}
消费者1:
package org.example.Fanout;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
import org.example.RabbitMQUtils;
/**
* java类简单作用描述
*
* @ProjectName: rabbitmq
* @Package: org.example.Work
* @ClassName: consum1
* @Description: java类作用描述
* @Author: zhangq
* @CreateDate: 2020-12-10 18:08
* @UpdateUser: zhangq
* @UpdateDate: 2020-12-10 18:08
* @UpdateRemark: The modified content
* @Version: 1.0 *
*/
public class consum11 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
//参数1 交换机名称 参数2 类型
channel.exchangeDeclare("logs", "fanout");
//临时队列名称
String queneName=channel.queueDeclare().getQueue();
//绑定交换机和队列
channel.queueBind(queneName,"logs","");
//autoAck 置位 false
channel.basicConsume(queneName, true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
byte[] body) throws IOException {
System.out.println("消费者-1:" + new String(body));
}
});
}
}
消费者2:
package org.example.Fanout;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
import org.example.RabbitMQUtils;
/**
* java类简单作用描述
*
* @ProjectName: rabbitmq
* @Package: org.example.Work
* @ClassName: consum1
* @Description: java类作用描述
* @Author: zhangq
* @CreateDate: 2020-12-10 18:08
* @UpdateUser: zhangq
* @UpdateDate: 2020-12-10 18:08
* @UpdateRemark: The modified content
* @Version: 1.0 *
*/
public class consum22 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
//参数1 交换机名称 参数2 类型
channel.exchangeDeclare("logs", "fanout");
//临时队列名称
String queneName=channel.queueDeclare().getQueue();
//绑定交换机和队列
channel.queueBind(queneName,"logs","");
//autoAck 置位 false
channel.basicConsume(queneName, true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
byte[] body) throws IOException {
System.out.println("消费者-2:" + new String(body));
}
});
}
}
消费者3:
package org.example.Fanout;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
import org.example.RabbitMQUtils;
/**
* java类简单作用描述
*
* @ProjectName: rabbitmq
* @Package: org.example.Work
* @ClassName: consum1
* @Description: java类作用描述
* @Author: zhangq
* @CreateDate: 2020-12-10 18:08
* @UpdateUser: zhangq
* @UpdateDate: 2020-12-10 18:08
* @UpdateRemark: The modified content
* @Version: 1.0 *
*/
public class consum33 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitMQUtils.getConnection();
Channel channel = connection.createChannel();
//参数1 交换机名称 参数2 类型
channel.exchangeDeclare("logs", "fanout");
//临时队列名称
String queneName=channel.queueDeclare().getQueue();
//绑定交换机和队列
channel.queueBind(queneName,"logs","");
//autoAck 置位 false
channel.basicConsume(queneName, true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
byte[] body) throws IOException {
System.out.println("消费者-3:" + new String(body));
}
});
}
}
先启动消费者,在启动生产者:
都能收到消息