コード例 #1
0
        /// <summary>
        /// 获取消息
        /// </summary>
        /// <param name="queueName">队列名称</param>
        /// <param name="autoAck">是否消息自动确认</param>
        /// <param name="beforeAckAction">手动确认消息前回调(自动确认消息时无效)</param>
        /// <returns></returns>
        public MessageAskModel GetMessage(string queueName, bool autoAck = true, Action <IModel, MessageAskModel> beforeAckAction = null)
        {
            using var channel = GetChannel();
            if (channel.MessageCount(queueName) == 0)
            {
                return(null);
            }
            var baseResult = channel.BasicGet(queueName, autoAck);

            if (baseResult == null)
            {
                return(null);
            }
            var message = new MessageAskModel()
            {
                DeliveryTag = baseResult.DeliveryTag,
                Message     = Encoding.UTF8.GetString(baseResult.Body)
            };

            if (autoAck == false)
            {
                beforeAckAction.Invoke(channel, message);
            }
            return(message);
        }
コード例 #2
0
        /// <summary>
        /// 获取消息
        /// </summary>
        /// <param name="queueName">队列名称</param>
        /// <param name="autoAck">是否消息自动确认</param>
        /// <returns></returns>
        private MessageAskModel GetMessage(string queueName, bool autoAck = true)
        {
            using var channel = GetChannel();
            if (channel.MessageCount(queueName) == 0)
            {
                return(null);
            }
            var baseResult = channel.BasicGet(queueName, autoAck);

            if (baseResult == null)
            {
                return(null);
            }
            var message = new MessageAskModel()
            {
                DeliveryTag = baseResult.DeliveryTag,
                Message     = Encoding.UTF8.GetString(baseResult.Body)
            };

            return(message);
        }