Interface PartitionReceiver
-
public interface PartitionReceiver
This is a logical representation of receiving from a EventHub partition.A
PartitionReceiver
is tied to a ConsumerGroup + EventHub Partition combination.- If an epoch based
PartitionReceiver
(i.e., PartitionReceiver.getEpoch != 0) is created, EventHubs service will guarantee only 1 active receiver exists per ConsumerGroup + Partition combo. This is the recommended approach to create aPartitionReceiver
. - Multiple receivers per ConsumerGroup + Partition combo can be created using non-epoch receivers.
- If an epoch based
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_PREFETCH_COUNT
static int
MAXIMUM_PREFETCH_COUNT
static int
MINIMUM_PREFETCH_COUNT
static long
NULL_EPOCH
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description CompletableFuture<Void>
close()
void
closeSync()
long
getEpoch()
Get the epoch value that this receiver is currently using for partition ownership.EventPosition
getEventPosition()
Get theEventPosition
that corresponds to anEventData
which was returned last by the receiver.boolean
getIsOpen()
Determine the current state of the receiver.String
getPartitionId()
Get EventHubs partition identifier.Duration
getReceiveTimeout()
ReceiverRuntimeInformation
getRuntimeInformation()
Gets the temporalReceiverRuntimeInformation
for this EventHub partition.CompletableFuture<Iterable<EventData>>
receive(int maxEventCount)
Receive a batch ofEventData
's from an EventHub partitiondefault Iterable<EventData>
receiveSync(int maxEventCount)
Synchronous version ofreceive(int)
.CompletableFuture<Void>
setReceiveHandler(PartitionReceiveHandler receiveHandler)
Register a receive handler that will be called when an event is available.CompletableFuture<Void>
setReceiveHandler(PartitionReceiveHandler receiveHandler, boolean invokeWhenNoEvents)
Register a receive handler that will be called when an event is available.void
setReceiveTimeout(Duration value)
-
-
-
Field Detail
-
MINIMUM_PREFETCH_COUNT
static final int MINIMUM_PREFETCH_COUNT
- See Also:
- Constant Field Values
-
DEFAULT_PREFETCH_COUNT
static final int DEFAULT_PREFETCH_COUNT
- See Also:
- Constant Field Values
-
MAXIMUM_PREFETCH_COUNT
static final int MAXIMUM_PREFETCH_COUNT
- See Also:
- Constant Field Values
-
NULL_EPOCH
static final long NULL_EPOCH
- See Also:
- Constant Field Values
-
-
Method Detail
-
getPartitionId
String getPartitionId()
Get EventHubs partition identifier.- Returns:
- The identifier representing the partition from which this receiver is fetching data
-
getReceiveTimeout
Duration getReceiveTimeout()
-
setReceiveTimeout
void setReceiveTimeout(Duration value)
-
getIsOpen
boolean getIsOpen()
Determine the current state of the receiver.- Returns:
- false if the receiver is closing or has been closed, true if the receiver is open and ready to use.
-
getEpoch
long getEpoch()
Get the epoch value that this receiver is currently using for partition ownership.A value of 0 means this receiver is not an epoch-based receiver.
- Returns:
- the epoch value that this receiver is currently using for partition ownership.
-
getRuntimeInformation
ReceiverRuntimeInformation getRuntimeInformation()
Gets the temporalReceiverRuntimeInformation
for this EventHub partition. In general, this information is a representation of, where thisPartitionReceiver
's end of stream is, at the timeReceiverRuntimeInformation.getRetrievalTime()
.This value will not be populated, unless the knob
ReceiverOptions.setReceiverRuntimeMetricEnabled(boolean)
is set. This value will be refreshed every time anEventData
is consumed fromPartitionReceiver
. For ex: if no events have been consumed, then this value is not populated.- Returns:
- receiver runtime information
-
getEventPosition
EventPosition getEventPosition()
Get theEventPosition
that corresponds to anEventData
which was returned last by the receiver.This value will not be populated, unless the knob
ReceiverOptions.setReceiverRuntimeMetricEnabled(boolean)
is set. Note that EventPosition object is initialized using SequenceNumber and other parameters are not set and get will return null.- Returns:
- the EventPosition object.
-
receiveSync
default Iterable<EventData> receiveSync(int maxEventCount) throws EventHubException
Synchronous version ofreceive(int)
.- Parameters:
maxEventCount
- maximum number ofEventData
's that this call should return- Returns:
- Batch of
EventData
's from the partition on which this receiver is created. Returns 'null' if noEventData
is present. - Throws:
EventHubException
- if ServiceBus client encountered any unrecoverable/non-transient problems duringreceive(int)
-
receive
CompletableFuture<Iterable<EventData>> receive(int maxEventCount)
Receive a batch ofEventData
's from an EventHub partitionSample code (sample uses sync version of the api but concept are identical):
EventHubClient client = EventHubClient.createSync("__connection__"); PartitionReceiver receiver = client.createPartitionReceiverSync("ConsumerGroup1", "1"); Iterable<EventData> receivedEvents = receiver.receiveSync(); while (true) { int batchSize = 0; if (receivedEvents != null) { for(EventData receivedEvent: receivedEvents) { System.out.println(String.format("Message Payload: %s", new String(receivedEvent.getBytes(), Charset.defaultCharset()))); System.out.println(String.format("Offset: %s, SeqNo: %s, EnqueueTime: %s", receivedEvent.getSystemProperties().getOffset(), receivedEvent.getSystemProperties().getSequenceNumber(), receivedEvent.getSystemProperties().getEnqueuedTime())); batchSize++; } } System.out.println(String.format("ReceivedBatch Size: %s", batchSize)); receivedEvents = receiver.receiveSync(); }
-
setReceiveHandler
CompletableFuture<Void> setReceiveHandler(PartitionReceiveHandler receiveHandler)
Register a receive handler that will be called when an event is available. APartitionReceiveHandler
is a handler that allows user to specify a callback for event processing and error handling in a receive pump model.- Parameters:
receiveHandler
- An implementation ofPartitionReceiveHandler
. Setting this handler tonull
will stop the receive pump.- Returns:
- A completableFuture which sets receiveHandler
-
setReceiveHandler
CompletableFuture<Void> setReceiveHandler(PartitionReceiveHandler receiveHandler, boolean invokeWhenNoEvents)
Register a receive handler that will be called when an event is available. APartitionReceiveHandler
is a handler that allows user to specify a callback for event processing and error handling in a receive pump model.- Parameters:
receiveHandler
- An implementation ofPartitionReceiveHandler
invokeWhenNoEvents
- flag to indicate whether thePartitionReceiveHandler.onReceive(Iterable)
should be invoked when the receive call times out- Returns:
- A completableFuture which sets receiveHandler
-
close
CompletableFuture<Void> close()
-
closeSync
void closeSync() throws EventHubException
- Throws:
EventHubException
-
-