RabbitMQ: Four ExChange usages

Abstract:  When RabbitMQ sends a message, it first sends the message to ExChange (exchange), and then distributes it to the Queue (queue) with the corresponding RoutingKey (routing) relationship. ExChange and Queue used to have a many-to-many relationship. When creating ExChange after RabbitMQ 3.0, there are four types of options "fanout, direct, topic, headers".

When RabbitMQ sends a message, it first sends the message to ExChange (exchange), and then distributes it to the Queue (queue) with the corresponding RoutingKey (routing) relationship.
ExChange and Queue used to have a many-to-many relationship.
When creating ExChange after RabbitMQ 3.0, there are four types of options "fanout, direct, topic, headers".
 
1. Fanout
The setting of RoutingKey has no effect when sending a message to a fanout.
The message will be sent to all queues under the same exchange, and the message received by each queue is the same;
A queue with all consumers (including those without a corresponding RoutingKey ) will divide the messages received by the queue equally .
 
----------------Message Producer ----------------
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(S_RabbitMQ.QUEUE_IP);// MQ host
factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ port
factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ username
factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ password
 
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
 
// declare route name and type
channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true, false, null);
String message = "hello world! ";
 
for(int i=0;i<100;i++)
{
channel.basicPublish(EXCHANGE_NAME, "", null, (message+i).getBytes());
}
 
System.out.println("Sent msg finish");
 
channel.close();
connection.close();

----------------Message Consumers ----------------
ConnectionFactory factory = new ConnectionFactory();
 
factory.setHost(S_RabbitMQ.QUEUE_IP);// MQ host
factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ port
factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ username
factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ password
 
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
 
//declare route name and type
channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true, false, null);
// 声明 队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
//绑定路由和队列
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "routkey2", null);
 
System.out.println(" Waiting for msg....");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope,   AMQP.BasicProperties properties, byte[] body) {
String message = "";
try
{
message = new String(body, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (Throwable ex)
{
ex.printStackTrace();
}
 
System.out.println("Received msg='" + message + "'");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
 
二、direct
当向一个direct发送一个消息时, 消息会被发送给同一个交换机下的 拥有相应RoutingKey的队列, 每个队列接收到的消息是一样的;
一个队列内拥有相应RoutingKey的消费者,将平分队列接收到的消息。
 
----------------消息生产者 ----------------
ConnectionFactory factory = new ConnectionFactory();
 
factory.setHost(S_RabbitMQ.QUEUE_IP);// MQ主机
factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ端口
factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ用户名
factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ密码
 
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
 
// 声明路由名字和类型
channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);
String message = "hello world! ";
 
for(int i=0;i<100;i++)
{
channel.basicPublish(EXCHANGE_NAME, "routingkey1", null, (message+i).getBytes());
}
 
System.out.println("Sent msg is '" + message + "'");
 
channel.close();
connection.close();

----------------消息消费者 ----------------
ConnectionFactory factory = new ConnectionFactory();
 
factory.setHost(S_RabbitMQ.QUEUE_IP);// MQ主机
factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ端口
factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ用户名
factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ密码
 
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
 
//声明路由名字和类型
channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);
//声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
//绑定路由和队列
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "routingkey1", null);
 
System.out.println(" Waiting for msg....");
Consumer consumer = new DefaultConsumer(channel)
{
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body)
{
String message = "";
try
{
message = new String(body, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (Throwable ex)
{
ex.printStackTrace();
}
 
System.out.println("1 Received msg='" + message + "'");
}
};
 
channel.basicConsume(QUEUE_NAME, true, consumer);
 
三、topic
当向一个topic发送一个消息时 消息会被发送给同一个交换机下的 拥有相应RoutingKey的队列, 每个队列接收到的消息是一样的;
一个队列内有所有消费者(包含那些并没有相应RoutingKey的 消费者 ),将平分队列接收到的消息
 
----------------消息生产者 ----------------
ConnectionFactory factory = new ConnectionFactory();
 
factory.setHost(S_RabbitMQ.QUEUE_IP);// MQ主机
factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ端口
factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ用户名
factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ密码
 
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
 
// 声明路由名字和类型
channel.exchangeDeclare(EXCHANGE_NAME, "topic", true, false, null);
String message = "hello world! ";
 
// int i=101;
for (int i = 0; i < 100; i++)
{
channel.basicPublish(EXCHANGE_NAME, "routingkey1", null, (message + i).getBytes());
}
 
System.out.println("Sent msg is '" + message + "'");
 
channel.close();
connection.close();
 
----------------消息消费者 ----------------
ConnectionFactory factory = new ConnectionFactory();
 
factory.setHost(S_RabbitMQ.QUEUE_IP);// MQ主机
factory.setPort(S_RabbitMQ.QUEUE_PORT);// MQ端口
factory.setUsername(S_RabbitMQ.QUEUE_USER);// MQ用户名
factory.setPassword(S_RabbitMQ.QUEUE_PWD);// MQ密码
 
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
 
// 声明路由名字和类型
channel.exchangeDeclare(EXCHANGE_NAME, "topic", true, false, null);
//声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
//绑定路由和队列// 把队列绑定到路由上并指定headers
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "routingkey1", null);
 
System.out.println("1 Waiting for msg....");
Consumer consumer = new DefaultConsumer(channel)
{
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,  byte[] body)
{
String message = "";
try
{
message = new String(body, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (Throwable ex)
{
ex.printStackTrace();
}
 
System.out.println("1 Received msg='" + message + "'");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326991902&siteId=291194637