RabbitMQ与.net core(三) fanout类型Exchange 与 消息的过期时间 与 队列的存活时间
上一篇我们讲了关于direct类型的Exchange,这一片我们来了解一下fanout类型的Exchange。
1.Exchange的fanout类型
fanout类型的Exchange的特点是会把消息发送给与之绑定的所有Queue中,我们来测试一下。代码如下
using RabbitMQ.Client;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace RabbitMQConsole
{
class Program
{
static void Main(string[] args)
{
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "39.**.**.**";
factory.Port = ;
factory.VirtualHost = "/";
factory.UserName = "root";
factory.Password = "root"; var exchange = "change3";
var route = "route2";
var queue3 = "queue3";
var queue4 = "queue4";
var queue5 = "queue5"; using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchange, type: "fanout", durable: true, autoDelete: false);
channel.QueueDeclare(queue3, durable: true, exclusive: false, autoDelete: false);
channel.QueueBind(queue3, exchange, queue3); channel.QueueDeclare(queue4, durable: true, exclusive: false, autoDelete: false);
channel.QueueBind(queue4, exchange, queue4); channel.QueueDeclare(queue5, durable: true, exclusive: false, autoDelete: false);
channel.QueueBind(queue5, exchange, queue5); var props = channel.CreateBasicProperties();
props.Persistent = true;
channel.BasicPublish(exchange, route, true, props, Encoding.UTF8.GetBytes("hello rabbit")); }
}
}
}
}
运行代码,去可视化工具中查看一下
消费其中的一个
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
using System.Threading; namespace RabbitMQClient
{
class Program
{
private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory()
{
HostName = "39.**.**.**",
Port = ,
UserName = "root",
Password = "root",
VirtualHost = "/"
};
static void Main(string[] args)
{
var exchange = "change3";
var route = "route2";
var queue = "queue3"; using (IConnection conn = rabbitMqFactory.CreateConnection())
using (IModel channel = conn.CreateModel())
{
channel.ExchangeDeclare(exchange, "fanout", durable: true, autoDelete: false);
channel.QueueDeclare(queue, durable: true, exclusive: false, autoDelete: false);
channel.QueueBind(queue, exchange, route); channel.BasicQos(prefetchSize: , prefetchCount: , global: false);
EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
Byte[] body = ea.Body;
String message = Encoding.UTF8.GetString(body);
Console.WriteLine( message+Thread.CurrentThread.ManagedThreadId);
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
}; channel.BasicConsume(queue: queue, autoAck: false, consumer: consumer);
Console.ReadLine();
}
}
}
}
结果如下
大家可以依次消费其他两个Queue,这里就不演示了
2.消息的过期时间
我们在发送一些消息的时候,有时希望给消息设置一下过期时间,我们可以通过两种方式来设置
2.1设置队列的过期时间
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace RabbitMQConsole
{
class Program
{
static void Main(string[] args)
{
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "39.**.**.**";
factory.Port = ;
factory.VirtualHost = "/";
factory.UserName = "root";
factory.Password = "root"; var exchange = "change4";
var route = "route2";
var queue7 = "queue7"; using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchange, type: "fanout", durable: true, autoDelete: false);
//队列过期时间,单位毫秒
channel.QueueDeclare(queue7, durable: true, exclusive: false, autoDelete: false,arguments:new Dictionary<string, object> { { "x-message-ttl", } });
channel.QueueBind(queue7, exchange, queue7); var props = channel.CreateBasicProperties();
props.Persistent = true;
channel.BasicPublish(exchange, route, true, props, Encoding.UTF8.GetBytes("hello rabbit")); }
}
}
}
}
这样过8秒去Queue就看不到该消息了
2.2设置message的过期时间
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace RabbitMQConsole
{
class Program
{
static void Main(string[] args)
{
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "39.**.**.**";
factory.Port = ;
factory.VirtualHost = "/";
factory.UserName = "root";
factory.Password = "root"; var exchange = "change4";
var route = "route2";
var queue7 = "queue7"; using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchange, type: "fanout", durable: true, autoDelete: false);
channel.QueueDeclare(queue7, durable: true, exclusive: false, autoDelete: false,arguments:new Dictionary<string, object> { { "x-message-ttl", } });
channel.QueueBind(queue7, exchange, queue7); var props = channel.CreateBasicProperties();
//message过期时间,单位毫秒
props.Expiration = "";
props.Persistent = true;
channel.BasicPublish(exchange, route, true, props, Encoding.UTF8.GetBytes("hello rabbit")); }
}
}
}
}
我们发现还是8秒就过期了,说明如果同时设置了队列与消息的过期时间,则按照队列的时间过期。我们把队列的过期时间去掉重新试一下。
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace RabbitMQConsole
{
class Program
{
static void Main(string[] args)
{
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "39.**.**.**";
factory.Port = ;
factory.VirtualHost = "/";
factory.UserName = "root";
factory.Password = "root"; var exchange = "change4";
var route = "route2";
var queue7 = "queue7"; using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchange, type: "fanout", durable: true, autoDelete: false);
channel.QueueDeclare(queue7, durable: true, exclusive: false, autoDelete: false);
channel.QueueBind(queue7, exchange, queue7); var props = channel.CreateBasicProperties();
props.Expiration = "";
props.Persistent = true;
channel.BasicPublish(exchange, route, true, props, Encoding.UTF8.GetBytes("hello rabbit")); }
}
}
}
}
3.队列生存时间
我们还可以设置一个队列的生存时间
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace RabbitMQConsole
{
class Program
{
static void Main(string[] args)
{
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "39.**.**.**";
factory.Port = ;
factory.VirtualHost = "/";
factory.UserName = "root";
factory.Password = "root"; var exchange = "change4";
var route = "route2";
var queue8 = "queue8"; using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchange, type: "fanout", durable: true, autoDelete: false);
channel.QueueDeclare(queue8, durable: true, exclusive: false, autoDelete: false,arguments: new Dictionary<string, object> {
{ "x-expires",} //设置当前队列的过期时间为10000毫秒
});
channel.QueueBind(queue8, exchange, queue8); var props = channel.CreateBasicProperties();
props.Persistent = true;
channel.BasicPublish(exchange, route, true, props, Encoding.UTF8.GetBytes("hello rabbit")); }
}
}
}
}
这样10秒后队列就消失了
RabbitMQ与.net core(三) fanout类型Exchange 与 消息的过期时间 与 队列的存活时间的更多相关文章
- 如何从40亿整数中找到不存在的一个 webservice Asp.Net Core 轻松学-10分钟使用EFCore连接MSSQL数据库 WPF实战案例-打印 RabbitMQ与.net core(五) topic类型 与 headers类型 的Exchange
如何从40亿整数中找到不存在的一个 前言 给定一个最多包含40亿个随机排列的32位的顺序整数的顺序文件,找出一个不在文件中的32位整数.(在文件中至少确实一个这样的数-为什么?).在具有足够内存的情况 ...
- ExpandoObject与DynamicObject的使用 RabbitMQ与.net core(一)安装 RabbitMQ与.net core(二)Producer与Exchange ASP.NET Core 2.1 : 十五.图解路由(2.1 or earler) .NET Core中的一个接口多种实现的依赖注入与动态选择看这篇就够了
ExpandoObject与DynamicObject的使用 using ImpromptuInterface; using System; using System.Dynamic; names ...
- RabbitMQ与.net core(二)Producer与Exchange
Producer:消息的生产者,也就是创建消息的对象 Exchange:消息的接受者,也就是用来接收消息的对象,Exchange接收到消息后将消息按照规则发送到与他绑定的Queue中.下面我们来定义一 ...
- RabbitMQ与.net core(五) topic类型 与 headers类型 的Exchange
1.topic类型的Exchange 我们之前说过Topic类型的Exchange是direct类型的模糊查询模式,可以通过routkey来实现模糊消费message,topic的模糊匹配有两种模式: ...
- RabbitMQ fanout类型的Exchange
就目前来说,Exchange是与消息发送端有关的,因为它可以指定将消息发送到哪个或哪些队列中. 本篇文章介绍的fanout类型就是指定将消息群发到与Exchange绑定的所有队列中. fanout这个 ...
- 【RabbitMQ】三种类型交换器 Fanout,Direct,Topic(转)
出处:https://blog.csdn.net/fxq8866/article/details/62049393 RabbitMQ服务器会根据路由键将消息从交换器路由到队列中,如何处理投递到多个队列 ...
- RabbitMQ Exchange中的fanout类型
fanout 多播 在之前都是使用direct直连类型的交换机,通过routingkey来决定把消息推到哪个queue中. 而fanout则是把拿到消息推到与之绑定的所有queue中. 分析业务,怎样 ...
- RabbitMQ各种交换机类型Exchange Types介绍
最新版本的RabbitMQ有四种交换机类型,分别是Direct exchange.Fanout exchange.Topic exchange.Headers exchange. 一.Direct E ...
- RabbitMQ 入门 (Go) - 4. 使用 Fanout Exchange 做服务发现(上)
到目前为止,我们项目的结果大致如下: 传感器生成的模拟数据(包含传感器名称.数据.时间戳)是通过传感器在运行时动态创建的 Queue 来发送的.这些 Queue 很难直接被发现. 为了解决这个问题,我 ...
随机推荐
- 伸展树(SplayTree)的实现
优点:伸展树(splay tree)是一种能自我调整的二叉搜索树(BST).虽然某一次的访问操作所花费的时间比较长,但是平摊(amortized) 之后的访问操作(例如旋转)时间能达到O(logn)的 ...
- Kibana安装(图文详解)(多节点的ELK集群安装在一个节点就好)
对于Kibana ,我们知道,是Elasticsearch/Logstash/Kibana的必不可少成员. 前提: Elasticsearch-2.4.3的下载(图文详解) Elasticsearch ...
- 选择性导出excel表中内容
package com.huawei.utils; import java.io.FileNotFoundException;import java.io.FileOutputStream;impor ...
- java动态规划取硬币问题
最近一直在研究动态规划的问题.今天遇到了取硬币问题. 其实动态规划还是,我从底部向顶部,依次求出每个状态的最小值,然后就可以标记上. 这道题目就是,假如有1,5,7,10这四种币值的硬币,我取14元, ...
- 小程序动态生成二维码,生成image图片
前端: <image src="{{img_usrl}}" style="width:100%;height:104px;" bindlongtap=&q ...
- Codeforces 678E 状压DP
题意:有n位选手,已知n位选手之间两两获胜的概率,问主角(第一个选手)最终站在擂台上的概率是多少? 思路:一看数据范围肯定是状压DP,不过虽然是概率DP,但是需要倒着推:我们如果正着推式子的话,初始状 ...
- jdk+Tomcat环境
1.Tomcat概述 Tomcat服务器由Apache提供,开源免费.安装Tomcat之前需要先安装JDK,其实无论哪一种Javaweb服务器都需要先安装JDK. Tomcat6支持Servlet2. ...
- 注意for循环中变量的作用域-乾颐堂
1 2 for e in collections: pass 在for 循环里, 最后一个对象e一直存在在上下文中.就是在循环外面,接下来对e的引用仍然有效. 这里有个问题容易被忽略,如果在循 ...
- go语言的特殊变量 iota
iota,是go语言的特殊常量,可以认为是一个可以被编译器修改的常量. 在每一个const关键字出现时,被重置为0,然后在下一个const出现之前,每出现一次iota,其所代表的数字会自动增加1. 来 ...
- Golang 之 key-value LevelDB
时常会在应用中用到数据库功能,象 Key-Value 性质的.直接搬个 Redis,Mysql嫌大,好在有 LevelDB,直接编进应用中. 有关什么是 LevelDB 以及 LevelDB 的特性, ...