Search in sources :

Example 1 with PartitionReceiveHandler

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));
    }
}
Also used : PartitionReceiver(com.microsoft.azure.eventhubs.PartitionReceiver) EventHubClientManager(org.apache.samza.system.eventhub.EventHubClientManager) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BlockingEnvelopeMap(org.apache.samza.util.BlockingEnvelopeMap) PartitionReceiveHandler(com.microsoft.azure.eventhubs.PartitionReceiveHandler) SamzaException(org.apache.samza.SamzaException) EventHubException(com.microsoft.azure.eventhubs.EventHubException) SamzaException(org.apache.samza.SamzaException) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition)

Aggregations

EventHubException (com.microsoft.azure.eventhubs.EventHubException)1 PartitionReceiveHandler (com.microsoft.azure.eventhubs.PartitionReceiveHandler)1 PartitionReceiver (com.microsoft.azure.eventhubs.PartitionReceiver)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 SamzaException (org.apache.samza.SamzaException)1 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)1 EventHubClientManager (org.apache.samza.system.eventhub.EventHubClientManager)1 BlockingEnvelopeMap (org.apache.samza.util.BlockingEnvelopeMap)1