-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Event Hubs: Synchronous APIs Part 2 #4970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
30ac167
Formatting changes in EventHubAsyncProducer.
conniey 5c041aa
Adding EventHubClient, EventHubConsumer, and EventHubProducer.
conniey 4f3bab3
Exposing EventHubClient creation in EventHubClientBuilder.
conniey 5e10349
EventHubClient, Consumer and Producer implements Closeable.
conniey 4727715
Fixing sample by removing event hub instance from namespace connectio…
conniey 4c94da5
Remove unneeded sample in EventHubClientBuilder.
conniey 42c099a
Add EventHubClient to builder annotation.
conniey d1ab53c
Update EventHubClientBuilder samples. Remove unneeded ones.
conniey eb02600
Update samples in EventHubClientBuilder.
conniey e473b67
Add documentation to EventHubConsumer.
conniey beb0090
Fixing links to EventHubAsyncProducer samples.
conniey 17edced
Adding EventHubProducer code samples.
conniey d4a7871
Fixing spotbug issues.
conniey e4de89d
Update from Iterable to IterableResponse.
conniey 1f48bf7
Make test contents package-private.
conniey 49c2aaa
Update sdk/eventhubs/azure-messaging-eventhubs/src/main/java/com/azur…
conniey 3b5a012
Update sdk/eventhubs/azure-messaging-eventhubs/src/main/java/com/azur…
conniey fd83955
Merge branch 'synchronous-client' of https://github.com/conniey/azure…
conniey 0d40de4
Fix typo in connection strings.
conniey bbb1eb5
Fix javadoc errors.
conniey d4c89b2
Add Null checks for options.
conniey 104fe4b
Fix null checks.
conniey d49d7c8
Remove unneeded log message.
conniey e942750
Adding tests for EventHubProducer.
conniey 5f51b4e
Simplifying creation of EventHubAsyncProducer
conniey 1018908
Remove redundant null check.
conniey 470f4ef
Select correct retryDuration when constructing EventHubProducer.
conniey c648c2e
Adding EventHubProducer tests.
conniey cfb1d11
Add correct header file.
conniey f7cf4e4
Rename EventHubClientIntegrationTest -> EventHubAsyncClientIntegratio…
conniey 48b960a
Add integration tests for EventHubClient.
conniey 4f26053
Make EventHubConsumer methods public
conniey dbe3d82
Fix spotbug issues
conniey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
...azure-messaging-eventhubs/src/main/java/com/azure/messaging/eventhubs/EventHubClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.messaging.eventhubs; | ||
|
|
||
| import com.azure.core.amqp.RetryOptions; | ||
| import com.azure.core.http.rest.IterableResponse; | ||
| import com.azure.core.implementation.annotation.ReturnType; | ||
| import com.azure.core.implementation.annotation.ServiceClient; | ||
| import com.azure.core.implementation.annotation.ServiceMethod; | ||
| import com.azure.messaging.eventhubs.implementation.ConnectionOptions; | ||
| import com.azure.messaging.eventhubs.models.EventHubConsumerOptions; | ||
| import com.azure.messaging.eventhubs.models.EventHubProducerOptions; | ||
| import com.azure.messaging.eventhubs.models.EventPosition; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.time.Duration; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * The main point of interaction with Azure Event Hubs, the client offers a connection to a specific Event Hub within | ||
| * the Event Hubs namespace and offers operations for sending event data, receiving events, and inspecting the connected | ||
| * Event Hub. | ||
| * | ||
| * <p> | ||
| * <strong>Creating a synchronous {@link EventHubClient} using an Event Hub instance connection string</strong> | ||
| * </p> | ||
| * | ||
| * {@codesnippet com.azure.messaging.eventhubs.eventhubclient.instantiation} | ||
| * | ||
| * @see EventHubClientBuilder | ||
| * @see EventHubAsyncClient To communicate with Event Hub using an asynchronous client. | ||
| * @see <a href="https://docs.microsoft.com/Azure/event-hubs/event-hubs-about">About Azure Event Hubs</a> | ||
| */ | ||
| @ServiceClient(builder = EventHubClientBuilder.class) | ||
| public class EventHubClient implements Closeable { | ||
| private final EventHubAsyncClient client; | ||
| private final RetryOptions retry; | ||
| private final EventHubProducerOptions defaultProducerOptions; | ||
| private final EventHubConsumerOptions defaultConsumerOptions; | ||
|
|
||
| EventHubClient(EventHubAsyncClient client, ConnectionOptions connectionOptions) { | ||
| Objects.requireNonNull(connectionOptions); | ||
|
|
||
| this.client = Objects.requireNonNull(client); | ||
| this.retry = connectionOptions.retry(); | ||
| this.defaultProducerOptions = new EventHubProducerOptions() | ||
| .retry(connectionOptions.retry()); | ||
| this.defaultConsumerOptions = new EventHubConsumerOptions() | ||
| .retry(connectionOptions.retry()) | ||
| .scheduler(connectionOptions.scheduler()); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves information about an Event Hub, including the number of partitions present and their identifiers. | ||
| * | ||
| * @return The set of information for the Event Hub that this client is associated with. | ||
| */ | ||
| @ServiceMethod(returns = ReturnType.SINGLE) | ||
| public EventHubProperties getProperties() { | ||
| return client.getProperties().block(retry.tryTimeout()); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the identifiers for all the partitions of an Event Hub. | ||
| * | ||
| * @return The identifiers for all partitions of an Event Hub. | ||
| */ | ||
| @ServiceMethod(returns = ReturnType.COLLECTION) | ||
| public IterableResponse<String> getPartitionIds() { | ||
| return new IterableResponse<>(client.getPartitionIds()); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves information about a specific partition for an Event Hub, including elements that describe the available | ||
| * events in the partition event stream. | ||
| * | ||
| * @param partitionId The unique identifier of a partition associated with the Event Hub. | ||
| * @return The information for the requested partition under the Event Hub this client is associated with. | ||
| */ | ||
| @ServiceMethod(returns = ReturnType.SINGLE) | ||
| public PartitionProperties getPartitionProperties(String partitionId) { | ||
| return client.getPartitionProperties(partitionId).block(retry.tryTimeout()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an Event Hub producer responsible for transmitting {@link EventData} to the Event Hub, grouped together | ||
| * in batches. Event data is automatically routed to an available partition. | ||
| * | ||
| * @return A new {@link EventHubProducer}. | ||
| */ | ||
| public EventHubProducer createProducer() { | ||
| return createProducer(defaultProducerOptions); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an Event Hub producer responsible for transmitting {@link EventData} to the Event Hub, grouped together | ||
| * in batches. If {@link EventHubProducerOptions#partitionId() options.partitionId()} is not {@code null}, the | ||
| * events are routed to that specific partition. Otherwise, events are automatically routed to an available | ||
| * partition. | ||
| * | ||
| * @param options The set of options to apply when creating the producer. | ||
| * @return A new {@link EventHubProducer}. | ||
| * @throws NullPointerException if {@code options} is {@code null}. | ||
| */ | ||
| public EventHubProducer createProducer(EventHubProducerOptions options) { | ||
| Objects.requireNonNull(options); | ||
|
|
||
| final EventHubAsyncProducer producer = client.createProducer(options); | ||
|
|
||
| final Duration tryTimeout = options.retry() != null && options.retry().tryTimeout() != null | ||
| ? options.retry().tryTimeout() | ||
| : defaultProducerOptions.retry().tryTimeout(); | ||
|
|
||
| return new EventHubProducer(producer, tryTimeout); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an Event Hub consumer responsible for reading {@link EventData} from a specific Event Hub partition, as a | ||
| * member of the specified consumer group, and begins reading events from the {@code eventPosition}. | ||
| * | ||
| * The consumer created is non-exclusive, allowing multiple consumers from the same consumer group to be actively | ||
| * reading events from the partition. These non-exclusive consumers are sometimes referred to as "Non-epoch | ||
| * Consumers". | ||
| * | ||
| * @param consumerGroup The name of the consumer group this consumer is associated with. Events are read in the | ||
| * context of this group. The name of the consumer group that is created by default is {@link | ||
| * EventHubAsyncClient#DEFAULT_CONSUMER_GROUP_NAME "$Default"}. | ||
| * @param partitionId The identifier of the Event Hub partition. | ||
| * @param eventPosition The position within the partition where the consumer should begin reading events. | ||
| * @return A new {@link EventHubConsumer} that receives events from the partition at the given position. | ||
| * @throws NullPointerException If {@code eventPosition}, {@code consumerGroup}, {@code partitionId}, or {@code | ||
| * options} is {@code null}. | ||
| * @throws IllegalArgumentException If {@code consumerGroup} or {@code partitionId} is an empty string. | ||
| */ | ||
| public EventHubConsumer createConsumer(String consumerGroup, String partitionId, EventPosition eventPosition) { | ||
| final EventHubAsyncConsumer consumer = client.createConsumer(consumerGroup, partitionId, eventPosition); | ||
| return new EventHubConsumer(consumer, defaultConsumerOptions); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an Event Hub consumer responsible for reading {@link EventData} from a specific Event Hub partition, as a | ||
| * member of the configured consumer group, and begins reading events from the specified {@code eventPosition}. | ||
| * | ||
| * <p> | ||
| * A consumer may be exclusive, which asserts ownership over the partition for the consumer group to ensure that | ||
| * only one consumer from that group is reading from the partition. These exclusive consumers are sometimes referred | ||
| * to as "Epoch Consumers." | ||
| * | ||
| * A consumer may also be non-exclusive, allowing multiple consumers from the same consumer group to be actively | ||
| * reading events from the partition. These non-exclusive consumers are sometimes referred to as "Non-epoch | ||
| * Consumers." | ||
| * | ||
| * Designating a consumer as exclusive may be specified in the {@code options}, by setting {@link | ||
| * EventHubConsumerOptions#ownerLevel(Long)} to a non-null value. By default, consumers are created as | ||
| * non-exclusive. | ||
| * </p> | ||
| * | ||
| * @param consumerGroup The name of the consumer group this consumer is associated with. Events are read in the | ||
| * context of this group. The name of the consumer group that is created by default is {@link | ||
| * EventHubAsyncClient#DEFAULT_CONSUMER_GROUP_NAME "$Default"}. | ||
| * @param partitionId The identifier of the Event Hub partition from which events will be received. | ||
| * @param eventPosition The position within the partition where the consumer should begin reading events. | ||
| * @param options The set of options to apply when creating the consumer. | ||
| * @return An new {@link EventHubConsumer} that receives events from the partition with all configured {@link | ||
| * EventHubConsumerOptions}. | ||
| * @throws NullPointerException If {@code eventPosition}, {@code consumerGroup}, {@code partitionId}, or {@code | ||
| * options} is {@code null}. | ||
| * @throws IllegalArgumentException If {@code consumerGroup} or {@code partitionId} is an empty string. | ||
| */ | ||
| public EventHubConsumer createConsumer(String consumerGroup, String partitionId, EventPosition eventPosition, | ||
| EventHubConsumerOptions options) { | ||
| final EventHubAsyncConsumer consumer = client.createConsumer(consumerGroup, partitionId, eventPosition, options); | ||
| return new EventHubConsumer(consumer, options); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public void close() { | ||
| client.close(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.