rabbitmq 三种交换器类型 fanout direct topic
sender 代码
package com.lzf.cn.model;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Send {
/**
* 场景1
* 单发送单接收
*/
private final static String QUEUE_NAME = "test";
public static void main(String[] argv) throws Exception {
/* ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.84.222");
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("*******");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
boolean durable = true; //消息持久化
channel.queueDeclare(QUEUE_NAME, durable, false, false, null);
String message = "Hello World23!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();*/
Send send = new Send();
send.topic();
}
private final static String EXCHANGE_NAME_FANOUT="test_exchange_fanout";
private final static String EXCHANGE_NAME_DIRECT="test_exchange_direct";
private final static String EXCHANGE_NAME_TOPIC="test_exchange_topic";
public void fanout() {
try{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("XXX.XXX.XXX");
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("*******");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//声明交换器
channel.exchangeDeclare(EXCHANGE_NAME_FANOUT,"fanout");//fanout交换器
//消息内容
String message = "message=测试";
channel.basicPublish(EXCHANGE_NAME_FANOUT,"",null,message.getBytes());
System.out.println(" [x] Sent'"+message+"'" );
channel.close();
connection.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void direct(){
try{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("XXX.XXX.XXX");
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("*******");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//声明exchange
channel.exchangeDeclare(EXCHANGE_NAME_DIRECT,"direct");
//消息内容
String message = "message=测试";
channel.basicPublish(EXCHANGE_NAME_DIRECT,"delete",null,message.getBytes()); //此处delete为路由键;
System.out.println(" [x] Sent '"+ message+"'");
channel.close();
connection.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void topic(){
try{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("XXX.XXX.XXX");
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("*******");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//声明exchange
channel.exchangeDeclare(EXCHANGE_NAME_TOPIC,"topic");
//消息内容
String message = "message=测试";
//发布消息
channel.basicPublish(EXCHANGE_NAME_TOPIC,"item.insert",null,message.getBytes());
System.out.println(" [x] Sent '"+message + "'");
channel.close();
connection.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
consumer 代码
package com.lzf.cn.model;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
public class Recv {
private final static String QUEUE_NAME="test_queue_fanout_1";
public static void main(String[] argv) throws Exception {
/* ConnectionFactory factory = new ConnectionFactory();
factory.setHost("XXX.XXX.XXX");
factory.setPort(5672);
factory.setUsername("liuzhengfeng");
factory.setPassword("liuzhengfeng");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
boolean durable = true; //消息持久化
channel.queueDeclare(QUEUE_NAME, durable, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
boolean autoack = false; //消息确认
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, autoack, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println(" [x] Received '" + message + "'");
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), true);
continue;
}*/
Recv recv = new Recv();
recv.recvTopic();
}
private final static String EXCHANGE_NAME_DIRECT="test_exchange_direct";
private final static String EXCHANGE_NAME_FANOUT="test_exchange_fanout";
private final static String EXCHANGE_NAME_TOPIC="test_exchange_topic";
public void recvFanout(){
try{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("XXX.XXX.XXX");
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("******");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
//绑定队列到交换器
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME_FANOUT,""); //不设置路由键
//统一时刻服务器只会发一条消息给消费者;
channel.basicQos(1);
//定义队列的消费者
QueueingConsumer consumer = new QueueingConsumer(channel);
//监听队列,手动返回完成
channel.basicConsume(QUEUE_NAME,false,consumer);
//获取消息
while (true)
{
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println(" message:'" + message + "'");
Thread.sleep(10);
//手动返回
channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
}
}catch(Exception e){
e.printStackTrace();
}
}
public void recvDirect(){
try{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("XXX.XXX.XXX");
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("******");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
//绑定队列到交换机;
// channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"update"); //匹配路由键为update
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME_DIRECT,"delete"); //匹配路由键是delete
//同一时刻服务器只会发送一条消息给消费者;
channel.basicQos(1);
QueueingConsumer consumer = new QueueingConsumer(channel);
//监听队列,手动返回完成
channel.basicConsume(QUEUE_NAME,false,consumer);
//获取消息
while (true)
{
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println("message:'"+message+"'");
Thread.sleep(10);
channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 匹配方式
* # 匹配一个或多个词以.隔开
* * 匹配一个词,以.隔开
*/
public void recvTopic(){
try{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("XXX.XXX.XXX");
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("******");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//声明通道
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
//绑定exchange
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME_TOPIC,"item.#"); //使用item.# 匹配所有的以item开头的
//同一时刻服务器只能发送一条消息给消费者;
channel.basicQos(1);
//声明消费者
QueueingConsumer consumer = new QueueingConsumer(channel);
//监控队列,设置手动完成
channel.basicConsume(QUEUE_NAME,false,consumer);
while (true)
{
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println("搜索系统 '" + message + "'");
Thread.sleep(10);
channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
}
}catch(Exception e){
e.printStackTrace();
}
}
}