Ejemplo n.º 1
0
        public async ValueTask <string> PublishAsync(byte[] data, int offset, int count, string topic, IEnumerable <string> tags, MessageTopicPublishOptions options = null, CancellationToken cancellation = default)
        {
            if (data == null || data.Length == 0)
            {
                return(null);
            }

            if (offset < 0 || offset > data.Length - 1)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

            if (count < 1)
            {
                count = data.Length - offset;
            }
            else
            {
                if (offset + count > data.Length)
                {
                    throw new ArgumentOutOfRangeException(nameof(count));
                }
            }

            var response = await _http.PostAsync(MESSAGE_SEND_URL, CreateMessageRequest(data, offset, count, tags), cancellation);

            if (cancellation.IsCancellationRequested)
            {
                return(null);
            }

            var content = await response.Content.ReadAsStreamAsync(cancellation);

            return(MessageUtility.GetMessageResponseId(content));
        }
Ejemplo n.º 2
0
        public TopicInfo GetInfo(bool refresh = false)
        {
            if (refresh || _info == null)
            {
                var response = _http.GetAsync(MESSAGE_SEND_URL);

                if (response.Result.IsSuccessStatusCode)
                {
                    _info = MessageUtility.ResolveTopicInfo(response.Result.Content.ReadAsStreamAsync().Result);
                }
                else
                {
                    _info = null;
                }
            }

            return(_info);
        }
Ejemplo n.º 3
0
        public async ValueTask <string> EnqueueAsync(byte[] data, MessageEnqueueOptions options = null, CancellationToken cancellation = default)
        {
            if (options == null)
            {
                options = MessageEnqueueOptions.Default;
            }

            if (options.Priority < 1)
            {
                options.Priority = 1;
            }
            else if (options.Priority > 16)
            {
                options.Priority = 16;
            }

            if (options.Delay.TotalDays > 7)
            {
                options.Delay = TimeSpan.FromDays(7);
            }

            var text = @"<Message xmlns=""http://mqs.aliyuncs.com/doc/v1/""><MessageBody>" +
                       System.Convert.ToBase64String(data) +
                       "</MessageBody><DelaySeconds>" +
                       options.Delay.TotalSeconds.ToString() +
                       "</DelaySeconds><Priority>" + options.Priority.ToString() + "</Priority></Message>";

            var request = new HttpRequestMessage(HttpMethod.Post, this.GetRequestUrl("messages"))
            {
                Content = new StringContent(text, Encoding.UTF8, "text/xml")
            };

            request.Headers.Add("x-mns-version", "2015-06-06");

            var response = await _http.SendAsync(request, cancellation);

            if (!response.IsSuccessStatusCode)
            {
                Zongsoft.Diagnostics.Logger.Warn("[" + response.StatusCode + "] The message enqueue failed." + Environment.NewLine + await response.Content.ReadAsStringAsync());
                return(null);
            }

            return(MessageUtility.GetMessageResponseId(await response.Content.ReadAsStreamAsync(cancellation)));
        }
Ejemplo n.º 4
0
        public static ICertificate GetCertificate(string name)
        {
            var options     = MessageUtility.GetOptions();
            var certificate = string.Empty;

            if (options.Topics.TryGet(name, out var option))
            {
                certificate = option.Certificate;
            }

            if (string.IsNullOrWhiteSpace(certificate))
            {
                certificate = options.Topics.Certificate;
            }

            if (string.IsNullOrWhiteSpace(certificate))
            {
                return(Aliyun.Options.GeneralOptions.Instance.Certificates.Default);
            }

            return(Aliyun.Options.GeneralOptions.Instance.Certificates.Get(certificate));
        }
Ejemplo n.º 5
0
        public static string GetRequestUrl(string topicName, params string[] parts)
        {
            var options = MessageUtility.GetOptions();
            var region  = options.Topics.Region ?? Aliyun.Options.GeneralOptions.Instance.Name;

            if (options.Topics.TryGet(topicName, out var option) && option.Region.HasValue)
            {
                region = option.Region.Value;
            }

            var center = ServiceCenter.GetInstance(region, Aliyun.Options.GeneralOptions.Instance.IsIntranet);

            var path = parts == null ? string.Empty : string.Join("/", parts);

            if (string.IsNullOrEmpty(path))
            {
                return(string.Format("http://{0}.{1}/topics/{2}", options.Name, center.Path, topicName));
            }
            else
            {
                return(string.Format("http://{0}.{1}/topics/{2}/{3}", options.Name, center.Path, topicName, path));
            }
        }
Ejemplo n.º 6
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);
        }