Esempio n. 1
0
        public async Task PublishEventsAsync <T>(T message, string topicName, AFBusMessageContext messageContext) where T : class
        {
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_EVENTHUB))
            {
                EntityPath = topicName
            };

            var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_STORAGE));

            try
            {
                var messageAsString = serializer.Serialize(message);

                var messageWithEnvelope = new AFBusMessageEnvelope()
                {
                    Context = messageContext,
                    Body    = messageAsString
                };

                messageContext.Destination = topicName;


                var finalMessage = serializer.Serialize(messageWithEnvelope);

                //if the message is bigger than the limit put the body in the blob storage
                if ((finalMessage.Length * sizeof(Char)) > MAX_MESSAGE_SIZE)
                {
                    var fileName = Guid.NewGuid().ToString("N").ToLower() + ".afbus";
                    messageWithEnvelope.Context.BodyInFile = true;

                    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

                    // Create a container
                    var cloudBlobContainer = cloudBlobClient.GetContainerReference(CONTAINER_NAME.ToLower());
                    await cloudBlobContainer.CreateIfNotExistsAsync().ConfigureAwait(false);

                    CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);
                    await blockBlob.UploadTextAsync(messageWithEnvelope.Body);

                    messageWithEnvelope.Body = fileName;

                    finalMessage = serializer.Serialize(messageWithEnvelope);
                }



                await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(finalMessage)));
            }
            finally
            {
                await eventHubClient.CloseAsync();
            }
        }
Esempio n. 2
0
        public void Deliver(Message message)
        {
            var transportMessageToSend   = serializer.Serialize(message);
            var receivedTransportMessage = transportMessageToSend.ToReceivedTransportMessage();

            messageQueue.Enqueue(receivedTransportMessage);
        }
        public void Deliver(Message message)
        {
            var transportMessageToSend   = serializer.Serialize(message);
            var receivedTransportMessage = new ReceivedTransportMessage
            {
                Id      = NewMessageId(),
                Body    = transportMessageToSend.Body,
                Label   = transportMessageToSend.Label,
                Headers = transportMessageToSend.Headers.Clone(),
            };

            messageQueue.Enqueue(receivedTransportMessage);
        }
Esempio n. 4
0
        /// <summary>
        /// Calls each function referenced by each message in the dictionary.
        /// </summary>
        internal async Task HandleAsync <T>(T message, AFBusMessageContext messageContext, ILogger log) where T : class
        {
            log?.LogInformation("Message (" + messageContext.MessageID + ") of type " + message.GetType().ToString() + " received in AFBus" + " for transaction " + messageContext.TransactionID);

            if (!messageHandlersDictionary.ContainsKey(message.GetType()) && !messageToSagaDictionary.ContainsKey(message.GetType()))
            {
                throw new Exception("Handler not found for this message." + serializer.Serialize(message));
            }

            //still sometime to wait, go back to queue
            if (messageContext.MessageDelayedTime != null && messageContext.MessageDelayedTime > TimeSpan.Zero)
            {
                var transport = SolveDependency <ISendMessages>();

                var differenceUntilFinalWakeUp = messageContext.MessageFinalWakeUpTimeStamp.Value - DateTime.UtcNow;

                if (differenceUntilFinalWakeUp > TimeSpan.FromSeconds(1))
                {
                    if (differenceUntilFinalWakeUp >= transport.MaxDelay())
                    {
                        messageContext.MessageDelayedTime = transport.MaxDelay();
                    }
                    else
                    {
                        messageContext.MessageDelayedTime = differenceUntilFinalWakeUp;
                    }


                    await transport.SendMessageAsync(message, messageContext.Destination, messageContext);

                    return;
                }
            }

            await InvokeStatelessHandlers(message, messageContext, log).ConfigureAwait(false);

            await InvokeSagaHandlers(message, messageContext, log).ConfigureAwait(false);
        }
Esempio n. 5
0
        public async Task SendMessageAsync <T>(T message, string serviceName, AFBusMessageContext messageContext) where T : class
        {
            serviceName = serviceName.ToLower();

            CloudQueue queue = queueClient.GetQueueReference(serviceName);

            if (!createdQueues.Contains(serviceName))
            {
                await queue.CreateIfNotExistsAsync();

                createdQueues.Add(serviceName);
            }


            var messageAsString = serializer.Serialize(message);

            var messageWithEnvelope = new AFBusMessageEnvelope()
            {
                Context = messageContext,
                Body    = messageAsString
            };

            messageContext.Destination = serviceName;

            TimeSpan?initialVisibilityDelay = null;

            if (messageContext.MessageDelayedTime != null && messageContext.MessageDelayedTime >= MaxDelay())
            {
                initialVisibilityDelay = MaxDelay();

                messageContext.MessageDelayedTime = MaxDelay();
            }
            else if (messageContext.MessageDelayedTime != null)
            {
                initialVisibilityDelay = messageContext.MessageDelayedTime;
            }

            if (messageContext.MessageDelayedTime != null && initialVisibilityDelay.Value < TimeSpan.Zero)
            {
                initialVisibilityDelay = null;

                messageContext.MessageDelayedTime = null;
            }

            var finalMessage = serializer.Serialize(messageWithEnvelope);

            //if the message is bigger than the limit put the body in the blob storage
            if ((finalMessage.Length * sizeof(Char)) > MAX_MESSAGE_SIZE)
            {
                var fileName = Guid.NewGuid().ToString("N").ToLower() + ".afbus";
                messageWithEnvelope.Context.BodyInFile = true;

                // Create a container
                var cloudBlobContainer = cloudBlobClient.GetContainerReference(CONTAINER_NAME.ToLower());

                CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);
                await blockBlob.UploadTextAsync(messageWithEnvelope.Body);

                messageWithEnvelope.Body = fileName;

                finalMessage = serializer.Serialize(messageWithEnvelope);
            }

            await queue.AddMessageAsync(new CloudQueueMessage(finalMessage), null, initialVisibilityDelay, null, null).ConfigureAwait(false);
        }
Esempio n. 6
0
        public async Task PublishEventsAsync <T>(T message, string topicName, AFBusMessageContext messageContext) where T : class
        {
            var sender = new MessageSender(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_SERVICEBUS), topicName.ToLower());
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_STORAGE));


            var messageAsString = serializer.Serialize(message);

            var messageWithEnvelope = new AFBusMessageEnvelope()
            {
                Context = messageContext,
                Body    = messageAsString
            };

            messageContext.Destination = topicName;

            TimeSpan?initialVisibilityDelay = null;

            if (messageContext.MessageDelayedTime != null && messageContext.MessageDelayedTime >= MaxDelay())
            {
                initialVisibilityDelay = MaxDelay();

                messageContext.MessageDelayedTime = MaxDelay();
            }
            else if (messageContext.MessageDelayedTime != null)
            {
                initialVisibilityDelay = messageContext.MessageDelayedTime;
            }

            if (messageContext.MessageDelayedTime != null && initialVisibilityDelay.Value < TimeSpan.Zero)
            {
                initialVisibilityDelay = null;

                messageContext.MessageDelayedTime = null;
            }

            var finalMessage = serializer.Serialize(messageWithEnvelope);

            //if the message is bigger than the limit put the body in the blob storage
            if ((finalMessage.Length * sizeof(Char)) > MAX_MESSAGE_SIZE)
            {
                var fileName = Guid.NewGuid().ToString("N").ToLower() + ".afbus";
                messageWithEnvelope.Context.BodyInFile = true;

                CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

                // Create a container
                var cloudBlobContainer = cloudBlobClient.GetContainerReference(CONTAINER_NAME.ToLower());
                await cloudBlobContainer.CreateIfNotExistsAsync().ConfigureAwait(false);

                CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);
                await blockBlob.UploadTextAsync(messageWithEnvelope.Body);

                messageWithEnvelope.Body = fileName;

                finalMessage = serializer.Serialize(messageWithEnvelope);
            }


            var finalSBMessage = new Message(Encoding.UTF8.GetBytes(finalMessage))
            {
                ContentType = "application/json",
                Label       = topicName,
                MessageId   = messageContext.MessageID.ToString(),
                TimeToLive  = TimeSpan.FromDays(10)
            };

            if (messageContext.MessageDelayedTime.HasValue)
            {
                finalSBMessage.ScheduledEnqueueTimeUtc = DateTime.UtcNow + messageContext.MessageDelayedTime.Value;
            }


            await sender.SendAsync(finalSBMessage).ConfigureAwait(false);
        }