public void StartMns(string _topicName)
        {
            try
            {
                IMNS client = new Aliyun.MNS.MNSClient(_accessKeyId, _secretAccessKey, _endpoint);

                var nativeQueue           = client.GetNativeQueue(_topicName);
                var receiveMessageRequest = new ReceiveMessageRequest();
                receiveMessageRequest.WaitSeconds = 30;

                while (isRunning)
                {
                    try
                    {
                        nativeQueue.BeginReceiveMessage(receiveMessageRequest, ReceiveMessageCallback, nativeQueue);
                        _autoSetEvent.WaitOne();
                    }
                    catch (MNSException me)
                    {
                        throw new ArgumentException("1.接受MNS数据:" + me.Message + me.StackTrace);
                    }
                }
            }
            catch (Exception err)
            {
                throw new ArgumentException("接受MNS数据:" + err.Message + err.StackTrace);
            }
        }
示例#2
0
 /// 处理消息
 public void Handle()
 {
     while (true)
     {
         try
         {
             QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO token = null;
             Queue queue = null;
             lock (o)
             {
                 if (tokenMap.ContainsKey(messageType))
                 {
                     token = tokenMap[messageType];
                 }
                 if (queueMap.ContainsKey(queueName))
                 {
                     queue = queueMap[queueName];
                 }
                 TimeSpan ts = new TimeSpan(0);
                 if (token != null)
                 {
                     DateTime b = Convert.ToDateTime(token.ExpireTime);
                     DateTime c = Convert.ToDateTime(DateTime.Now);
                     ts = b - c;
                 }
                 if (token == null || ts.TotalSeconds < bufferTime || queue == null)
                 {
                     token = GetTokenByMessageType(acsClient, messageType);
                     IMNS client = new Aliyun.MNS.MNSClient(token.AccessKeyId, token.AccessKeySecret, mnsAccountEndpoint, token.SecurityToken);
                     queue = client.GetNativeQueue(queueName);
                     if (tokenMap.ContainsKey(messageType))
                     {
                         tokenMap.Remove(messageType);
                     }
                     if (queueMap.ContainsKey(queueName))
                     {
                         queueMap.Remove(queueName);
                     }
                     tokenMap.Add(messageType, token);
                     queueMap.Add(queueName, queue);
                 }
             }
             BatchReceiveMessageResponse batchReceiveMessageResponse = queue.BatchReceiveMessage(16);
             List <Message> messages = batchReceiveMessageResponse.Messages;
             for (int i = 0; i <= messages.Count - 1; i++)
             {
                 try
                 {
                     byte[] outputb = Convert.FromBase64String(messages[i].Body);
                     string orgStr  = Encoding.UTF8.GetString(outputb);
                     System.Console.WriteLine(orgStr);
                     //TODO 具体消费逻辑,待客户自己实现.
                     //消费成功的前提下删除消息
                     queue.DeleteMessage(messages[i].ReceiptHandle);
                 }
                 catch (Exception e)
                 {
                     System.Console.WriteLine("Hello World!", e);
                 }
             }
         }
         catch (Exception e)
         {
             System.Console.WriteLine("Handle exception", e);
         }
         Thread.Sleep(sleepTime);
     }
 }