Esempio 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));
        }
Esempio n. 2
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)));
        }