示例#1
0
        /// <summary>
        /// exchange为string.Empty时,routingKey就是要发布到的Queue。
        /// </summary>
        public virtual void Publish(string exchange, string routingKey, object content)
        {
            if (Channel == null)
            {
                return;
            }

            try
            {
                var jsonStr = SerializeUtil.ToJson(content);

                //MQ 打印日志
                LogService.Debug($"{exchange}[{routingKey}]: {jsonStr}");

                var bodyData = string.IsNullOrEmpty(jsonStr) ? null : Encoding.UTF8.GetBytes(jsonStr);

                lock (Channel)
                {
                    _commonProp?.ClearMessageId();
                    Channel.BasicPublish(exchange, routingKey, true, _commonProp, bodyData);
                }
            }
            catch (Exception e)
            {
                LogService.Error(e);
            }
        }