/// <summary> /// 将交换机信息列表转换为消息队列信息列表 /// </summary> /// <param name="exchanges">交换机信息列表</param> /// <returns>消息队列信息列表</returns> public static IList <RabbitMessageQueueInfo> ToMessageQueues(this IList <RabbitExchangeInfo> exchanges) { if (exchanges.IsNullOrCount0()) { return(null); } IList <RabbitMessageQueueInfo> result = new List <RabbitMessageQueueInfo>(); foreach (RabbitExchangeInfo item in exchanges) { if (item.Queues.IsNullOrCount0()) { result.Add(ToMessageQueue(item)); } else { foreach (RabbitQueueModel item2 in item.Queues) { RabbitMessageQueueInfo model = ToMessageQueue(item); model.AutoDelQueue = item2.AutoDelQueue; model.Qos = item2.Qos; model.Queue = item2.Name; model.RoutingKeys = item2.RoutingKeys; result.Add(model); } } } return(result); }
/// <summary> /// 根据队列读取消息队列信息 /// </summary> /// <param name="queueOrOtherIdentify">队列或其他标识</param> /// <param name="extend">扩展</param> /// <returns>消息队列信息</returns> public RabbitMessageQueueInfo Reader(string queueOrOtherIdentify, IDictionary <string, string> extend = null) { if (string.IsNullOrWhiteSpace(queueOrOtherIdentify)) { throw new ArgumentNullException("队列或其他标识不能为空"); } var virtualPath = extend.GetVirtualPath(); RabbitMessageQueueInfo info = null; if (queueOrOtherIdentify.Contains(":")) { var names = queueOrOtherIdentify.Split(':'); if (string.IsNullOrWhiteSpace(names[0]) || string.IsNullOrWhiteSpace(names[1])) { throw new ArgumentNullException("交换机和队列不能为空"); } info = ReaderByExchangeAndQueue(names[0], names[1], virtualPath); } else { // 先按队列名去找,找不到再按交换机名去找 info = ReaderByQueue(queueOrOtherIdentify, virtualPath); if (info == null) { info = ReaderByExchange(queueOrOtherIdentify, virtualPath); } } return(info); }
/// <summary> /// 根据队列读取消息队列信息 /// </summary> /// <param name="queueOrOtherIdentify">队列或其他标识</param> /// <param name="allList">所有列表</param> /// <returns>消息队列信息</returns> public static RabbitMessageQueueInfo Reader(string queueOrOtherIdentify, IList <RabbitMessageQueueInfo> allList) { if (string.IsNullOrWhiteSpace(queueOrOtherIdentify)) { throw new ArgumentNullException("队列或其他标识不能为空"); } RabbitMessageQueueInfo info = null; if (queueOrOtherIdentify.Contains(":")) { var names = queueOrOtherIdentify.Split(':'); if (string.IsNullOrWhiteSpace(names[0]) || string.IsNullOrWhiteSpace(names[1])) { throw new ArgumentNullException("交换机和队列不能为空"); } info = ReaderByExchangeAndQueue(names[0], names[1], allList); } else { // 先按队列名去找,找不到再按交换机名去找 info = ReaderByQueue(queueOrOtherIdentify, allList); if (info == null) { info = ReaderByExchange(queueOrOtherIdentify, allList); } } return(info); }
/// <summary> /// 构造方法 /// 初始化各个对象以便就绪 /// 只初始化交换机与基本属性,队列定义请重写Init方法进行操作 /// </summary> /// <param name="channel">渠道</param> /// <param name="rabbitMessageQueueInfo">Rabbit消息队列信息</param> public RabbitRpcClient(IModel channel, RabbitMessageQueueInfo rabbitMessageQueueInfo) { ValidateUtil.ValidateNull(channel, "渠道"); ValidateUtil.ValidateNull(rabbitMessageQueueInfo, "消息队列信息"); this._channel = channel; this.rabbitMessageQueueInfo = rabbitMessageQueueInfo; _replyQueueName = _channel.QueueDeclare().QueueName; _channel.QueueBind(_replyQueueName, rabbitMessageQueueInfo.Exchange, _replyQueueName); _consumer = new EventingBasicConsumer(_channel); _consumer.Received += (model, ea) => { if (!_callbackMapper.TryRemove(ea.BasicProperties.CorrelationId, out TaskCompletionSource <byte[]> tcs)) { return; } if (ea.Body.IsEmpty) { tcs.TrySetResult(null); } else { tcs.TrySetResult(ea.Body.ToArray()); } }; }
/// <summary> /// 将交换机信息列表转换为消息队列信息列表 /// </summary> /// <param name="exchanges">交换机信息列表</param> /// <returns>消息队列信息列表</returns> private IList <RabbitMessageQueueInfo> Trans(IList <RabbitExchangeInfo> exchanges) { if (exchanges.IsNullOrCount0()) { return(null); } IList <RabbitMessageQueueInfo> result = new List <RabbitMessageQueueInfo>(); foreach (RabbitExchangeInfo item in exchanges) { if (item.Queues.IsNullOrCount0()) { result.Add(CreateBasicProps(item)); } else { foreach (RabbitQueueModel item2 in item.Queues) { RabbitMessageQueueInfo model = CreateBasicProps(item); model.AutoDelQueue = item2.AutoDelQueue; model.Qos = item2.Qos; model.Queue = item2.Name; model.RoutingKeys = item2.RoutingKeys; result.Add(model); } } } return(result); }
/// <summary> /// 构造方法 /// 初始化各个对象以便就绪 /// 只初始化交换机与基本属性,队列定义请重写Init方法进行操作 /// </summary> /// <param name="channel">渠道</param> /// <param name="rabbitMessageQueueInfo">Rabbit消息队列信息</param> public RabbitRpcServer(IModel channel, RabbitMessageQueueInfo rabbitMessageQueueInfo) { ValidateUtil.ValidateNull(channel, "渠道"); ValidateUtil.ValidateNull(rabbitMessageQueueInfo, "消息队列信息"); this._channel = channel; this.rabbitMessageQueueInfo = rabbitMessageQueueInfo; }
/// <summary> /// 创建消息队列信息并赋基本值 /// </summary> /// <param name="rabbitExchangeInfo">Rabbit交换机信息</param> /// <returns>Rabbit消息队列信息</returns> private RabbitMessageQueueInfo CreateBasicProps(RabbitExchangeInfo rabbitExchangeInfo) { RabbitMessageQueueInfo model = new RabbitMessageQueueInfo(); model.Exchange = rabbitExchangeInfo.Name; model.Type = rabbitExchangeInfo.Type; model.Persistent = rabbitExchangeInfo.Persistent; return(model); }
/// <summary> /// 创建消息队列信息并赋基本值 /// </summary> /// <param name="rabbitExchangeInfo">Rabbit交换机信息</param> /// <returns>Rabbit消息队列信息</returns> public static RabbitMessageQueueInfo ToMessageQueue(this RabbitExchangeInfo rabbitExchangeInfo) { RabbitMessageQueueInfo model = new RabbitMessageQueueInfo(); model.Exchange = rabbitExchangeInfo.Name; model.Type = rabbitExchangeInfo.Type; model.Persistent = rabbitExchangeInfo.Persistent; return(model); }
/// <summary> /// 创建生产者 /// </summary> /// <param name="messageQueueInfo">消息队列信息</param> /// <returns>生产者</returns> public IProducer CreateProducer(MessageQueueInfo messageQueueInfo) { ValidateUtil.ValidateNull(messageQueueInfo, "Rabbit消息队列信息"); ValidateConnection(); IProducer producer = new RabbitProducer(CreateChannel(), RabbitMessageQueueInfo.From(messageQueueInfo)); AddClosedEventHandler(producer); return(producer); }
/// <summary> /// 创建RPC服务端 /// </summary> /// <param name="messageQueueInfo">消息队列信息</param> /// <returns>RPC服务端</returns> public IRpcServer CreateRpcServer(MessageQueueInfo messageQueueInfo) { ValidateUtil.ValidateNull(messageQueueInfo, "Rabbit消息队列信息"); ValidateConnection(); IRpcServer rpcServer = new RabbitRpcServer(CreateChannel(), RabbitMessageQueueInfo.From(messageQueueInfo)); AddClosedEventHandler(rpcServer); return(rpcServer); }
/// <summary> /// 从配置里验证消息队列信息 /// </summary> /// <param name="queueOrOtherIdentify">队列或其他标识</param> /// <returns>Rabbit消息队列信息</returns> private RabbitMessageQueueInfo ValidateMessageQueueInfoFromConfig(string queueOrOtherIdentify) { if (MessageQueueInfoFactory == null) { MessageQueueInfoFactory = new RabbitMessageQueueInfoConfigFactory(); } RabbitMessageQueueInfo rabbitMessageQueueInfo = MessageQueueInfoFactory.Create(queueOrOtherIdentify) as RabbitMessageQueueInfo; ValidateUtil.ValidateNull2(rabbitMessageQueueInfo, string.Format("队列或其他标识[{0}]在配置里不存在", queueOrOtherIdentify)); return(rabbitMessageQueueInfo); }
/// <summary> /// 构造方法 /// 初始化各个对象以便就绪 /// 只初始化交换机与基本属性,队列定义请重写Init方法进行操作 /// </summary> /// <param name="channel">渠道</param> /// <param name="rabbitMessageQueueInfo">Rabbit消息队列信息</param> public RabbitCoreBase(IModel channel, RabbitMessageQueueInfo rabbitMessageQueueInfo) { ValidateUtil.ValidateNull(channel, "渠道"); ValidateUtil.ValidateNull(rabbitMessageQueueInfo, "消息队列信息"); this.channel = channel; this.rabbitMessageQueueInfo = rabbitMessageQueueInfo; channel.ExchangeDeclare(rabbitMessageQueueInfo.Exchange, rabbitMessageQueueInfo.Type, rabbitMessageQueueInfo.Persistent); if (rabbitMessageQueueInfo.Qos != null) { channel.BasicQos(0, rabbitMessageQueueInfo.Qos.GetValueOrDefault(), false); } basicProperties = channel.CreateBasicProperties(); basicProperties.Persistent = rabbitMessageQueueInfo.Persistent; Init(); }
/// <summary> /// 构造方法 /// 初始化各个对象以便就绪 /// </summary> /// <param name="channel">渠道</param> /// <param name="messageQueueInfoFactory">消息队列信息工厂</param> public RabbitCoreBase(IModel channel, IMessageQueueInfoFactory messageQueueInfoFactory) : this(channel, RabbitMessageQueueInfo.From(messageQueueInfoFactory.Create())) { }
/// <summary> /// 构造方法 /// </summary> /// <param name="channel">渠道</param> /// <param name="rabbitMessageQueueInfo">Rabbit消息队列信息</param> public RabbitRpcServer(IModel channel, RabbitMessageQueueInfo rabbitMessageQueueInfo) : base(channel, rabbitMessageQueueInfo) { }
/// <summary> /// 构造方法 /// 初始化各个对象以便就绪 /// </summary> /// <param name="channel">渠道</param> /// <param name="queueOrOtherIdentify">队列或其他标识</param> /// <param name="messageQueueInfoFactory">消息队列信息工厂</param> /// <param name="virtualPath">虚拟路径</param> public RabbitRpcServer(IModel channel, string queueOrOtherIdentify, IMessageQueueInfoFactory messageQueueInfoFactory, string virtualPath = RabbitConnectionInfo.DEFAULT_VIRTUAL_PATH) : this(channel, RabbitMessageQueueInfo.From(messageQueueInfoFactory.Create(queueOrOtherIdentify, ConfigUtil.CreateContainerVirtualPathDic(virtualPath)))) { }
/// <summary> /// 构造方法 /// </summary> /// <param name="channel">渠道</param> /// <param name="rabbitMessageQueueInfo">Rabbit消息队列信息</param> public RabbitProducer(IModel channel, RabbitMessageQueueInfo rabbitMessageQueueInfo) : base(channel, rabbitMessageQueueInfo) { }
/// <summary> /// 构造方法 /// </summary> /// <param name="channel">渠道</param> /// <param name="rabbitMessageQueueInfo">Rabbit消息队列信息</param> public RabbitConsumer(IModel channel, RabbitMessageQueueInfo rabbitMessageQueueInfo) : base(channel, rabbitMessageQueueInfo) { }
/// <summary> /// 构造方法 /// </summary> /// <param name="channel">渠道</param> /// <param name="rabbitMessageQueueInfo">Rabbit消息队列信息</param> public RabbitRpcClient(IModel channel, RabbitMessageQueueInfo rabbitMessageQueueInfo) : base(channel, rabbitMessageQueueInfo) { }
/// <summary> /// 获取日志标签集合 /// </summary> /// <param name="rabbitMessageQueueInfo">Rabbit消息队列信息</param> /// <returns>日志标签集合</returns> private string[] GetLogTags(RabbitMessageQueueInfo rabbitMessageQueueInfo) { return(new string[] { typeof(RabbitConsumer).Name, rabbitMessageQueueInfo.Exchange, rabbitMessageQueueInfo.Queue }); }