fanout 广播:
生产者生产消息,并发送到交换机,由交换机进行消息发布,消费者订阅消息并生产临时消息队列来消费
rabbitMQ 连接工具
package Utils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class RabbitMqUtil {
//创建连接MQ的工厂对象
private static ConnectionFactory connectionFactory;
//静态代码块
static{
connectionFactory=new ConnectionFactory();
connectionFactory.setHost("192.168.56.10");
connectionFactory.setPort(5672);
//设置连接哪个虚拟主机
connectionFactory.setVirtualHost("/ems");
//设置访问虚拟主机的用户名和密码
connectionFactory.setUsername("ems");
connectionFactory.setPassword("123");
}
//启动连接
public static Connection getConnection() throws IOException, TimeoutException {
return connectionFactory.newConnection();
}
//关闭连接
public static void closeConnectionAndChanel(Channel channel,Connection connection) throws IOException, TimeoutException {
if(channel==null){
channel.close();
connection.close();
}
}
}
生产者:
package rabbitMq.fanout;
import Utils.RabbitMqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
//生产者
public class TestRabbitMqP {
public static void main(String[] args) throws IOException, TimeoutException {
//传递的消息
String mes="HELLO WORD !!";
//通过工具获取连接对象
Connection connection = RabbitMqUtil.getConnection();
//获取连接中通道
Channel channel = connection.createChannel();
/**
* 将通道声明指定交换机
* 参数:
* 1、交换机名称
* 2、交换机类型:fanout 广播类型
*/
channel.exchangeDeclare("logs","fanout");
/**
* 消息发布
* 参数:
* 1、交换机名称
* 2、队列名称
* 3、传递消息的额外设置
* 4、消息内容
*/
channel.basicPublish("logs","",null,"fanout type message".getBytes());
//关闭连接, 释放资源
RabbitMqUtil.closeConnectionAndChanel(channel,connection);
}
}
消费者1:
package rabbitMq.fanout;
import Utils.RabbitMqUtil;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
//消费者
public class TestRabbitMqC_1 {
public static void main(String[] args) throws IOException, TimeoutException {
//通过工具获取连接对象
Connection connection = RabbitMqUtil.getConnection();
//获取连接中通道
Channel channel = connection.createChannel();
//每次只接受一条未确认的消息
channel.basicQos(1);
/**
* 通道绑定交换机
* 参数:
* 1、交换机名称
* 2、交换机类型
*/
channel.exchangeDeclare("logs","fanout");
//临时队列
String queueName = channel.queueDeclare().getQueue();
//绑定交换机和队列
channel.queueBind(queueName,"logs","");
/**
* 消息订阅
* 参数:
* 1、需要消费的队列名称
* 2、是否开启消息自动队列
* 3、消费的回调接口
*/
channel.basicConsume(queueName,false,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("==========================="+new String(body));
//手动确认消息
channel.basicAck(envelope.getDeliveryTag(),true);
}
});
}
}
消费者2:
package rabbitMq.fanout;
import Utils.RabbitMqUtil;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
//消费者
public class TestRabbitMqC_2 {
public static void main(String[] args) throws IOException, TimeoutException {
//通过工具获取连接对象
Connection connection = RabbitMqUtil.getConnection();
//获取连接中通道
Channel channel = connection.createChannel();
//每次只接受一条未确认的消息
channel.basicQos(1);
/**
* 通道绑定交换机
* 参数:
* 1、交换机名称
* 2、交换机类型
*/
channel.exchangeDeclare("logs","fanout");
//临时队列
String queueName = channel.queueDeclare().getQueue();
//绑定交换机和队列
channel.queueBind(queueName,"logs","");
/**
* 消息订阅
* 参数:
* 1、需要消费的队列名称
* 2、是否开启消息自动队列
* 3、消费的回调接口
*/
channel.basicConsume(queueName,false,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("==========================="+new String(body));
//手动确认消息
channel.basicAck(envelope.getDeliveryTag(),true);
}
});
}
}
消费者3:
package rabbitMq.fanout;
import Utils.RabbitMqUtil;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
//消费者
public class TestRabbitMqC_3 {
public static void main(String[] args) throws IOException, TimeoutException {
//通过工具获取连接对象
Connection connection = RabbitMqUtil.getConnection();
//获取连接中通道
Channel channel = connection.createChannel();
//每次只接受一条未确认的消息
channel.basicQos(1);
/**
* 通道绑定交换机
* 参数:
* 1、交换机名称
* 2、交换机类型
*/
channel.exchangeDeclare("logs","fanout");
//临时队列
String queueName = channel.queueDeclare().getQueue();
//绑定交换机和队列
channel.queueBind(queueName,"logs","");
/**
* 消息订阅
* 参数:
* 1、需要消费的队列名称
* 2、是否开启消息自动队列
* 3、消费的回调接口
*/
channel.basicConsume(queueName,false,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("==========================="+new String(body));
//手动确认消息
channel.basicAck(envelope.getDeliveryTag(),true);
}
});
}
}
当生产者运行之后,三个消费者都能监听到消息发布