예제 #1
0
        public Subscriber(string name, Uri queueUrl, string queueArn, bool longLived)
        {
            name.Requires("name").IsNotNull();
            queueUrl.Requires("queueUrl").IsNotNull();
            queueArn.Requires("queueArn").IsNotNull();

            Name = name;
            QueueUrl = queueUrl;
            QueueArn = queueArn;
            LongLived = longLived;
        }
        public string GetQueueArn(Uri queueUrl)
        {
            queueUrl.Requires("queueUrl").IsNotNull();

            var attributes = new List<string> {QueueAttributeName.QueueArn};
            var attributeRequest = new GetQueueAttributesRequest(queueUrl.AbsoluteUri, attributes);
            using (var sqs = amazonSqsFactory())
            {
                var queueAttributes = sqs.GetQueueAttributes(attributeRequest);
                return queueAttributes.Attributes[QueueAttributeName.QueueArn];
            }
        }
        public string DeleteMessage(Uri queueUrl, string receiptHandle)
        {
            queueUrl.Requires("queueUrl").IsNotNull();
            receiptHandle.Requires("receiptHandle").IsNotNullOrWhiteSpace();

            var deleteMessageRequest = new DeleteMessageRequest(queueUrl.AbsoluteUri, receiptHandle);
            using (var sqs = amazonSqsFactory())
            {
                var deleteResponseMessage = sqs.DeleteMessage(deleteMessageRequest);
                return deleteResponseMessage.ResponseMetadata.RequestId;
            }
        }
        public string DeleteQueue(Uri queueUri)
        {
            queueUri.Requires("queueUri").IsNotNull();

            var sqsDeleteRequest = new DeleteQueueRequest(queueUri.AbsoluteUri);
            try
            {
                using (var sqs = amazonSqsFactory())
                {
                    var deleteQueueResponse = sqs.DeleteQueue(sqsDeleteRequest);
                    return deleteQueueResponse.ResponseMetadata.RequestId;
                }
            }
            catch (AmazonSQSException)
            {
                return null;
            }
        }
        public IEnumerable<Message> ReadQueue(Uri queueUrl)
        {
            queueUrl.Requires("queueUrl").IsNotNull();

            var receiveMessageRequest = new ReceiveMessageRequest(queueUrl.AbsoluteUri);
            using (var sqs = amazonSqsFactory())
            {
                var response = sqs.ReceiveMessage(receiveMessageRequest);
                return response.Messages;
            }
        }
        public string SetSqsPolicyForSnsPublish(Uri queueUrl, string queueArn, string mytopicArn)
        {
            queueUrl.Requires("queueUrl").IsNotNull();
            queueArn.Requires("queueArn").IsNotNullOrWhiteSpace();
            mytopicArn.Requires("mytopicArn").IsNotNullOrWhiteSpace();

            var sqsPolicy = new Policy().WithStatements(
                new Statement(Statement.StatementEffect.Allow)
                    .WithResources(new Resource(queueArn))
                    .WithPrincipals(Principal.AllUsers)
                    .WithActionIdentifiers(SQSActionIdentifiers.SendMessage)
                    .WithConditions(ConditionFactory.NewCondition(ConditionFactory.ArnComparisonType.ArnEquals,
                        ConditionSourceArn, mytopicArn)));

            var attributes = new Dictionary<string, string>
            {
                {QueueAttributeName.Policy, sqsPolicy.ToJson()}
            };

            var setQueueAttributesRequest = new SetQueueAttributesRequest(queueUrl.AbsoluteUri, attributes);
            using (var sqs = amazonSqsFactory())
            {
                var response = sqs.SetQueueAttributes(setQueueAttributesRequest);
                return response.ResponseMetadata.RequestId;
            }
        }