Ejemplo n.º 1
0
        static async Task Main(string[] args)
        {
            var eventHubConnectionString = $"Endpoint=sb://event-hub-examples.servicebus.windows.net/;SharedAccessKeyName={SHARED_ACCESS_KEY_NAME};SharedAccessKey={SHARED_ACCESS_KEY};EntityPath={EVENT_HUB_NAME}";

            _client = EventHubClient.CreateFromConnectionString(eventHubConnectionString);
            var runtimeInformation = await _client.GetRuntimeInformationAsync();

            var partitionInformation = runtimeInformation.PartitionIds;

            var partitionSender = _client.CreatePartitionSender(partitionInformation[0]);

            var newEventData = new SimpleDataModel()
            {
                Age       = 35,
                FirstName = "User",
                LastName  = "Named"
            };

            await partitionSender.SendAsync(newEventData.ToEventData());

            var newEventData2 = new
            {
                Age       = 15,
                FirstName = "Pracka",
                LastName  = "Meen"
            };

            await _client.SendAsync(EventDataMapper.MapToEventData(newEventData2));
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        protected override async Task <BoolResult> StartupCoreAsync(OperationContext context)
        {
            Tracer.Info(context, $"Initializing Event Hub-based content location event store with epoch '{_configuration.Epoch}'.");

            var baseInitializeResult = await base.StartupCoreAsync(context);

            if (!baseInitializeResult)
            {
                return(baseInitializeResult);
            }

            var connectionStringBuilder =
                new EventHubsConnectionStringBuilder(_configuration.EventHubConnectionString)
            {
                EntityPath = _configuration.EventHubName,
            };

            _currentEventProcessor = new Processor(context, this);

            // Retry behavior in the Azure Event Hubs Client Library is controlled by the RetryPolicy property on the EventHubClient class.
            // The default policy retries with exponential backoff when Azure Event Hub returns a transient EventHubsException or an OperationCanceledException.
            _eventHubClient  = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
            _partitionSender = _eventHubClient.CreatePartitionSender(PartitionId);

            return(BoolResult.Success);
        }
Ejemplo n.º 3
0
        internal static async Task SendToPartitionAsync(EventHubClient ehClient, string partitionId, EventDataBatch batch)
        {
            TestUtility.Log($"Starting to send {batch.Count} messages to partition {partitionId}.");
            var partitionSender = ehClient.CreatePartitionSender(partitionId);
            await partitionSender.SendAsync(batch);

            TestUtility.Log("Sends done.");
        }
Ejemplo n.º 4
0
        // Send and receive given event on given partition.
        protected async Task <EventData> SendAndReceiveEventAsync(string partitionId, EventData sendEvent, EventHubClient client)
        {
            PartitionSender   partitionSender   = client.CreatePartitionSender(partitionId);
            PartitionReceiver partitionReceiver = client.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, EventPosition.FromEnqueuedTime(DateTime.UtcNow.AddMinutes(-10)));

            EventData receivedEvent = null;

            try
            {
                string uniqueEventId = Guid.NewGuid().ToString();
                TestUtility.Log($"Sending event to Partition {partitionId} with custom property EventId {uniqueEventId}");
                sendEvent.Properties["EventId"] = uniqueEventId;
                await partitionSender.SendAsync(sendEvent);

                bool expectedEventReceived = false;
                do
                {
                    IEnumerable <EventData> eventDatas = await partitionReceiver.ReceiveAsync(10);

                    if (eventDatas == null)
                    {
                        break;
                    }

                    TestUtility.Log($"Received a batch of {eventDatas.Count()} events:");
                    foreach (var eventData in eventDatas)
                    {
                        object objectValue;

                        if (eventData.Properties != null && eventData.Properties.TryGetValue("EventId", out objectValue))
                        {
                            TestUtility.Log($"Received message with EventId {objectValue}");
                            string receivedId = objectValue.ToString();
                            if (receivedId == uniqueEventId)
                            {
                                TestUtility.Log("Success");
                                receivedEvent         = eventData;
                                expectedEventReceived = true;
                                break;
                            }
                        }
                    }
                }while (!expectedEventReceived);

                Assert.True(expectedEventReceived, $"Did not receive expected event with EventId {uniqueEventId}");
            }
            finally
            {
                await Task.WhenAll(
                    partitionReceiver.CloseAsync(),
                    partitionSender.CloseAsync());
            }

            return(receivedEvent);
        }
        internal static async Task SendToPartitionAsync(EventHubClient ehClient, string partitionId, EventData eventData, int numberOfMessages = 1)
        {
            TestUtility.Log($"Starting to send {numberOfMessages} to partition {partitionId}.");
            var partitionSender = ehClient.CreatePartitionSender(partitionId);

            for (int i = 0; i < numberOfMessages; i++)
            {
                await partitionSender.SendAsync(eventData);
            }

            TestUtility.Log("Sends done.");
        }
Ejemplo n.º 6
0
        internal static async Task SendToPartitionAsync(EventHubClient ehClient, string partitionId, string messageBody, int numberOfMessages = 1)
        {
            TestUtility.Log($"Starting to send {numberOfMessages} to partition {partitionId}.");
            var partitionSender = ehClient.CreatePartitionSender(partitionId);

            for (int i = 0; i < numberOfMessages; i++)
            {
                await partitionSender.SendAsync(new EventData(Encoding.UTF8.GetBytes(messageBody)));
            }

            TestUtility.Log("Sends done.");
        }
Ejemplo n.º 7
0
        /// <inheritdoc />
        protected override async Task <BoolResult> StartupCoreAsync(OperationContext context)
        {
            await base.StartupCoreAsync(context).ThrowIfFailure();

            var connectionStringBuilder =
                new EventHubsConnectionStringBuilder(_configuration.EventHubConnectionString)
            {
                EntityPath = _configuration.EventHubName,
            };

            // Retry behavior in the Azure Event Hubs Client Library is controlled by the RetryPolicy property on the EventHubClient class.
            // The default policy retries with exponential backoff when Azure Event Hub returns a transient EventHubsException or an OperationCanceledException.
            _eventHubClient  = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
            _partitionSender = _eventHubClient.CreatePartitionSender(PartitionId);

            return(BoolResult.Success);
        }
Ejemplo n.º 8
0
        public async Task PublishToSpecificPartition()
        {
            await using var scope = await EventHubScope.CreateAsync(1);

            #region Snippet:EventHubs_Migrate_T1_PublishToSpecificPartition
#if SNIPPET
            var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
            var eventHubName     = "<< NAME OF THE EVENT HUB >>";
#else
            var connectionString = TestUtility.EventHubsConnectionString;
            var eventHubName     = scope.EventHubName;
#endif

            var builder = new EventHubsConnectionStringBuilder(connectionString);
            builder.EntityPath = eventHubName;

            EventHubClient  client = EventHubClient.CreateFromConnectionString(builder.ToString());
            PartitionSender sender = default;

            try
            {
                using var eventBatch = client.CreateBatch();

                for (var index = 0; index < 5; ++index)
                {
                    var eventData = new EventData(Encoding.UTF8.GetBytes($"Event #{ index }"));

                    if (!eventBatch.TryAdd(eventData))
                    {
                        throw new Exception($"The event at { index } could not be added");
                    }
                }

                string firstPartition = (await client.GetRuntimeInformationAsync()).PartitionIds.First();
                sender = client.CreatePartitionSender(firstPartition);

                await sender.SendAsync(eventBatch);
            }
            finally
            {
                sender?.Close();
                client.Close();
            }

            #endregion
        }
        /// <summary>
        ///   Performs the tasks needed to initialize and set up the environment for the test scenario.
        ///   When multiple instances are run in parallel, the setup will take place once, prior to the
        ///   execution of the first test instance.
        /// </summary>
        ///
        public async override Task GlobalSetupAsync()
        {
            await base.GlobalSetupAsync().ConfigureAwait(false);

            s_scope = await EventHubScope.CreateAsync(4).ConfigureAwait(false);

            s_client    = EventHubClient.CreateFromConnectionString(TestUtility.BuildEventHubsConnectionString(s_scope.EventHubName));
            s_eventBody = EventGenerator.CreateRandomBody(Options.Size);

            var partition = (await s_client.GetRuntimeInformationAsync().ConfigureAwait(false)).PartitionIds[0];

            s_sender = s_client.CreatePartitionSender(partition);

            // Publish an empty event to force the connection and link to be established.

            await s_sender.SendAsync(new[] { new EventData(Array.Empty <byte>()) }).ConfigureAwait(false);
        }
Ejemplo n.º 10
0
        /// <summary>
        ///   Seeds the Event Hub partition with enough events to satisfy the read operations the test scenario
        ///   is expected to perform.
        /// </summary>
        ///
        /// <param name="client">The client to use for publishing events; ownership is assumed to be retained by the caller.</param>
        /// <param name="partitionId">The identifier of the partition to which events should be published.</param>
        ///
        /// <remarks>
        ///   This method makes heavy use of class state, including the
        ///   test environment and command line options.
        /// </remarks>
        ///
        private async Task SeedEventsToBeReadAsync(EventHubClient client,
                                                   string partitionId)
        {
            // Calculate the number of events needed to satisfy the number of events per iteration
            // and then buffer it to allow for the warm-up.

            var eventCount = Options.Count * Options.Iterations * 3;
            var eventBody  = EventGenerator.CreateRandomBody(Options.Size);
            var sender     = client.CreatePartitionSender(partitionId);

            try
            {
                foreach (var eventData in EventGenerator.CreateEventsFromBody(eventCount, eventBody))
                {
                    using var batch = sender.CreateBatch();

                    // Fill the batch with as many events that will fit.

                    while (batch.TryAdd(EventGenerator.CreateEventFromBody(eventBody)))
                    {
                    }

                    // If there were no events in the batch, then a single event was generated that is too large to
                    // ever send.  In this context, it will be detected on the first TryAdd call, since events are
                    // sharing a common body.

                    if (batch.Count == 0)
                    {
                        throw new InvalidOperationException("There was an event too large to fit into a batch.");
                    }

                    await sender.SendAsync(batch).ConfigureAwait(false);
                }
            }
            finally
            {
                await sender.CloseAsync().ConfigureAwait(false);
            }
        }
Ejemplo n.º 11
0
        static async Task Main(string[] args)
        {
            // Creates an EventHubsConnectionStringBuilder object from the connection string, and sets the EntityPath.
            // Typically, the connection string should have the entity path in it, but this simple scenario
            // uses the connection string from the namespace.
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(EventHubConnectionString)
            {
                EntityPath = EventHubName
            };

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

            // Specify a specific partition Id to send the messages to.
            partitionSender = eventHubClient.CreatePartitionSender("2");

            await SendEventsToEventHub(100);

            await eventHubClient.CloseAsync();

            Console.WriteLine("Press ENTER to exit.");
            Console.ReadLine();
        }
        /// <summary>
        ///   Performs the tasks needed to initialize and set up the environment for the test scenario.
        ///   When multiple instances are run in parallel, the setup will take place once, prior to the
        ///   execution of the first test instance.
        /// </summary>
        ///
        public async override Task GlobalSetupAsync()
        {
            await base.GlobalSetupAsync().ConfigureAwait(false);

            s_scope = await EventHubScope.CreateAsync(4).ConfigureAwait(false);

            s_client    = EventHubClient.CreateFromConnectionString(TestUtility.BuildEventHubsConnectionString(s_scope.EventHubName));
            s_eventBody = EventGenerator.CreateRandomBody(Options.Size);

            var partition = (await s_client.GetRuntimeInformationAsync().ConfigureAwait(false)).PartitionIds[0];

            s_sender = s_client.CreatePartitionSender(partition);

            // Publish an empty event to force the connection and link to be established.

            using var batch = s_sender.CreateBatch();

            if (!batch.TryAdd(new EventData(Array.Empty <byte>())))
            {
                throw new InvalidOperationException("The empty event could not be added to the batch during global setup.");
            }

            await s_sender.SendAsync(batch).ConfigureAwait(false);
        }
Ejemplo n.º 13
0
 public IPartitionSenderWrapper CreatePartitionSender(string partitionId)
 {
     return(new DefaultPartitionSender(_client.CreatePartitionSender(partitionId)));
 }