Exemplo n.º 1
0
        public void Start()
        {
            #region Queue Releated Test Cases

            /* 1.1. Async create queue */
            var createQueueRequest = new CreateQueueRequest
            {
                QueueName  = _queueName,
                Attributes =
                {
                    DelaySeconds           =     10,
                    VisibilityTimeout      =     30,
                    MaximumMessageSize     =  40960,
                    MessageRetentionPeriod = 345600,
                    PollingWaitSeconds     = 15
                }
            };

            try
            {
                _client.BeginCreateQueue(createQueueRequest, CreateQueueCallback, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Create queue failed, exception info: " + ex.Message);
            }

            _autoSetEvent.WaitOne();

            /* 2.1 Async get queue attributes */
            try
            {
                var nativeQueue = _client.GetNativeQueue(_queueName);
                var getQueueAttributesRequest = new GetQueueAttributesRequest();
                nativeQueue.BeginGetAttributes(getQueueAttributesRequest, GetQueueAttributesCallback, nativeQueue);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Get queue attributes failed, exception info: " + ex.Message);
            }

            _autoSetEvent.WaitOne();

            /* 3. Async list queue */
            try
            {
                do
                {
                    var listQueueRequest = new ListQueueRequest
                    {
                        QueueNamePrefix = _queueNamePrefix,
                        Marker          = _nextMarker,
                        MaxReturns      = 5
                    };

                    _client.BeginListQueue(listQueueRequest, ListQueueCallback, null);

                    _autoSetEvent.WaitOne();
                } while (_nextMarker != string.Empty);
            }
            catch (Exception ex)
            {
                Console.WriteLine("List queue failed, exception info: " + ex.Message);
            }

            /* 4. Async set queue attributes */
            var setQueueAttributesRequest = new SetQueueAttributesRequest
            {
                Attributes =
                {
                    DelaySeconds           =     0,
                    VisibilityTimeout      =    10,
                    MaximumMessageSize     = 10240,
                    PollingWaitSeconds     =    10,
                    MessageRetentionPeriod = 50000
                }
            };
            try
            {
                var nativeQueue = _client.GetNativeQueue(_queueName);
                nativeQueue.BeginSetAttributes(setQueueAttributesRequest, SetQueueAttributesCallback, nativeQueue);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Set queue attributes failed, exception info: " + ex.Message);
            }

            _autoSetEvent.WaitOne();

            /* 2.2 Async get queue attributes again */
            try
            {
                var nativeQueue = _client.GetNativeQueue(_queueName);
                var getQueueAttributesRequest = new GetQueueAttributesRequest();
                nativeQueue.BeginGetAttributes(getQueueAttributesRequest, GetQueueAttributesCallback, nativeQueue);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Get queue attributes failed again, exception info: " + ex.Message);
            }

            _autoSetEvent.WaitOne();

            /* 5.1. Async delete queue */
            var deleteQueueRequest = new DeleteQueueRequest(_queueName);
            deleteQueueRequest.AddHeader("Accept", "IE6");
            try
            {
                _client.BeginDeleteQueue(deleteQueueRequest, DeleteQueueCallback, _queueName);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Delete queue failed, exception info: " + ex.Message);
            }

            _autoSetEvent.WaitOne();

            /* 1.2. Async create queue again */
            try
            {
                _client.BeginCreateQueue(createQueueRequest, CreateQueueCallback, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Create queue failed again, exception info: " + ex.Message);
            }

            _autoSetEvent.WaitOne();

            #endregion

            #region Messge Releated Test Cases

            /* 5. Async receive message */
            try
            {
                var nativeQueue = _client.GetNativeQueue(_queueName);
                for (int i = 0; i < _receiveTimes; i++)
                {
                    var receiveMessageRequest = new ReceiveMessageRequest();
                    nativeQueue.BeginReceiveMessage(receiveMessageRequest, ReceiveMessageCallback, nativeQueue);

                    _autoSetEvent.WaitOne();

                    Thread.Sleep(_receiveInterval);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Receive message failed, exception info: " + ex.Message);
            }

            /* 6. Async send message */
            try
            {
                var nativeQueue        = _client.GetNativeQueue(_queueName);
                var sendMessageRequest = new SendMessageRequest("阿里云计算");
                nativeQueue.BeginSendMessage(sendMessageRequest, SendMessageCallback, nativeQueue);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Send message failed, exception info: " + ex.Message);
            }

            _autoSetEvent.WaitOne();

            /* 7. Async receive message */
            try
            {
                var nativeQueue = _client.GetNativeQueue(_queueName);
                for (int i = 0; i < _receiveTimes; i++)
                {
                    var receiveMessageRequest = new ReceiveMessageRequest();
                    nativeQueue.BeginReceiveMessage(receiveMessageRequest, ReceiveMessageCallback, nativeQueue);

                    _autoSetEvent.WaitOne();

                    Thread.Sleep(_receiveInterval);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Receive message failed, exception info: " + ex.Message);
            }

            /* 8. Async change message visibility */
            try
            {
                var nativeQueue = _client.GetNativeQueue(_queueName);
                var changeMessageVisibilityRequest = new ChangeMessageVisibilityRequest
                {
                    ReceiptHandle     = _receiptHandle,
                    VisibilityTimeout = 5
                };
                nativeQueue.BeginChangeMessageVisibility(changeMessageVisibilityRequest, ChangeMessageVisibilityCallback, nativeQueue);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Change message visibility failed, exception info: " + ex.Message);
            }

            _autoSetEvent.WaitOne();

            /* 9. Async peek message */
            try
            {
                var nativeQueue        = _client.GetNativeQueue(_queueName);
                var peekMessageRequest = new PeekMessageRequest();
                for (uint i = 0; i < _receiveTimes; i++)
                {
                    nativeQueue.BeginPeekMessage(peekMessageRequest, PeekMessageCallback, nativeQueue);

                    _autoSetEvent.WaitOne();

                    Thread.Sleep(_receiveInterval);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Peek message failed, exception info: " + ex.Message);
            }

            /* 10. Async delete message */
            try
            {
                var nativeQueue            = _client.GetNativeQueue(_queueName);
                var receiveMessageResponse = nativeQueue.ReceiveMessage();
                _receiptHandle = receiveMessageResponse.Message.ReceiptHandle;
                var deleteMessageRequest = new DeleteMessageRequest(_receiptHandle);
                nativeQueue.BeginDeleteMessage(deleteMessageRequest, DeleteMessageCallback, nativeQueue);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Async BeginDeleteMessage failed, exception info: " + ex.Message);
            }

            _autoSetEvent.WaitOne();

            /* 11. Async batch send message */
            try
            {
                var nativeQueue = _client.GetNativeQueue(_queueName);
                List <SendMessageRequest> requests = new List <SendMessageRequest>();
                requests.Add(new SendMessageRequest("阿里云计算 Priority1", 0, 1));
                for (int i = 0; i < _batchSize; i++)
                {
                    requests.Add(new SendMessageRequest("阿里云计算" + i.ToString()));
                }
                BatchSendMessageRequest batchSendRequest = new BatchSendMessageRequest()
                {
                    Requests = requests
                };
                nativeQueue.BeginBatchSendMessage(batchSendRequest, BatchSendMessageCallback, nativeQueue);

                _autoSetEvent.WaitOne();
            }
            catch (Exception ex)
            {
                Console.WriteLine("BeginBatchSend message failed, exception info: " + ex.Message);
            }

            Thread.Sleep(12000);

            /* 12. Async batch peek message */
            try
            {
                var nativeQueue             = _client.GetNativeQueue(_queueName);
                var batchPeekMessageRequest = new BatchPeekMessageRequest(_batchSize + 2);
                Console.WriteLine(batchPeekMessageRequest.BatchSize.ToString());
                nativeQueue.BeginBatchPeekMessage(batchPeekMessageRequest, BatchPeekMessageCallback, nativeQueue);

                _autoSetEvent.WaitOne();
            }
            catch (Exception ex)
            {
                Console.WriteLine("BeginBatchPeek message failed, exception info: " + ex.Message);
            }

            /* 13. Async batch receive message */
            try
            {
                var nativeQueue = _client.GetNativeQueue(_queueName);
                BatchReceiveMessageRequest batchReceiveMessageRequest = new BatchReceiveMessageRequest(_batchSize + 1, 3);
                var batchReceiveMessageResponse = nativeQueue.BeginBatchReceiveMessage(batchReceiveMessageRequest, BatchReceiveMessageCallback, nativeQueue);
                _autoSetEvent.WaitOne();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Batch receive message failed, exception info: " + ex.Message);
            }

            /* 14. Async batch delete message */
            if (_batchReceiveMessageResponse != null && _batchReceiveMessageResponse.Messages.Count > 0)
            {
                try
                {
                    var           nativeQueue    = _client.GetNativeQueue(_queueName);
                    List <string> receiptHandles = new List <string>();
                    foreach (var message in _batchReceiveMessageResponse.Messages)
                    {
                        receiptHandles.Add(message.ReceiptHandle);
                    }
                    var batchDeleteMessageRequest = new BatchDeleteMessageRequest()
                    {
                        ReceiptHandles = receiptHandles
                    };
                    var batchDeleteMessageResponse = nativeQueue.BeginBatchDeleteMessage(batchDeleteMessageRequest, BatchDeleteMessageCallback, nativeQueue);
                    _autoSetEvent.WaitOne();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Batch delete message failed, exception info: " + ex.Message);
                }
            }

            #endregion

            #region Clean Generated Queue

            /* 5.2. Async delete queue again */
            try
            {
                _client.BeginDeleteQueue(deleteQueueRequest, DeleteQueueCallback, _queueName);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Delete queue failed again, exception info: " + ex.Message);
            }

            _autoSetEvent.WaitOne();

            #endregion

            Console.ReadKey();
        }