예제 #1
0
        /// <summary>从指定队列拉取消息</summary>
        /// <param name="mq"></param>
        /// <param name="offset"></param>
        /// <param name="maxNums"></param>
        /// <returns></returns>
        public PullResult Pull(MessageQueue mq, Int64 offset, Int32 maxNums)
        {
            var header = new PullMessageRequestHeader
            {
                ConsumerGroup = Group,
                Topic         = Topic,
                QueueId       = mq.QueueId,
                QueueOffset   = offset,
                MaxMsgNums    = maxNums,
                SysFlag       = 6,
            };

            var dic = header.GetProperties();
            var bk  = GetBroker(mq.BrokerName);

            var rs = bk.Invoke(RequestCode.PULL_MESSAGE, null, dic);

            var pr = new PullResult
            {
                Status = PullStatus.Found,
            };

            pr.Read(rs.Header.ExtFields);

            return(pr);
        }
예제 #2
0
        /// <summary>从指定队列拉取消息</summary>
        /// <param name="mq"></param>
        /// <param name="offset"></param>
        /// <param name="maxNums"></param>
        /// <param name="msTimeout"></param>
        /// <returns></returns>
        public PullResult Pull(MessageQueue mq, Int64 offset, Int32 maxNums, Int32 msTimeout = -1)
        {
            var header = new PullMessageRequestHeader
            {
                ConsumerGroup = Group,
                Topic         = Topic,
                QueueId       = mq.QueueId,
                QueueOffset   = offset,
                MaxMsgNums    = maxNums,
                SysFlag       = 6,
                SubVersion    = StartTime.ToLong(),
            };

            if (msTimeout >= 0)
            {
                header.SuspendTimeoutMillis = msTimeout;
            }

            var st = _Queues.FirstOrDefault(e => e.Queue == mq);

            if (st != null)
            {
                header.CommitOffset = st.CommitOffset;
            }

            var dic = header.GetProperties();
            var bk  = GetBroker(mq.BrokerName);

            var rs = bk.Invoke(RequestCode.PULL_MESSAGE, null, dic, true);

            if (rs?.Header == null)
            {
                return(null);
            }

            var pr = new PullResult();

            if (rs.Header.Code == 0)
            {
                pr.Status = PullStatus.Found;
            }
            else if (rs.Header.Code == (Int32)ResponseCode.PULL_NOT_FOUND)
            {
                pr.Status = PullStatus.NoNewMessage;
            }
            else if (rs.Header.Code == (Int32)ResponseCode.PULL_OFFSET_MOVED)
            {
                pr.Status = PullStatus.OffsetIllegal;
            }

            pr.Read(rs.Header?.ExtFields);

            // 读取内容
            if (rs.Body != null && rs.Body.Length > 0)
            {
                pr.Messages = MessageExt.ReadAll(rs.Body).ToArray();
            }

            return(pr);
        }
예제 #3
0
        /// <summary>从指定队列拉取消息</summary>
        /// <param name="mq"></param>
        /// <param name="offset"></param>
        /// <param name="maxNums"></param>
        /// <param name="msTimeout"></param>
        /// <returns></returns>
        public PullResult Pull(MessageQueue mq, Int64 offset, Int32 maxNums, Int32 msTimeout = -1)
        {
            // 性能埋点
            using var span = Tracer?.NewSpan($"mq:{Topic}:Consume");
            try
            {
                var header = new PullMessageRequestHeader
                {
                    ConsumerGroup = Group,
                    Topic         = Topic,
                    QueueId       = mq.QueueId,
                    QueueOffset   = offset,
                    MaxMsgNums    = maxNums,
                    SysFlag       = 6,
                    SubVersion    = StartTime.ToLong(),
                };
                if (msTimeout >= 0)
                {
                    header.SuspendTimeoutMillis = msTimeout;
                }

                var st = _Queues.FirstOrDefault(e => e.Queue == mq);
                if (st != null)
                {
                    header.CommitOffset = st.CommitOffset;
                }

                var dic = header.GetProperties();
                var bk  = GetBroker(mq.BrokerName);

                var rs = bk.Invoke(RequestCode.PULL_MESSAGE, null, dic, true);
                if (rs?.Header == null)
                {
                    return(null);
                }

                var pr = new PullResult();

                if (rs.Header.Code == 0)
                {
                    pr.Status = PullStatus.Found;
                }
                else if (rs.Header.Code == (Int32)ResponseCode.PULL_NOT_FOUND)
                {
                    pr.Status = PullStatus.NoNewMessage;
                }
                else if (rs.Header.Code == (Int32)ResponseCode.PULL_OFFSET_MOVED || rs.Header.Code == (Int32)ResponseCode.PULL_RETRY_IMMEDIATELY)
                {
                    pr.Status = PullStatus.OffsetIllegal;
                }
                else
                {
                    pr.Status = PullStatus.Unknown;
                    Log.Warn("响应编号:{0} 响应备注:{1} 序列编号:{2} 序列偏移量:{3}", rs.Header.Code, rs.Header.Remark, mq.QueueId, offset);
                }

                pr.Read(rs.Header?.ExtFields);

                // 读取内容
                var pk = rs.Payload;
                if (pk != null)
                {
                    pr.Messages = MessageExt.ReadAll(pk).ToArray();
                }

                return(pr);
            }
            catch (Exception ex)
            {
                span?.SetError(ex, mq);

                throw;
            }
        }