Contains the details of a single Amazon SQS message along with an Id.
        // Use this to manually test the performance / throttling of getting messages out of the queue.
        public void HandlingManyMessages(int throttleMessageCount)
        {
            var locker = new object();
            var awsQueueClient = CreateMeABus.DefaultClientFactory().GetSqsClient(RegionEndpoint.EUWest1);
 
            var q = new SqsQueueByName(RegionEndpoint.EUWest1, "throttle_test", awsQueueClient, 1);
            if (!q.Exists())
            {
                q.Create(new SqsBasicConfiguration());
                Thread.Sleep(TimeSpan.FromMinutes(1));  // wait 60 secs for queue creation to be guaranteed completed by aws. :(
            }

            Assert.True(q.Exists());

            Console.WriteLine("{0} - Adding {1} messages to the queue.", DateTime.Now, throttleMessageCount);

            var entriesAdded = 0;
            // Add some messages
            do
            {
                var entries = new List<SendMessageBatchRequestEntry>();
                for (var j = 0; j < 10; j++)
                {
                    var batchEntry = new SendMessageBatchRequestEntry
                                         {
                                             MessageBody = "{\"Subject\":\"GenericMessage\", \"Message\": \"" + entriesAdded.ToString() + "\"}",
                                             Id = Guid.NewGuid().ToString()
                                         };
                    entries.Add(batchEntry);
                    entriesAdded++;
                }
                awsQueueClient.SendMessageBatch(new SendMessageBatchRequest { QueueUrl = q.Url, Entries = entries });
            }
            while (entriesAdded < throttleMessageCount);

            Console.WriteLine("{0} - Done adding messages.", DateTime.Now);
            
            var handleCount = 0;
            var serialisations = Substitute.For<IMessageSerialisationRegister>();
            var monitor = Substitute.For<IMessageMonitor>();
            var handler = Substitute.For<IHandler<GenericMessage>>();
            handler.Handle(null).ReturnsForAnyArgs(true).AndDoes(x => {lock (locker) { handleCount++; } });

            serialisations.DeserializeMessage(string.Empty).ReturnsForAnyArgs(new GenericMessage());
            var listener = new SqsNotificationListener(q, serialisations, monitor);
            listener.AddMessageHandler(() => handler);

            var stopwatch = new Stopwatch();
            stopwatch.Start();
            listener.Listen();
            var waitCount = 0;
            do
            {
                Thread.Sleep(TimeSpan.FromSeconds(5));
                Console.WriteLine("{0} - Handled {1} messages. Waiting for completion.", DateTime.Now, handleCount);
                waitCount++;
            }
            while (handleCount < throttleMessageCount && waitCount < 100);

            listener.StopListening();
            stopwatch.Stop();

            Console.WriteLine("{0} - Handled {1} messages.", DateTime.Now, handleCount);
            Console.WriteLine("{0} - Took {1} ms", DateTime.Now, stopwatch.ElapsedMilliseconds);
            Console.WriteLine("{0} - Throughput {1} msg/sec", DateTime.Now, (float)handleCount / stopwatch.ElapsedMilliseconds * 1000);
            Assert.AreEqual(throttleMessageCount, handleCount);
        }
Beispiel #2
0
        public Task Send(string destinationAddress, TransportMessage message, ITransactionContext context)
        {
            if (destinationAddress == null) throw new ArgumentNullException("destinationAddress");
            if (message == null) throw new ArgumentNullException("message");
            if (context == null) throw new ArgumentNullException("context");

            var outputQueue = context.GetOrAdd(OutgoingQueueContextKey, () => new InMemOutputQueue());
            var contextActionsSet = context.GetOrAdd(OutgoingQueueContextActionIsSetKey, () => false);

            if (!contextActionsSet)
            {
                context.OnCommitted(async () =>
                                          {

                                              var client = GetClientFromTransactionContext(context);
                                              var messageSendRequests = outputQueue.GetMessages();
                                              var tasks = messageSendRequests.Select(r => client.SendMessageBatchAsync(new SendMessageBatchRequest(r.DestinationAddressUrl, r.Messages.ToList())));

                                              var response = await Task.WhenAll(tasks);
                                              if (response.Any(r => r.Failed.Any()))
                                              {
                                                  GenerateErrorsAndThrow(response);
                                              }

                                          });
                context.OnAborted(outputQueue.Clear);

                context.Items[OutgoingQueueContextActionIsSetKey] = true;
            }

            var sendMessageRequest = new SendMessageBatchRequestEntry()
                                     {

                                         MessageAttributes = CreateAttributesFromHeaders(message.Headers),
                                         MessageBody = GetBody(message.Body),
                                         Id = message.Headers.GetValueOrNull(Headers.MessageId) ?? Guid.NewGuid().ToString(),
                                     };

            outputQueue.AddMessage(GetDestinationQueueUrlByName(destinationAddress, context), sendMessageRequest);

            return _emptyTask;
        }
Beispiel #3
0
    public static void SQSSendMessageBatch()
    {
      #region SQSSendMessageBatch
      var client = new AmazonSQSClient();

      var entry1 = new SendMessageBatchRequestEntry
      {
        DelaySeconds = 0,
        Id = "Entry1",
        MessageAttributes = new Dictionary<string, MessageAttributeValue>
        {
          {
            "MyNameAttribute", new MessageAttributeValue 
              { DataType = "String", StringValue = "John Doe" }
          },
          {
            "MyAddressAttribute", new MessageAttributeValue 
              { DataType = "String", StringValue = "123 Main St." }
          },
          {
            "MyRegionAttribute", new MessageAttributeValue 
              { DataType = "String", StringValue = "Any Town, United States" }
          }
        },
        MessageBody = "John Doe customer information."
      };

      var entry2 = new SendMessageBatchRequestEntry
      {
        DelaySeconds = 0,
        Id = "Entry2",
        MessageAttributes = new Dictionary<string, MessageAttributeValue>
        {
          {
            "MyNameAttribute", new MessageAttributeValue 
              { DataType = "String", StringValue = "Jane Doe" }
          },
          {
            "MyAddressAttribute", new MessageAttributeValue 
              { DataType = "String", StringValue = "456 Center Road" }
          },
          {
            "MyRegionAttribute", new MessageAttributeValue 
              { DataType = "String", StringValue = "Any City, United States" }
          }
        },
        MessageBody = "Jane Doe customer information."
      };

      var entry3 = new SendMessageBatchRequestEntry
      {
        DelaySeconds = 0,
        Id = "Entry3",
        MessageAttributes = new Dictionary<string, MessageAttributeValue>
        {
          {
            "MyNameAttribute", new MessageAttributeValue 
              { DataType = "String", StringValue = "Richard Doe" }
          },
          {
            "MyAddressAttribute", new MessageAttributeValue 
              { DataType = "String", StringValue = "789 East Blvd." }
          },
          {
            "MyRegionAttribute", new MessageAttributeValue 
              { DataType = "String", StringValue = "Anywhere, United States" }
          }
        },
        MessageBody = "Richard Doe customer information."
      };

      var request = new SendMessageBatchRequest
      {
        Entries = new List<SendMessageBatchRequestEntry>() { entry1, entry2, entry3 },
        QueueUrl = "https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyTestQueue"
      };

      var response = client.SendMessageBatch(request);

      if (response.Successful.Count > 0)
      {
        Console.WriteLine("Successfully sent:");

        foreach (var success in response.Successful)
        {
          Console.WriteLine("  For ID: '" + success.Id + "':");
          Console.WriteLine("    Message ID = " + success.MessageId);
          Console.WriteLine("    MD5 of message attributes = " +
            success.MD5OfMessageAttributes);
          Console.WriteLine("    MD5 of message body = " +
            success.MD5OfMessageBody);
        }
      }

      if (response.Failed.Count > 0)
      {
        Console.WriteLine("Failed to be sent:");

        foreach (var fail in response.Failed)
        {
          Console.WriteLine("  For ID '" + fail.Id + "':");
          Console.WriteLine("    Code = " + fail.Code);
          Console.WriteLine("    Message = " + fail.Message);
          Console.WriteLine("    Sender's fault? = " +
            fail.SenderFault);
        }
      }
      #endregion

      Console.ReadLine();
    }