Search in sources :

Example 1 with ConnectionStringBuilder

use of com.microsoft.azure.servicebus.ConnectionStringBuilder 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)

Example 2 with ConnectionStringBuilder

use of com.microsoft.azure.servicebus.ConnectionStringBuilder in project nifi by apache.

the class GetAzureEventHub method onScheduled.

@OnScheduled
public void onScheduled(final ProcessContext context) throws ProcessException, URISyntaxException {
    final BlockingQueue<String> partitionNames = new LinkedBlockingQueue<>();
    for (int i = 0; i < context.getProperty(NUM_PARTITIONS).asInteger(); i++) {
        partitionNames.add(String.valueOf(i));
    }
    this.partitionNames = partitionNames;
    final String policyName = context.getProperty(ACCESS_POLICY).getValue();
    final String policyKey = context.getProperty(POLICY_PRIMARY_KEY).getValue();
    final String namespace = context.getProperty(NAMESPACE).getValue();
    final String eventHubName = context.getProperty(EVENT_HUB_NAME).getValue();
    final String serviceBusEndpoint = context.getProperty(SERVICE_BUS_ENDPOINT).getValue();
    if (context.getProperty(ENQUEUE_TIME).isSet()) {
        configuredEnqueueTime = Instant.parse(context.getProperty(ENQUEUE_TIME).toString());
    } else {
        configuredEnqueueTime = null;
    }
    if (context.getProperty(RECEIVER_FETCH_SIZE).isSet()) {
        receiverFetchSize = context.getProperty(RECEIVER_FETCH_SIZE).asInteger();
    } else {
        receiverFetchSize = 100;
    }
    if (context.getProperty(RECEIVER_FETCH_TIMEOUT).isSet()) {
        receiverFetchTimeout = Duration.ofMillis(context.getProperty(RECEIVER_FETCH_TIMEOUT).asLong());
    } else {
        receiverFetchTimeout = null;
    }
    final String connectionString = new ConnectionStringBuilder(new URI("amqps://" + namespace + serviceBusEndpoint), eventHubName, policyName, policyKey).toString();
    setupReceiver(connectionString);
}
Also used : ConnectionStringBuilder(com.microsoft.azure.servicebus.ConnectionStringBuilder) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) URI(java.net.URI) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Aggregations

ConnectionStringBuilder (com.microsoft.azure.servicebus.ConnectionStringBuilder)2 EventProcessorHost (com.microsoft.azure.eventprocessorhost.EventProcessorHost)1 EventProcessorOptions (com.microsoft.azure.eventprocessorhost.EventProcessorOptions)1 IEventProcessorFactory (com.microsoft.azure.eventprocessorhost.IEventProcessorFactory)1 URI (java.net.URI)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 OnScheduled (org.apache.nifi.annotation.lifecycle.OnScheduled)1