RabbitMQ入门案例—fanout模式
package com.tian.rabbitmq.routing;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Producer {
public static void main(String[] args) {
//所有的中间件技术都是基于tcp/ip协议基础之上构建新型的协议规范,只不过rabbitmq遵循的是amcp
// ip port
//1:创建连接工程
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.1.150");
connectionFactory.setPort(5672);
connectionFactory.setUsername("admin");
connectionFactory.setPassword("admin");
connectionFactory.setVirtualHost("/");
Connection connection = null;
Channel channel = null;
try {
//2:创建连接connection
connection = connectionFactory.newConnection("生产者");
//3:通过连接获取通道Channel
channel = connection.createChannel();
//4:通过创建交换机,声明队列,绑定关系,路由key, 发送消息,和接收消息
String queueName = "queue1";
/**
* @params1 队列的名称
* @params2 是否持久化
* @params3 排他性
* @params4 是否自动删除
* @params5 携带附属参数
*/
channel.queueDeclare(queueName, false, false, false, null);
//5:准备消息内容
String message = "hello rabbitmq";
// 准备交换机
String exchangeName="faout-exchange";
//定义路由key
String routeKey="";
//指定交换机的类型
String type="fanout";
//6:发送消息给队列queue
//@params1:交换机
channel.basicPublish(exchangeName, routeKey, null, message.getBytes());
System.out.println("消息发送成功");
} catch (Exception ex) {
ex.printStackTrace();
}finally {
//7:关闭连接
if(channel!=null&&channel.isOpen()){
try {
channel.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
//8:关闭通道
if(connection!=null&&connection.isOpen()){
try {
connection.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
}
}
}
已经是绑定上了
运行后 queue11 queue2 queue3 都会多一条消息
package com.tian.rabbitmq.routing;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Consumer {
private static Runnable runnable=new Runnable() {
public void run() {
//1:创建连接工程
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.1.150");
connectionFactory.setPort(5672);
connectionFactory.setUsername("admin");
connectionFactory.setPassword("admin");
connectionFactory.setVirtualHost("/");
final String queryName = Thread.currentThread().getName();
Connection connection = null;
Channel channel = null;
try {
//2:创建连接connection
connection = connectionFactory.newConnection("生产者");
//3:通过连接获取通道Channel
channel = connection.createChannel();
channel.basicConsume(queryName, true, new DeliverCallback() {
public void handle(String s, Delivery delivery) throws IOException {
System.out.println(queryName+"收到的消息是" + new String(delivery.getBody(), "UTF-8"));
}
}, new CancelCallback() {
public void handle(String s) throws IOException {
System.out.println("接收失败了");
}
});
System.out.println("开始接收消息");
// System.in.read();
} catch (Exception ex) {
ex.printStackTrace();
}finally {
//7:关闭连接
if(channel!=null&&channel.isOpen()){
try {
channel.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
//8:关闭通道
if(connection!=null&&connection.isOpen()){
try {
connection.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
}
}
};
public static void main(String[] args) {
new Thread(runnable,"queue11").start();
new Thread(runnable,"queue2").start();
new Thread(runnable,"queue3").start();
}
}
运行之后 消息都被消费了
Direct模式
producer代码改这几个即可
Topic模式
producer代码改这几个即可
绑定关系
完整的声明创建方式
producer
package com.tian.rabbitmq.all;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Producer {
public static void main(String[] args) {
//所有的中间件技术都是基于tcp/ip协议基础之上构建新型的协议规范,只不过rabbitmq遵循的是amcp
// ip port
//1:创建连接工程
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.1.150");
connectionFactory.setPort(5672);
connectionFactory.setUsername("admin");
connectionFactory.setPassword("admin");
connectionFactory.setVirtualHost("/");
Connection connection = null;
Channel channel = null;
try {
//2:创建连接connection
connection = connectionFactory.newConnection("生产者");
//3:通过连接获取通道Channel
channel = connection.createChannel();
//4:通过创建交换机,声明队列,绑定关系,路由key, 发送消息,和接收消息
String queueName = "queue1";
channel.queueDeclare(queueName, false, false, false, null);
//5:准备消息内容
String message = "你好 hello rabbitmq";
// 准备交换机
String exchangeName="direct_message_exchange";
//指定交换机的类型
String exchangeType="direct";
channel.exchangeDeclare(exchangeName,exchangeType,true);
//声明队列
channel.queueDeclare("queue5",true,false,false,null);
channel.queueDeclare("queue6",true,false,false,null);
channel.queueDeclare("queue7",true,false,false,null);
//绑定队列和交换机的关系
channel.queueBind("queue5",exchangeName,"order");
channel.queueBind("queue6",exchangeName,"order");
channel.queueBind("queue7",exchangeName,"course");
channel.basicPublish(exchangeName, "course", null, message.getBytes());
System.out.println("消息发送成功");
} catch (Exception ex) {
ex.printStackTrace();
}finally {
//7:关闭连接
if(channel!=null&&channel.isOpen()){
try {
channel.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
//8:关闭通道
if(connection!=null&&connection.isOpen()){
try {
connection.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
}
}
}