示例#1
0
        private static void ReceiveMessage()
        {
            var mqClient =
                new MNSClient(AliConfig.Endpoint,
                              AliConfig.AccessKey, AliConfig.AccessKeySecret);

            while (true)
            {
                try
                {
                    MNSQueue queue = mqClient.GetQueue("toll-open");
                    MessageReceiveResponse message = queue.PopMessage();

                    if (!string.IsNullOrWhiteSpace(message.Code)) //有错误
                    {
                        Console.WriteLine(message.Message);
                        continue;
                    }

                    Console.WriteLine("收到消息Receive mesaage : {0}", message.MessageBody);
                    queue.DeleteMessage(message.ReceiptHandle);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Thread.Sleep(3000);
                }
            }
        }
        private static void SendMessage()
        {
            for (int i = 0; i < 20; i++)
            {
                try
                {
                    var mqClient =
                        new MNSClient(AliConfig.Endpoint, AliConfig.AccessKey, AliConfig.AccessKeySecret);
                    MNSQueue queue   = mqClient.GetQueue("toll-open");
                    string   message = string.Format("Hello World! <from {0}, No.{1}>", Thread.CurrentThread.ManagedThreadId,
                                                     i);
                    var result = queue.SendMessage(message);

                    Console.WriteLine("Send message : {0},return code {1}", message, result.Code);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }