RabbitMQ备份交换器
备份交换器,AlternateExchange(AE);
备份交换器是为了实现没有路由到队列的消息,与上篇介绍到的mandatory都是为了处理没有路由到的消息。
AE相对于mandatory逻辑更简单,不需要另外实现basic.Return,但是要另外设置一个Exchange,指向默认队列,消费该默认队列的消费者,处理接收到的消息。
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = IP_ADDRESS;
factory.Port = PORT;
factory.UserName = USER_NAME;
factory.Password = PASSWORD;
con = factory.CreateConnection();
channel = con.CreateModel();
//备份交换器
Dictionary<string, object> args = new Dictionary<string, object>();
args.Add("alternate-exchage", "myAe");
channel.ExchangeDeclare(EXCHANGE_NAME, "topic", true, false, args);
channel.ExchangeDeclare("myAe", "fanout", true, false, null);//myAe的类型是fanout,没有路由的
//主队列
channel.QueueDeclare(QUEUE_NAME, true, false, false, null);
channel.QueueBind(QUEUE_NAME, EXCHANGE_NAME, BINDING_KEY, null);
//备份队列
channel.QueueDeclare("unroutedQueue", true, false, false, null);
channel.QueueBind("unroutedQueue", "myAe", null);//myAe的类型是fanout,没有路由的
string message = "Hello Word!";
var body = Encoding.UTF8.GetBytes(message);
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
//如果交换器EXCHANGE_NAME根据ROUTING_KEY找到对应的队列,则发送给对应的队列
//如果交换器EXCHANGE_NAME根据ROUTING_KEY没有找到对应的队列,则发送给交换器myAe
channel.BasicPublish(EXCHANGE_NAME, ROUTING_KEY, properties, body);
---------------------
作者:wby90
来源:CSDN
原文:https://blog.csdn.net/wby90/article/details/79590136
版权声明:本文为博主原创文章,转载请附上博文链接!