use of com.microsoft.azure.eventhubs.PartitionReceiveHandler in project samza by apache.
the class EventHubSystemConsumer method initializeEventHubsManagers.
private synchronized void initializeEventHubsManagers() {
LOG.info("Starting EventHubSystemConsumer. Count of SSPs registered: " + streamPartitionOffsets.entrySet().size());
eventHubNonTransientError.set(null);
// Create receivers for Event Hubs
for (Map.Entry<SystemStreamPartition, String> entry : streamPartitionOffsets.entrySet()) {
SystemStreamPartition ssp = entry.getKey();
String streamId = config.getStreamId(ssp.getStream());
Integer partitionId = ssp.getPartition().getPartitionId();
String offset = entry.getValue();
String consumerGroup = config.getStreamConsumerGroup(systemName, streamId);
String namespace = config.getStreamNamespace(systemName, streamId);
String entityPath = config.getStreamEntityPath(systemName, streamId);
EventHubClientManager eventHubClientManager = createOrGetEventHubClientManagerForSSP(streamId, ssp);
try {
PartitionReceiver receiver;
if (END_OF_STREAM.equals(offset)) {
// If the offset is greater than the newest offset, use the use current Instant as
// offset to fetch in Eventhub.
receiver = eventHubClientManager.getEventHubClient().createReceiver(consumerGroup, partitionId.toString(), EventPosition.fromEnqueuedTime(Instant.now())).get(DEFAULT_EVENTHUB_CREATE_RECEIVER_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
} else {
// EventHub will return the first message AFTER the offset that was specified in the fetch request.
// If no such offset exists Eventhub will return an error.
receiver = eventHubClientManager.getEventHubClient().createReceiver(consumerGroup, partitionId.toString(), EventPosition.fromOffset(offset, /* inclusiveFlag */
false)).get(DEFAULT_EVENTHUB_CREATE_RECEIVER_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
}
receiver.setPrefetchCount(prefetchCount);
PartitionReceiveHandler handler = new PartitionReceiverHandlerImpl(ssp, eventReadRates.get(streamId), eventByteReadRates.get(streamId), consumptionLagMs.get(streamId), readErrors.get(streamId), interceptors.getOrDefault(streamId, null), config.getMaxEventCountPerPoll(systemName));
// Timeout for EventHubClient receive
receiver.setReceiveTimeout(DEFAULT_EVENTHUB_RECEIVER_TIMEOUT);
// Start the receiver thread
receiver.setReceiveHandler(handler);
streamPartitionHandlers.put(ssp, handler);
streamPartitionReceivers.put(ssp, receiver);
} catch (Exception e) {
throw new SamzaException(String.format("Failed to create receiver for EventHubs: namespace=%s, entity=%s, partitionId=%d", namespace, entityPath, partitionId), e);
}
LOG.info(String.format("Connection successfully started for namespace=%s, entity=%s ", namespace, entityPath));
}
}
Aggregations