Example #1
0
        private async Task <Zongsoft.Messaging.MessageBase> DequeueOrPeekAsync(int waitSeconds)
        {
            var request = new HttpRequestMessage(HttpMethod.Get, this.GetRequestUrl("messages") + (waitSeconds >= 0 ? "?waitseconds=" + waitSeconds.ToString() : "?peekonly=true"));

            request.Headers.Add("x-mns-version", "2015-06-06");
            var response = await _http.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                return(MessageUtility.ResolveMessage(this, await response.Content.ReadAsStreamAsync()));
            }

            var exception = AliyunException.Parse(await response.Content.ReadAsStringAsync());

            if (exception != null)
            {
                switch (exception.Code)
                {
                case MessageUtility.MessageNotExist:
                    return(null);

                case MessageUtility.QueueNotExist:
                    throw exception;

                default:
                    throw exception;
                }
            }

            return(null);
        }
Example #2
0
        public override async Task <DateTime> DelayAsync(TimeSpan duration)
        {
            if (string.IsNullOrEmpty(this.AcknowledgementId))
            {
                return(this.Expires);
            }

            var http    = _queue.Http;
            var request = new HttpRequestMessage(HttpMethod.Put, _queue.GetRequestUrl("messages") + "?ReceiptHandle=" + Uri.EscapeDataString(this.AcknowledgementId) + "&VisibilityTimeout=" + duration.TotalSeconds.ToString());

            request.Headers.Add("x-mns-version", "2015-06-06");
            var response = await http.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                var exception = AliyunException.Parse(await response.Content.ReadAsStringAsync());

                if (exception != null)
                {
                    throw exception;
                }

                response.EnsureSuccessStatusCode();
            }

            var matches = _delay_regex.Matches(await response.Content.ReadAsStringAsync());

            foreach (Match match in matches)
            {
                switch (match.Groups["tag"].Value)
                {
                case "ReceiptHandle":
                    this.AcknowledgementId = match.Groups["value"].Value;
                    break;

                case "NextVisibleTime":
                    this.Expires = Utility.GetDateTimeFromEpoch(match.Groups["value"].Value);
                    break;
                }
            }

            return(this.Expires);
        }
Example #3
0
        public async ValueTask DelayAsync(string acknowledgementId, TimeSpan duration, CancellationToken cancellation = default)
        {
            if (string.IsNullOrEmpty(acknowledgementId))
            {
                return;
            }

            var request = new HttpRequestMessage(HttpMethod.Put, this.GetRequestUrl("messages") + "?ReceiptHandle=" + Uri.EscapeDataString(acknowledgementId) + "&VisibilityTimeout=" + duration.TotalSeconds.ToString());

            request.Headers.Add("x-mns-version", "2015-06-06");
            var response = await _http.SendAsync(request, cancellation);

            if (!response.IsSuccessStatusCode)
            {
                var exception = AliyunException.Parse(await response.Content.ReadAsStringAsync(cancellation));

                if (exception != null)
                {
                    throw exception;
                }

                response.EnsureSuccessStatusCode();
            }

            var matches = DELAY_REGEX.Matches(await response.Content.ReadAsStringAsync(cancellation));

            foreach (Match match in matches)
            {
                switch (match.Groups["tag"].Value)
                {
                case "ReceiptHandle":
                    acknowledgementId = match.Groups["value"].Value;
                    break;

                case "NextVisibleTime":
                    var expiration = Utility.GetDateTimeFromEpoch(match.Groups["value"].Value);
                    break;
                }
            }
        }
Example #4
0
        public async ValueTask <MessageQueueMessage> DequeueAsync(MessageDequeueOptions options, CancellationToken cancellation = default)
        {
            if (options == null)
            {
                options = MessageDequeueOptions.Default;
            }

            var waitSeconds = (long)Math.Ceiling(options.Timeout.TotalSeconds);
            var request     = new HttpRequestMessage(HttpMethod.Get, this.GetRequestUrl("messages") + (options != null && options.Timeout >= TimeSpan.Zero ? "?waitseconds=" + waitSeconds.ToString() : string.Empty));

            request.Headers.Add("x-mns-version", "2015-06-06");
            var response = await _http.SendAsync(request, cancellation);

            if (response.IsSuccessStatusCode)
            {
                return(MessageUtility.ResolveMessage(this, await response.Content.ReadAsStreamAsync(cancellation)));
            }

            var exception = AliyunException.Parse(await response.Content.ReadAsStringAsync(cancellation));

            if (exception != null)
            {
                switch (exception.Code)
                {
                case MessageUtility.MessageNotExist:
                    return(MessageQueueMessage.Empty);

                case MessageUtility.QueueNotExist:
                    throw exception;

                default:
                    throw exception;
                }
            }

            return(MessageQueueMessage.Empty);
        }