AWS中SQS的几项使用注意
AWS中的SQS收发message时,有几点需要注意:
1.message可以有参数,参数的类型锁定在“Binary”,“Number”,“String”三类。
2.message的参数有个数限制,限制在10个(小于或等于10个)。
3.鉴于参数个数限制,他的大小应当也是有限制的(尚未测出)。
4.超过10个参数怎么办?
将几个参数合并为dictionary,后转成binary存储。
1 Dictionary<string, int> dicIntParat = new Dictionary<string, int>(); 2 dicIntParat["DataDownloadID"] = _dataDownload.DataDownloadID; 3 dicIntParat["CreateUserID"] = _dataDownload.CreateUserID; 4 dicIntParat["TopicClientID"] = _dataDownload.TopicClientID; 5 dicIntParat["ProcessStatusID"] = _dataDownload.ProcessStatusID; 6 MemoryStream msNumber = new MemoryStream(); 7 BinaryFormatter bf = new BinaryFormatter(); 8 bf.Serialize(msNumber, dicIntParat); 9 10 MessageAttributeValue DataDownloadIDValue = new MessageAttributeValue(); 11 DataDownloadIDValue.DataType = "Binary"; 12 DataDownloadIDValue.BinaryValue = msNumber; 13 sendMessageRequest.MessageAttributes["DataDownloadID"] = DataDownloadIDValue;
读取message时,也相应的将Binary转换成dictionary后取值。
1 MemoryStream msNumber = new MemoryStream(); 2 BinaryFormatter bf = new BinaryFormatter(); 3 msNumber = result.Messages[i].MessageAttributes["DataDownloadID"].BinaryValue; 4 Dictionary<string, int> dicIntParat = (Dictionary<string, int>)bf.Deserialize(msNumber); 5 6 dd.DataDownloadID = dicIntParat["DataDownloadID"]; 7 dd.CreateUserID = dicIntParat["CreateUserID"]; 8 dd.TopicClientID = dicIntParat["TopicClientID"]; 9 dd.ProcessStatusID = dicIntParat["ProcessStatusID"];