1、引入依赖
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mine</groupId>
<artifactId>rabbitmq-basic</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.5</version>
</dependency>
</dependencies>
</project>
2、工具类
MQConnectionUtils.java
package com.mine.utils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class MQConnectionUtils {
public static Connection newConnection() throws IOException, TimeoutException {
// 1.定义连接工厂
ConnectionFactory factory = new ConnectionFactory();
// 2.设置服务器地址
factory.setHost("127.0.0.1");
// 3.设置协议端口号
factory.setPort(5672);
// 4.设置vhost
factory.setVirtualHost("/test");
// 5.设置用户名称
factory.setUsername("test");
// 6.设置用户密码
factory.setPassword("test");
// 7.创建新的连接
Connection newConnection = factory.newConnection();
return newConnection;
}
}
3、生产者
FanoutProducer
package com.mine.producer;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.mine.utils.MQConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
// 扇形交换机实现发布订阅
public class FanoutProducer {
// 交换机名称
private static final String EXCHANGE_NAME = "my_fanout_exchange";
public static void main(String[] args) throws IOException, TimeoutException {
// 1.获取连接
Connection connection = MQConnectionUtils.newConnection();
// 2.创建通道
Channel channel = connection.createChannel();
// 3.绑定交换机
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
// 4.发送消息
String msg = "i am publish message 2";
channel.basicPublish(EXCHANGE_NAME, "", null, msg.getBytes());
// 5.关闭资源
channel.close();
connection.close();
}
}
4、消费者
FanoutConsumer1
package com.mine.consumer;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.mine.utils.MQConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
// 扇形交换机消费者
public class FanoutConsumer1 {
// 队列名称
private static final String QUEUE_NAME_FANOUT = "queue_name_fanout_1";
// 交换机名称
private static final String EXCHANGE_NAME = "my_fanout_exchange";
public static void main(String[] args) throws IOException, TimeoutException {
System.out.println("消费者1启动。。。");
// 1.获取连接
Connection connection = MQConnectionUtils.newConnection();
// 2.创建通道
Channel channel = connection.createChannel();
// 3.声明队列
channel.queueDeclare(QUEUE_NAME_FANOUT, false, false, false, null);
// 4.交换机与队列绑定
channel.queueBind(QUEUE_NAME_FANOUT, EXCHANGE_NAME, "");
// 5.消息监听
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body, "UTF-8");
System.out.println("接收到消息:" + msg);
}
};
channel.basicConsume(QUEUE_NAME_FANOUT, true, consumer);
}
}
FanoutConsumer2
package com.mine.consumer;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.mine.utils.MQConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
// 扇形交换机消费者
public class FanoutConsumer2 {
// 队列名称
private static final String QUEUE_NAME_FANOUT = "queue_name_fanout_2";
// 交换机名称
private static final String EXCHANGE_NAME = "my_fanout_exchange";
public static void main(String[] args) throws IOException, TimeoutException {
System.out.println("消费者2启动。。。");
// 1.获取连接
Connection connection = MQConnectionUtils.newConnection();
// 2.创建通道
Channel channel = connection.createChannel();
// 3.声明队列
channel.queueDeclare(QUEUE_NAME_FANOUT, false, false, false, null);
// 4.交换机与队列绑定
channel.queueBind(QUEUE_NAME_FANOUT, EXCHANGE_NAME, "");
// 5.消息监听
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body, "UTF-8");
System.out.println("接收到消息:" + msg);
}
};
channel.basicConsume(QUEUE_NAME_FANOUT, true, consumer);
}
}