Example #1
0
        public static async Task <string> Run([ActivityTrigger] EventHubJobProperties ehJobProperties, TraceWriter log)
        {
            EventHubsConnectionStringBuilder connectionStringBuilder = new EventHubsConnectionStringBuilder(ehJobProperties.ConnectionString)
            {
                EntityPath = ehJobProperties.EventHub
            };

            string connectionString       = connectionStringBuilder.ToString();
            int    numOfMessages          = ehJobProperties.Frequency * 60 * ehJobProperties.Duration;
            IEnumerable <string> messages = new List <string>(Messages.CreateMessages(numOfMessages, ehJobProperties.MessageScheme));
            //IList<string> msgs = new List<string>(messages);

            int secondsPerBatch  = 5;
            int messagesPerBatch = secondsPerBatch * ehJobProperties.Frequency;

            while (messages.Count() > 0)
            {
                await SendMessageBatch(messages.Take(messagesPerBatch), connectionString, log);

                messages = messages.Skip(messagesPerBatch);
                log.Info($"sent batch of {numOfMessages} messages");
            }

            log.Info($"finished.");
            return($"finished sending {numOfMessages} to {ehJobProperties.EventHub}!");
        }
        public static async Task <string> Run([ActivityTrigger] EventHubJobProperties ehJobProperties, ILogger log)
        {
            try
            {
                EventHubsConnectionStringBuilder connectionStringBuilder = new EventHubsConnectionStringBuilder(ehJobProperties.ConnectionString)
                {
                    EntityPath = ehJobProperties.EventHub
                };

                string         connectionString = connectionStringBuilder.ToString();
                EventHubClient eventHubClient   = EventHubClient.CreateFromConnectionString(connectionString);

                int secondsPerJobBatch = Convert.ToInt16(Environment.GetEnvironmentVariable("secondsPerBatch"));
                int messagesInJobBatch = ehJobProperties.Frequency * secondsPerJobBatch;
                int messagesPerSend    = 250;

                IEnumerable <string> messages = Messages.CreateMessages(messagesInJobBatch, ehJobProperties.MessageScheme);

                while (messages.Count() > 0)
                {
                    var messagesToSend = messages.Take(messagesPerSend);
                    var ehMessages     = messagesToSend.Select(m => new EventData(Encoding.UTF8.GetBytes(m)));
                    await eventHubClient.SendAsync(ehMessages);

                    messages = messages.Skip(messagesPerSend);
                }
                await eventHubClient.CloseAsync();

                log.LogInformation($"sent batch of {messagesInJobBatch} messages");
                return($"finished sending {messagesInJobBatch} to {ehJobProperties.EventHub}!");
            }
            catch (Exception exception)
            {
                log.LogInformation($"Exception: {exception.Message}");
                return($"Exception: {exception.Message}");
            }
        }