Beispiel #1
0
        public void DisposeRespectsTheBatchLock()
        {
            var mockBatch = new MockTransportBatch();
            var batch     = new ServiceBusMessageBatch(mockBatch);

            Assert.That(batch.TryAdd(new ServiceBusMessage(new byte[] { 0x21 })), Is.True, "The message should have been accepted before locking.");

            batch.Lock();
            Assert.That(() => batch.Dispose(), Throws.InstanceOf <InvalidOperationException>(), "The batch should not accept messages when locked.");

            batch.Unlock();
            batch.Dispose();

            Assert.That(mockBatch.DisposeInvoked, Is.True, "The batch should have been disposed after unlocking.");
        }
Beispiel #2
0
        public void DisposeIsDelegatedToTheTransportClient()
        {
            var mockBatch = new MockTransportBatch();
            var batch     = new ServiceBusMessageBatch(mockBatch);

            batch.Dispose();
            Assert.That(mockBatch.DisposeInvoked, Is.True);
        }
Beispiel #3
0
        public void DisposeIsDelegatedToTheTransportClient()
        {
            var mockBatch = new MockTransportBatch();
            var mockScope = new EntityScopeFactory("mock", "mock");

            var batch = new ServiceBusMessageBatch(mockBatch, mockScope);

            batch.Dispose();
            Assert.That(mockBatch.DisposeInvoked, Is.True);
        }
        public async Task SendServiceBusMessagesAsync(List <ServiceBusMessage> listServiceBusMessages)
        {
            try
            {
                ServiceBusSender serviceBusSender = _serviceBusClient.CreateSender("submission");
                _log.LogInformation($"Sending {listServiceBusMessages.Count} messages");
                ServiceBusMessageBatch messageBatch = await serviceBusSender.CreateMessageBatchAsync();

                int batchMessageCount = 0;
                foreach (ServiceBusMessage serviceBusMessage in listServiceBusMessages)
                {
                    if (!messageBatch.TryAddMessage(serviceBusMessage))
                    {
                        // if it is too large for the batch
                        _log.LogError("$The message { i} is too large to fit in the batch.");
                        throw new Exception($"The message {serviceBusMessage.Body} is too large to fit in the batch.");
                    }
                    batchMessageCount++;

                    if (batchMessageCount >= batchMessageCountLimit)
                    {
                        // Use the producer client to send the batch of messages to the Service Bus queue
                        await serviceBusSender.SendMessagesAsync(messageBatch);

                        _log.LogInformation($"A batch of {batchMessageCount} messages has been published to the queue.");
                        messageBatch.Dispose();
                        messageBatch = await serviceBusSender.CreateMessageBatchAsync();

                        batchMessageCount = 0;
                    }
                }
                await serviceBusSender.SendMessagesAsync(messageBatch);

                messageBatch.Dispose();
                _log.LogInformation($"A batch of {batchMessageCount} messages has been published to the queue.");
            }
            catch (Exception ex)
            {
                _log.LogError("error occurred", ex);
            }
        }
Beispiel #5
0
        static async Task SendMessageBatchAsync()
        {
            ServiceBusSender sender = srv.CreateSender(QueueOrTopicName);

            string[] sessionIds = { Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString() };
            ServiceBusMessageBatch messageBatch = await sender.CreateMessageBatchAsync();

            try {
                for (int i = 0; i < NumeroDeEnvios; i++)
                {
                    string messageBody = $"Mensaje {DateTime.Now:mm:ss:ff}: valor {rnd.Next(1, 100)}.";
                    var    item        = new Item()
                    {
                        Id = i, Mensaje = messageBody
                    };
                    var message = item.AsMessage();
                    message.To      = item.Categoria;
                    message.Subject = item.Nivel;
                    message.ApplicationProperties.Add("nivel", item.Nivel);
                    message.MessageId = $"{DateTime.Now.Minute}-{i}";
                    message.SessionId = sessionIds[i % 3];
                    if (TimeToLive > 0)
                    {
                        message.TimeToLive = TimeSpan.FromSeconds(TimeToLive);
                    }
                    Console.WriteLine($"Sending message: {message.MessageId} - {item}");
                    if (!messageBatch.TryAddMessage(message))
                    {
                        await sender.SendMessagesAsync(messageBatch);

                        Console.WriteLine($"Sending Batch: {messageBatch.Count} - {messageBatch.SizeInBytes}");
                        messageBatch.Dispose();
                        messageBatch = await sender.CreateMessageBatchAsync();

                        messageBatch.TryAddMessage(message);
                    }
                }
                Console.WriteLine($"Sending Batch: {messageBatch.Count} - {messageBatch.SizeInBytes}");
                await sender.SendMessagesAsync(messageBatch);
            } catch (Exception exception) {
                Console.WriteLine($"{DateTime.Now} :: Exception: {exception.Message}");
            }
            await sender.CloseAsync();

            Console.WriteLine("Messages was sent successfully.");
        }