Beispiel #1
0
        public QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO GetTokenByMessageType(IAcsClient acsClient, String messageType)
        {
            QueryTokenForMnsQueueRequest request = new QueryTokenForMnsQueueRequest();

            request.MessageType = messageType;
            QueryTokenForMnsQueueResponse queryTokenForMnsQueueResponse = acsClient.GetAcsResponse(request);

            QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO token = queryTokenForMnsQueueResponse.MessageTokenDTO;
            return(token);
        }
Beispiel #2
0
        public static QueryTokenForMnsQueueResponse Unmarshall(UnmarshallerContext context)
        {
            QueryTokenForMnsQueueResponse queryTokenForMnsQueueResponse = new QueryTokenForMnsQueueResponse();

            queryTokenForMnsQueueResponse.HttpResponse = context.HttpResponse;
            queryTokenForMnsQueueResponse.RequestId    = context.StringValue("QueryTokenForMnsQueue.RequestId");
            queryTokenForMnsQueueResponse.Code         = context.StringValue("QueryTokenForMnsQueue.Code");
            queryTokenForMnsQueueResponse.Message      = context.StringValue("QueryTokenForMnsQueue.Message");

            QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO messageTokenDTO = new QueryTokenForMnsQueueResponse.QueryTokenForMnsQueue_MessageTokenDTO();
            messageTokenDTO.AccessKeyId     = context.StringValue("QueryTokenForMnsQueue.MessageTokenDTO.AccessKeyId");
            messageTokenDTO.AccessKeySecret = context.StringValue("QueryTokenForMnsQueue.MessageTokenDTO.AccessKeySecret");
            messageTokenDTO.SecurityToken   = context.StringValue("QueryTokenForMnsQueue.MessageTokenDTO.SecurityToken");
            messageTokenDTO.CreateTime      = context.StringValue("QueryTokenForMnsQueue.MessageTokenDTO.CreateTime");
            messageTokenDTO.ExpireTime      = context.StringValue("QueryTokenForMnsQueue.MessageTokenDTO.ExpireTime");
            queryTokenForMnsQueueResponse.MessageTokenDTO = messageTokenDTO;

            return(queryTokenForMnsQueueResponse);
        }
Beispiel #3
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);
     }
 }