1、工具类
package utils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
* Created by zjl
* 2020/10/23
**/
public class RabbitMQUtils {
public static ConnectionFactory connectionFactory;
static {
connectionFactory = new ConnectionFactory();
//设置连接rabbitmq主机
connectionFactory.setHost("127.0.0.1");
//设置连接rabbitmq端口
connectionFactory.setPort(5672);
//设置连接哪个虚拟主机
connectionFactory.setVirtualHost("/ems");
//设置访问虚拟主机的用户名
connectionFactory.setUsername("guest");
//设置访问虚拟主机的密码
connectionFactory.setPassword("guest");
}
public static Connection getConnection(){//创建链接mq的连接工厂对象
try{
//重量级资源,没必要每拿一次连接创建一次,这样的话代价有点大,所以我们在类加载的时候创建出来,只创建一次
// ConnectionFactory connectionFactory = new ConnectionFactory();
//设置连接rabbitmq主机
// connectionFactory.setHost("127.0.0.1");
// //设置连接rabbitmq端口
// connectionFactory.setPort(5672);
// //设置连接哪个虚拟主机
// connectionFactory.setVirtualHost("/ems");
// //设置访问虚拟主机的用户名
// connectionFactory.setUsername("guest");
// //设置访问虚拟主机的密码
// connectionFactory.setPassword("guest");
//获取连接对象
return connectionFactory.newConnection();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
//关闭通道和关闭连接的工具方法
public static void closeConnectionAndChanel(Channel channel,Connection connection){//创建链接mq的连接工厂对象
try{
if(channel!=null) channel.close();
if(connection!=null) connection.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
2、生产者
package fanout;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import utils.RabbitMQUtils;
import java.io.IOException;
/**
* Created by zjl
* 2020/10/23
**/
public class Provider {
public static void main(String[] args) throws IOException {
//通过工具类获取连接
Connection connection = RabbitMQUtils.getConnection();
//获取连接中通道
Channel channel = connection.createChannel();
//将通道声明指定交换机 //参数1:交换机名称 参数2:交换机类型 fanout:广播类型
channel.exchangeDeclare("logs","fanout");
//发布消息
channel.basicPublish("logs","", null,"fanout type message".getBytes());
//通过工具类来关闭连接
RabbitMQUtils.closeConnectionAndChanel(channel,connection);
}
}
3、消费者1
package fanout;
import com.rabbitmq.client.*;
import utils.RabbitMQUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* Created by zjl
* 2020/10/23
**/
public class Customer1 {
public static void main(String[] args) throws IOException, TimeoutException {
//通过工具类获取连接
Connection connection = RabbitMQUtils.getConnection();
//获取连接中通道
Channel channel = connection.createChannel();
//通道绑定交换机
channel.exchangeDeclare("logs","fanout");
//临时队列
String queueName = channel.queueDeclare().getQueue();
//绑定交换机和队列
channel.queueBind(queueName,"logs","");
//发布消息
//参数1:消费哪个队列的消息 队列名称 参数2:开始消息的自动确认机制 参数3:消费时的回调接口
channel.basicConsume(queueName,true,new DefaultConsumer(channel){
@Override //最后一个参数,消息队列中取出的消息
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("消费者-1:" + new String(body));
}
});
// 不建议关闭通道,因为要一直监听,而且很可能通道关闭了,回调函数还没来得及调用
// channel.close();
// connection.close();
}
}
4、消费者2
package fanout;
import com.rabbitmq.client.*;
import utils.RabbitMQUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* Created by zjl
* 2020/10/23
**/
public class Customer2 {
public static void main(String[] args) throws IOException, TimeoutException {
//通过工具类获取连接
Connection connection = RabbitMQUtils.getConnection();
//获取连接中通道
Channel channel = connection.createChannel();
//通道绑定交换机
channel.exchangeDeclare("logs","fanout");
//临时队列
String queueName = channel.queueDeclare().getQueue();
//绑定交换机和队列
channel.queueBind(queueName,"logs","");
//发布消息
//参数1:消费哪个队列的消息 队列名称 参数2:开始消息的自动确认机制 参数3:消费时的回调接口
channel.basicConsume(queueName,true,new DefaultConsumer(channel){
@Override //最后一个参数,消息队列中取出的消息
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("消费者-2:" + new String(body));
}
});
// 不建议关闭通道,因为要一直监听,而且很可能通道关闭了,回调函数还没来得及调用
// channel.close();
// connection.close();
}
}
5、消费者3
package fanout;
import com.rabbitmq.client.*;
import utils.RabbitMQUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* Created by zjl
* 2020/10/23
**/
public class Customer3 {
public static void main(String[] args) throws IOException, TimeoutException {
//通过工具类获取连接
Connection connection = RabbitMQUtils.getConnection();
//获取连接中通道
Channel channel = connection.createChannel();
//通道绑定交换机
channel.exchangeDeclare("logs","fanout");
//临时队列
String queueName = channel.queueDeclare().getQueue();
//绑定交换机和队列
channel.queueBind(queueName,"logs","");
//发布消息
//参数1:消费哪个队列的消息 队列名称 参数2:开始消息的自动确认机制 参数3:消费时的回调接口
channel.basicConsume(queueName,true,new DefaultConsumer(channel){
@Override //最后一个参数,消息队列中取出的消息
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("消费者-3:" + new String(body));
}
});
// 不建议关闭通道,因为要一直监听,而且很可能通道关闭了,回调函数还没来得及调用
// channel.close();
// connection.close();
}
}