public final class EventHubClientImpl extends Object implements EventHubClient
| Modifier and Type | Field and Description |
|---|---|
protected ScheduledExecutorService |
executor |
static String |
USER_AGENT
It will be truncated to 128 characters
|
DEFAULT_CONSUMER_GROUP_NAME| Modifier and Type | Method and Description |
|---|---|
CompletableFuture<Void> |
close() |
void |
closeSync() |
static CompletableFuture<EventHubClient> |
create(String connectionString,
RetryPolicy retryPolicy,
ScheduledExecutorService executor)
Factory method to create an instance of
EventHubClient using the supplied connectionString. |
EventDataBatch |
createBatch(BatchOptions options)
Creates an Empty Collection of
EventData. |
CompletableFuture<PartitionReceiver> |
createEpochReceiver(String consumerGroupName,
String partitionId,
EventPosition eventPosition,
long epoch)
Create a Epoch based EventHub receiver with given partition id and start receiving from the beginning of the partition stream.
|
CompletableFuture<PartitionReceiver> |
createEpochReceiver(String consumerGroupName,
String partitionId,
EventPosition eventPosition,
long epoch,
ReceiverOptions receiverOptions)
Create a Epoch based EventHub receiver with given partition id and start receiving from the beginning of the partition stream.
|
CompletableFuture<PartitionSender> |
createPartitionSender(String partitionId)
Create a
PartitionSender which can publish EventData's directly to a specific EventHub partition (sender type iii. |
CompletableFuture<PartitionReceiver> |
createReceiver(String consumerGroupName,
String partitionId,
EventPosition eventPosition)
Create the EventHub receiver with given partition id and start receiving from the specified starting offset.
|
CompletableFuture<PartitionReceiver> |
createReceiver(String consumerGroupName,
String partitionId,
EventPosition eventPosition,
ReceiverOptions receiverOptions)
Create the EventHub receiver with given partition id and start receiving from the specified starting offset.
|
String |
getClientId() |
String |
getEventHubName() |
protected Exception |
getLastKnownError() |
CompletableFuture<PartitionRuntimeInformation> |
getPartitionRuntimeInformation(String partitionId)
Retrieves dynamic information about a partition of an event hub (see
PartitionRuntimeInformation for
details. |
CompletableFuture<EventHubRuntimeInformation> |
getRuntimeInformation()
Retrieves general information about an event hub (see
EventHubRuntimeInformation for details). |
CompletableFuture<Void> |
onClose() |
CompletableFuture<Void> |
send(EventData data)
Send
EventData to EventHub. |
CompletableFuture<Void> |
send(EventDataBatch eventDatas)
Send
EventDataBatch to EventHub. |
CompletableFuture<Void> |
send(EventData eventData,
String partitionKey)
Send an '
EventData with a partitionKey' to EventHub. |
CompletableFuture<Void> |
send(Iterable<EventData> eventDatas)
Send a batch of
EventData to EventHub. |
CompletableFuture<Void> |
send(Iterable<EventData> eventDatas,
String partitionKey)
Send a 'batch of
EventData with the same partitionKey' to EventHub. |
protected void |
setClosed() |
protected void |
throwIfClosed() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitclose, closeSync, create, createBatch, createEpochReceiverSync, createEpochReceiverSync, createPartitionSenderSync, createReceiverSync, createReceiverSync, createSync, createSync, sendSync, sendSync, sendSync, sendSync, sendSyncpublic static String USER_AGENT
protected final ScheduledExecutorService executor
public static CompletableFuture<EventHubClient> create(String connectionString, RetryPolicy retryPolicy, ScheduledExecutorService executor) throws EventHubException, IOException
EventHubClientEventHubClient using the supplied connectionString.
In a normal scenario (when re-direct is not enabled) - one EventHubClient instance maps to one Connection to the Azure ServiceBus EventHubs service.
The EventHubClient created from this method creates a Sender instance internally, which is used by the EventHubClient.send(EventData) methods.
create in interface EventHubClientconnectionString - The connection string to be used. See ConnectionStringBuilder to construct a connectionString.retryPolicy - A custom RetryPolicy to be used when communicating with EventHub.executor - An ScheduledExecutorService to run all tasks performed by EventHubClient.EventHubException - If Service Bus service encountered problems during connection creation.IOException - If the underlying Proton-J layer encounter network errors.public String getEventHubName()
getEventHubName in interface EventHubClientpublic final EventDataBatch createBatch(BatchOptions options) throws EventHubException
EventHubClientEventData.
The same partitionKey must be used while sending these events using EventHubClient.send(EventDataBatch).createBatch in interface EventHubClientoptions - see BatchOptions for more detailsEventDataBatch, after negotiating maximum message size with EventHubs serviceEventHubException - if the Microsoft Azure Event Hubs service encountered problems during the operation.public final CompletableFuture<Void> send(EventData data)
EventHubClientEventData to EventHub. The sent EventData will land on any arbitrarily chosen EventHubs partition.
There are 3 ways to send to EventHubs, each exposed as a method (along with its sendBatch overload):
EventHubClient.send(EventData), EventHubClient.send(Iterable), or EventHubClient.send(EventDataBatch)
EventHubClient.send(EventData, String) or EventHubClient.send(Iterable, String)
PartitionSender.send(EventData), PartitionSender.send(Iterable), or PartitionSender.send(EventDataBatch)
Use this method to Send, if:
a) the send(EventData) operation should be highly available and
b) the data needs to be evenly distributed among all partitions; exception being, when a subset of partitions are unavailable
EventHubClient.send(EventData) send's the EventData to a Service Gateway, which in-turn will forward the EventData to one of the EventHubs' partitions. Here's the message forwarding algorithm:
i. Forward theEventData's to EventHub partitions, by equally distributing the data among all partitions (ex: Round-robin theEventData's to all EventHubs' partitions) ii. If one of the EventHub partitions is unavailable for a moment, the Service Gateway will automatically detect it and forward the message to another available partition - making the Send operation highly-available.
send in interface EventHubClientdata - the EventData to be sent.EventHubClient.send(EventData, String),
PartitionSender.send(EventData)public final CompletableFuture<Void> send(Iterable<EventData> eventDatas)
EventHubClientEventData to EventHub. The sent EventData will land on any arbitrarily chosen EventHubs partition.
This is the most recommended way to Send to EventHubs.
There are 3 ways to send to EventHubs, to understand this particular type of Send refer to the overload EventHubClient.send(EventData), which is used to send single EventData.
Use this overload versus EventHubClient.send(EventData), if you need to send a batch of EventData.
Sending a batch of EventData's is useful in the following cases:
i. Efficient send - sending a batch ofEventDatamaximizes the overall throughput by optimally using the number of sessions created to EventHubs' service. ii. Send multipleEventData's in a Transaction. To achieve ACID properties, the Gateway Service will forward allEventData's in the batch to a single EventHubs' partition.
Sample code (sample uses sync version of the api but concept are identical):
Gson gson = new GsonBuilder().create();
EventHubClient client = EventHubClient.createSync("__connection__");
while (true)
{
LinkedListEventData events = new LinkedListEventData();}
for (int count = 1; count 11; count++)
{
PayloadEvent payload = new PayloadEvent(count);
byte[] payloadBytes = gson.toJson(payload).getBytes(Charset.defaultCharset());
EventData sendEvent = new EventData(payloadBytes);
sendEvent.getProperties().put("from", "javaClient");
events.add(sendEvent);
}
client.sendSync(events);
System.out.println(String.format("Sent Batch... Size: %s", events.size()));
}
for Exceptions refer to EventHubClient.sendSync(Iterable)
send in interface EventHubClienteventDatas - batch of events to send to EventHubEventHubClient.send(EventData, String),
PartitionSender.send(EventData)public final CompletableFuture<Void> send(EventDataBatch eventDatas)
EventHubClientEventDataBatch to EventHub. The sent EventDataBatch will land according the partition key
set in the EventDataBatch. If a partition key is not set, then we will Round-robin the EventData's
to all EventHubs' partitions.send in interface EventHubClienteventDatas - EventDataBatch to send to EventHubEventHubClient.send(Iterable),
EventDataBatchpublic final CompletableFuture<Void> send(EventData eventData, String partitionKey)
EventHubClientEventData with a partitionKey' to EventHub. All EventData's with a partitionKey are guaranteed to land on the same partition.
This send pattern emphasize data correlation over general availability and latency.
There are 3 ways to send to EventHubs, each exposed as a method (along with its sendBatch overload):
i.EventHubClient.send(EventData)orEventHubClient.send(Iterable)ii.EventHubClient.send(EventData, String)orEventHubClient.send(Iterable, String)iii.PartitionSender.send(EventData)orPartitionSender.send(Iterable)
Use this type of Send, if:
i. There is a need for correlation of events based on Sender instance; The sender can generate a UniqueId and set it as partitionKey - which on the received Message can be used for correlation ii. The client wants to take control of distribution of data across partitions.
Multiple PartitionKey's could be mapped to one Partition. EventHubs service uses a proprietary Hash algorithm to map the PartitionKey to a PartitionId. Using this type of Send (Sending using a specific partitionKey), could sometimes result in partitions which are not evenly distributed.
send in interface EventHubClienteventData - the EventData to be sent.partitionKey - the partitionKey will be hash'ed to determine the partitionId to send the eventData to. On the Received message this can be accessed at EventData.SystemProperties.getPartitionKey()EventHubClient.send(EventData),
PartitionSender.send(EventData)public final CompletableFuture<Void> send(Iterable<EventData> eventDatas, String partitionKey)
EventHubClientEventData with the same partitionKey' to EventHub. All EventData's with a partitionKey are guaranteed to land on the same partition.
Multiple PartitionKey's will be mapped to one Partition.
There are 3 ways to send to EventHubs, to understand this particular type of Send refer to the overload EventHubClient.send(EventData, String), which is the same type of Send and is used to send single EventData.
Sending a batch of EventData's is useful in the following cases:
i. Efficient send - sending a batch of EventData maximizes the overall throughput by optimally using the number of sessions created to EventHubs service.
ii. Send multiple events in One Transaction. This is the reason why all events sent in a batch needs to have same partitionKey (so that they are sent to one partition only).
send in interface EventHubClienteventDatas - the batch of events to send to EventHubpartitionKey - the partitionKey will be hash'ed to determine the partitionId to send the eventData to. On the Received message this can be accessed at EventData.SystemProperties.getPartitionKey()EventHubClient.send(EventData),
PartitionSender.send(EventData)public final CompletableFuture<PartitionSender> createPartitionSender(String partitionId) throws EventHubException
EventHubClientPartitionSender which can publish EventData's directly to a specific EventHub partition (sender type iii. in the below list).
There are 3 patterns/ways to send to EventHubs:
i.EventHubClient.send(EventData)orEventHubClient.send(Iterable)ii.EventHubClient.send(EventData, String)orEventHubClient.send(Iterable, String)iii.PartitionSender.send(EventData)orPartitionSender.send(Iterable)
createPartitionSender in interface EventHubClientpartitionId - partitionId of EventHub to send the EventData's toEventHubException - if Service Bus service encountered problems during connection creation.PartitionSenderpublic final CompletableFuture<PartitionReceiver> createReceiver(String consumerGroupName, String partitionId, EventPosition eventPosition) throws EventHubException
EventHubClientcreateReceiver in interface EventHubClientconsumerGroupName - the consumer group name that this receiver should be grouped under.partitionId - the partition Id that the receiver belongs to. All data received will be from this partition only.eventPosition - the position to start receiving the events from. See EventPositionEventHubException - if Service Bus service encountered problems during the operation.PartitionReceiverpublic final CompletableFuture<PartitionReceiver> createReceiver(String consumerGroupName, String partitionId, EventPosition eventPosition, ReceiverOptions receiverOptions) throws EventHubException
EventHubClientcreateReceiver in interface EventHubClientconsumerGroupName - the consumer group name that this receiver should be grouped under.partitionId - the partition Id that the receiver belongs to. All data received will be from this partition only.eventPosition - the position to start receiving the events from. See EventPositionreceiverOptions - the set of options to enable on the event hubs receiverEventHubException - if Service Bus service encountered problems during the operation.PartitionReceiverpublic final CompletableFuture<PartitionReceiver> createEpochReceiver(String consumerGroupName, String partitionId, EventPosition eventPosition, long epoch) throws EventHubException
EventHubClientIt is important to pay attention to the following when creating epoch based receiver:
createEpochReceiver in interface EventHubClientconsumerGroupName - the consumer group name that this receiver should be grouped under.partitionId - the partition Id that the receiver belongs to. All data received will be from this partition only.eventPosition - the position to start receiving the events from. See EventPositionepoch - an unique identifier (epoch value) that the service uses, to enforce partition/lease ownership.EventHubException - if Service Bus service encountered problems during the operation.PartitionReceiver,
ReceiverDisconnectedExceptionpublic final CompletableFuture<PartitionReceiver> createEpochReceiver(String consumerGroupName, String partitionId, EventPosition eventPosition, long epoch, ReceiverOptions receiverOptions) throws EventHubException
EventHubClientIt is important to pay attention to the following when creating epoch based receiver:
createEpochReceiver in interface EventHubClientconsumerGroupName - the consumer group name that this receiver should be grouped under.partitionId - the partition Id that the receiver belongs to. All data received will be from this partition only.eventPosition - the position to start receiving the events from. See EventPositionepoch - an unique identifier (epoch value) that the service uses, to enforce partition/lease ownership.receiverOptions - the set of options to enable on the event hubs receiverEventHubException - if Service Bus service encountered problems during the operation.PartitionReceiver,
ReceiverDisconnectedExceptionpublic CompletableFuture<Void> onClose()
public CompletableFuture<EventHubRuntimeInformation> getRuntimeInformation()
EventHubClientEventHubRuntimeInformation for details).
Retries until it reaches the operation timeout, then either rethrows the last error if available or
returns null to indicate timeout.getRuntimeInformation in interface EventHubClientpublic CompletableFuture<PartitionRuntimeInformation> getPartitionRuntimeInformation(String partitionId)
EventHubClientPartitionRuntimeInformation for
details. Retries until it reaches the operation timeout, then either rethrows the last error if available or
returns null to indicate timeout.getPartitionRuntimeInformation in interface EventHubClientpartitionId - Partition to get information about. Must be one of the partition ids returned by EventHubClient.getRuntimeInformation().public String getClientId()
protected final void setClosed()
public final CompletableFuture<Void> close()
public final void closeSync()
throws EventHubException
EventHubExceptionprotected final void throwIfClosed()
protected Exception getLastKnownError()
Copyright © 2019. All rights reserved.