Exemplo n.º 1
0
        /// <summary>发送消息</summary>
        /// <param name="msg"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public virtual SendResult Publish(Message msg, Int32 timeout = -1)
        {
            // 选择队列分片
            var mq = SelectQueue();

            mq.Topic = Topic;

            // 构造请求头
            var ts   = DateTime.Now - _dt1970;
            var smrh = new SendMessageRequestHeader
            {
                ProducerGroup  = Group,
                Topic          = Topic,
                QueueId        = mq.QueueId,
                SysFlag        = 0,
                BornTimestamp  = (Int64)ts.TotalMilliseconds,
                Flag           = msg.Flag,
                Properties     = msg.GetProperties(),
                ReconsumeTimes = 0,
                UnitMode       = UnitMode,
            };

            // 性能埋点
            using var span = Tracer?.NewSpan($"mq:{Topic}:Publish");
            try
            {
                // 根据队列获取Broker客户端
                var bk = GetBroker(mq.BrokerName);
                var rs = bk.Invoke(RequestCode.SEND_MESSAGE_V2, msg.Body, smrh.GetProperties(), true);

                // 包装结果
                var sr = new SendResult
                {
                    //Status = SendStatus.SendOK,
                    Queue = mq
                };
                sr.Status = (ResponseCode)rs.Header.Code switch
                {
                    ResponseCode.SUCCESS => SendStatus.SendOK,
                    ResponseCode.FLUSH_DISK_TIMEOUT => SendStatus.FlushDiskTimeout,
                    ResponseCode.FLUSH_SLAVE_TIMEOUT => SendStatus.FlushSlaveTimeout,
                    ResponseCode.SLAVE_NOT_AVAILABLE => SendStatus.SlaveNotAvailable,
                    _ => throw rs.Header.CreateException(),
                };
                sr.Read(rs.Header?.ExtFields);

                return(sr);
            }
            catch (Exception ex)
            {
                span?.SetError(ex, msg);

                throw;
            }
        }
Exemplo n.º 2
0
        /// <summary>发送消息</summary>
        /// <param name="msg"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public virtual SendResult Publish(Message msg, Int32 timeout = -1)
        {
            // 选择队列分片
            var mq = SelectQueue();
            mq.Topic = Topic;

            // 构造请求头
            var ts = DateTime.Now - _dt1970;
            var smrh = new SendMessageRequestHeader
            {
                ProducerGroup = Group,
                Topic = Topic,
                QueueId = mq.QueueId,
                SysFlag = 0,
                BornTimestamp = (Int64)ts.TotalMilliseconds,
                Flag = msg.Flag,
                Properties = msg.GetProperties(),
                ReconsumeTimes = 0,
                UnitMode = UnitMode,
            };

            // 性能埋点
            using var span = Tracer?.NewSpan($"mq:{Topic}:Publish");
            try
            {
                // 根据队列获取Broker客户端
                var bk = GetBroker(mq.BrokerName);
                var rs = bk.Invoke(RequestCode.SEND_MESSAGE_V2, msg.Body, smrh.GetProperties());

                // 包装结果
                var sr = new SendResult
                {
                    Status = SendStatus.SendOK,
                    Queue = mq
                };
                sr.Read(rs.Header?.ExtFields);

                return sr;
            }
            catch (Exception ex)
            {
                span?.SetError(ex, msg);

                throw;
            }
        }
Exemplo n.º 3
0
        /// <summary>发送消息</summary>
        /// <param name="msg"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public virtual SendResult Send(Message msg, Int32 timeout = -1)
        {
            // 选择队列分片
            var mq = _NameServer.SelectQueue();

            mq.Topic = Topic;

            // 构造请求头
            var ts   = DateTime.Now - _dt1970;
            var smrh = new SendMessageRequestHeader
            {
                ProducerGroup  = Group,
                Topic          = Topic,
                QueueId        = mq.QueueId,
                SysFlag        = 0,
                BornTimestamp  = (Int64)ts.TotalMilliseconds,
                Flag           = msg.Flag,
                Properties     = msg.GetProperties(),
                ReconsumeTimes = 0,
                UnitMode       = UnitMode,
            };

            // 根据队列获取Broker客户端
            var bk = GetBroker(mq.BrokerName);
            var rs = bk.Invoke(RequestCode.SEND_MESSAGE_V2, msg.Body, smrh.GetProperties());

            // 包装结果
            var sr = new SendResult
            {
                Status = SendStatus.SendOK,
                Queue  = mq
            };

            sr.Read(rs.Header.ExtFields);

            return(sr);
        }