Search in sources :

Example 1 with EventProcessorHost

use of com.microsoft.azure.eventprocessorhost.EventProcessorHost in project nifi by apache.

the class ConsumeAzureEventHub method registerEventProcessor.

private void registerEventProcessor(final ProcessContext context) throws Exception {
    // Validate required properties.
    final String consumerGroupName = context.getProperty(CONSUMER_GROUP).evaluateAttributeExpressions().getValue();
    validateRequiredProperty(CONSUMER_GROUP, consumerGroupName);
    namespaceName = context.getProperty(NAMESPACE).evaluateAttributeExpressions().getValue();
    validateRequiredProperty(NAMESPACE, namespaceName);
    final String eventHubName = context.getProperty(EVENT_HUB_NAME).evaluateAttributeExpressions().getValue();
    validateRequiredProperty(EVENT_HUB_NAME, eventHubName);
    final String sasName = context.getProperty(ACCESS_POLICY_NAME).evaluateAttributeExpressions().getValue();
    validateRequiredProperty(ACCESS_POLICY_NAME, sasName);
    final String sasKey = context.getProperty(POLICY_PRIMARY_KEY).evaluateAttributeExpressions().getValue();
    validateRequiredProperty(POLICY_PRIMARY_KEY, sasKey);
    final String storageAccountName = context.getProperty(STORAGE_ACCOUNT_NAME).evaluateAttributeExpressions().getValue();
    validateRequiredProperty(STORAGE_ACCOUNT_NAME, storageAccountName);
    final String storageAccountKey = context.getProperty(STORAGE_ACCOUNT_KEY).evaluateAttributeExpressions().getValue();
    validateRequiredProperty(STORAGE_ACCOUNT_KEY, storageAccountKey);
    final String consumerHostname = orDefault(context.getProperty(CONSUMER_HOSTNAME).evaluateAttributeExpressions().getValue(), EventProcessorHost.createHostName("nifi"));
    final String containerName = orDefault(context.getProperty(STORAGE_CONTAINER_NAME).evaluateAttributeExpressions().getValue(), eventHubName);
    final EventProcessorOptions options = new EventProcessorOptions();
    final String initialOffset = context.getProperty(INITIAL_OFFSET).getValue();
    if (INITIAL_OFFSET_START_OF_STREAM.getValue().equals(initialOffset)) {
        options.setInitialOffsetProvider(options.new StartOfStreamInitialOffsetProvider());
    } else if (INITIAL_OFFSET_END_OF_STREAM.getValue().equals(initialOffset)) {
        options.setInitialOffsetProvider(options.new EndOfStreamInitialOffsetProvider());
    } else {
        throw new IllegalArgumentException("Initial offset " + initialOffset + " is not allowed.");
    }
    final Integer prefetchCount = context.getProperty(PREFETCH_COUNT).evaluateAttributeExpressions().asInteger();
    if (prefetchCount != null && prefetchCount > 0) {
        options.setPrefetchCount(prefetchCount);
    }
    final Integer batchSize = context.getProperty(BATCH_SIZE).evaluateAttributeExpressions().asInteger();
    if (batchSize != null && batchSize > 0) {
        options.setMaxBatchSize(batchSize);
    }
    final Long receiveTimeoutMillis = context.getProperty(RECEIVE_TIMEOUT).evaluateAttributeExpressions().asTimePeriod(TimeUnit.MILLISECONDS);
    options.setReceiveTimeOut(Duration.ofMillis(receiveTimeoutMillis));
    final String storageConnectionString = String.format(AzureStorageUtils.FORMAT_BLOB_CONNECTION_STRING, storageAccountName, storageAccountKey);
    final ConnectionStringBuilder eventHubConnectionString = new ConnectionStringBuilder(namespaceName, eventHubName, sasName, sasKey);
    eventProcessorHost = new EventProcessorHost(consumerHostname, eventHubName, consumerGroupName, eventHubConnectionString.toString(), storageConnectionString, containerName);
    options.setExceptionNotification(e -> {
        getLogger().error("An error occurred while receiving messages from Azure Event Hub {}" + " at consumer group {} and partition {}, action={}, hostname={}, exception={}", new Object[] { eventHubName, consumerGroupName, e.getPartitionId(), e.getAction(), e.getHostname() }, e.getException());
    });
    eventProcessorHost.registerEventProcessorFactory(new EventProcessorFactory(), options).get();
}
Also used : EventProcessorOptions(com.microsoft.azure.eventprocessorhost.EventProcessorOptions) ConnectionStringBuilder(com.microsoft.azure.servicebus.ConnectionStringBuilder) EventProcessorHost(com.microsoft.azure.eventprocessorhost.EventProcessorHost) IEventProcessorFactory(com.microsoft.azure.eventprocessorhost.IEventProcessorFactory)

Aggregations

EventProcessorHost (com.microsoft.azure.eventprocessorhost.EventProcessorHost)1 EventProcessorOptions (com.microsoft.azure.eventprocessorhost.EventProcessorOptions)1 IEventProcessorFactory (com.microsoft.azure.eventprocessorhost.IEventProcessorFactory)1 ConnectionStringBuilder (com.microsoft.azure.servicebus.ConnectionStringBuilder)1