2014-05-09 5 views
6

Mam proces, który tworzy komunikaty SQS i umieszcza je w kolejce SQS oraz innym procesie, który odczytuje te wiadomości i wykonuje określoną logikę na podstawie zawartości treści i atrybutów wiadomości.Nie można uzyskać dostępu do atrybutów wiadomości w usłudze Amazon SQS w języku C#

Mogę pomyślnie utworzyć wiadomość w kolejce SQS z treścią i atrybutami, ale mam problem z odczytaniem atrybutów wiadomości!

Jestem pewien, że mój proces tworzenia wiadomości jest poprawny, mogę zobaczyć atrybuty w konsoli AWS SQS dla kolejki. Po prostu nie mogę zrozumieć, dlaczego nie mogę odczytać tych atrybutów.

Mój kod, aby utworzyć wiadomość:

IAmazonSQS sqs = AWSClientFactory.CreateAmazonSQSClient(); 
var sendMessageRequest = new SendMessageRequest(); 
sendMessageRequest.QueueUrl = "myURL"; 
Dictionary<string, MessageAttributeValue> MessageAttributes = new Dictionary<string, MessageAttributeValue>(); 
MessageAttributeValue messageTypeAttribute = new MessageAttributeValue(); 
messageTypeAttribute.DataType = "String"; 
messageTypeAttribute.StringValue = "HIGH"; 
MessageAttributes.Add("MESSAGEPRIORITY", messageTypeAttribute); 

sendMessageRequest.MessageAttributes = MessageAttributes; 
sendMessageRequest.MessageBody = "Thats the message body"; 

sqs.SendMessage(sendMessageRequest); 

Powyższe prace i wiadomości z atrybutu MESSAGEPRIORITY = HIGH jest tworzony (widzę wiadomość i atrybut w konsoli SQS).

Podczas czytania z powrotem wiadomość (mam pominięte fragmenty kodu, które pokazują, kolejki itp) URL:

IAmazonSQS sqs = AWSClientFactory.CreateAmazonSQSClient(); 
ReceiveMessageResponse receiveMessage = new ReceiveMessageResponse(); 
ReceiveMessageRequest request = new ReceiveMessageRequest(); 
request.QueueUrl = "myURL"; 
receiveMessage = sqs.ReceiveMessage(request); 
string messageBody = receiveMessage.Messages[0].Body; 
Dictionary<string, MessageAttributeValue> messageAttributes = receiveMessage.Messages[0].MessageAttributes; 

Z powyższego kodu, otrzymuję treść wiadomości, ale atrybut pusty ! Mam ten sam problem podczas ręcznego tworzenia wiadomości, używając bezpośrednio kolejki SQS. Nie mogę zrozumieć, dlaczego? Każda pomoc będzie świetna. Wielkie dzięki,

Odpowiedz

15

Ok, więc wymyśliłem to. Nazwy atrybutów należy określić jako właściwość obiektu ReceiveMessageRequest przed wywołaniem, aby pobrać wiadomość.

więc powyższy kod musi się zmienić, aby:

IAmazonSQS sqs = AWSClientFactory.CreateAmazonSQSClient(); 
ReceiveMessageResponse receiveMessage = new ReceiveMessageResponse(); 
ReceiveMessageRequest request = new ReceiveMessageRequest(); 

//Specify attribute list 
List<string> AttributesList = new List<string>(); 
AttributesList.Add("MESSAGEPRIORITY"); 

//Assign list and QueueURL to request 
request.MessageAttributeNames = AttributesList; 
request.QueueUrl = "myURL"; 

//Receive the message... 
receiveMessage = sqs.ReceiveMessage(request); 
//Body... 
string messageBody = receiveMessage.Messages[0].Body; 
//...and attributes 
Dictionary<string, MessageAttributeValue> messageAttributes = receiveMessage.Messages[0].MessageAttributes; 

Powyższe prace dla mnie. Mam nadzieję, że będzie przydatna komuś ...

+0

dobry znaleźć, spędziłem myśląc miałem atrybuty nieprawidłowo ustawione. Możesz uprościć trochę rzeczy, wykonując w tekście listę MessageAttributeNames; 'request.MessageAttributeNames = new List () {" MESSAGEPRIORITY "};' trochę prostszy sposób, jeśli masz krótką listę atrybutów. – sixones

7

Aby pobrać wszystkie atrybuty wiadomości bez określania każdego z nich, możesz umieścić "*" lub "Wszystkie" na liście atrybutów. Tak:

//Specify attribute list 
List<string> AttributesList = new List<string>(); 
AttributesList.Add("*"); 

AWS SQS ReceiveMessage documentation