Java Code Examples for com.microsoft.azure.eventhubs.ConnectionStringBuilder

The following examples show how to use com.microsoft.azure.eventhubs.ConnectionStringBuilder. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: samza   Source File: EventHubConsoleConsumer.java    License: Apache License 2.0 6 votes vote down vote up
private static void consumeEvents(String ehName, String namespace, String keyName, String token)
    throws EventHubException, IOException, ExecutionException, InterruptedException {
  ConnectionStringBuilder connStr = new ConnectionStringBuilder().setNamespaceName(namespace)
      .setEventHubName(ehName)
      .setSasKeyName(keyName)
      .setSasKey(token);

  EventHubClient client = EventHubClient.createSync(connStr.toString(), Executors.newFixedThreadPool(10));

  EventHubRuntimeInformation runTimeInfo = client.getRuntimeInformation().get();
  int numPartitions = runTimeInfo.getPartitionCount();
  for (int partition = 0; partition < numPartitions; partition++) {
    PartitionReceiver receiver =
        client.createReceiverSync(EventHubClient.DEFAULT_CONSUMER_GROUP_NAME, String.valueOf(partition),
            EventPosition.fromStartOfStream());
    receiver.receive(10).handle((records, throwable) -> handleComplete(receiver, records, throwable));
  }
}
 
Example 2
Source Project: samza   Source File: SamzaEventHubClientManager.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void init() {
  String remoteHost = String.format(EVENTHUB_REMOTE_HOST_FORMAT, eventHubNamespace);
  LOG.info("Initializing SamzaEventHubClientManager for namespace: " + eventHubNamespace);
  try {
    ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder()
        .setNamespaceName(eventHubNamespace)
        .setEventHubName(entityPath)
        .setSasKeyName(sasKeyName)
        .setSasKey(sasKey);

    ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder().setNameFormat("Samza EventHubClient Thread-%d").setDaemon(true);
    eventHubClientExecutor = Executors.newFixedThreadPool(numClientThreads, threadFactoryBuilder.build());
    eventHubClient = EventHubClient.createSync(connectionStringBuilder.toString(), retryPolicy, eventHubClientExecutor);
  } catch (IOException | EventHubException e) {
    String msg = String.format("Creation of EventHub client failed for eventHub EntityPath: %s on remote host %s:%d",
            entityPath, remoteHost, ClientConstants.AMQPS_PORT);
    LOG.error(msg, e);
    throw new SamzaException(msg, e);
  }
  LOG.info("SamzaEventHubClientManager initialized for namespace: " + eventHubNamespace);
}
 
Example 3
Source Project: datacollector   Source File: EventHubCommon.java    License: Apache License 2.0 5 votes vote down vote up
public EventHubClient createEventHubClient(String threadNamePattern) throws IOException, EventHubException {
  final ConnectionStringBuilder connStr = new ConnectionStringBuilder()
      .setNamespaceName(commonConf.namespaceName)
      .setEventHubName(commonConf.eventHubName)
      .setSasKey(commonConf.sasKey.get())
      .setSasKeyName(commonConf.sasKeyName);


  final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(
      new ThreadFactoryBuilder().setNameFormat(threadNamePattern).build()
  );

  return EventHubClient.createSync(connStr.toString(), scheduledExecutorService);
}
 
Example 4
Source Project: nifi   Source File: AzureEventHubUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static String getSharedAccessSignatureConnectionString(final String namespace, final String eventHubName, final String sasName, final String sasKey) {
    return new ConnectionStringBuilder()
                .setNamespaceName(namespace)
                .setEventHubName(eventHubName)
                .setSasKeyName(sasName)
                .setSasKey(sasKey).toString();
}
 
Example 5
Source Project: nifi   Source File: GetAzureEventHub.java    License: Apache License 2.0 4 votes vote down vote up
@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 namespace = context.getProperty(NAMESPACE).getValue();
    final String eventHubName = context.getProperty(EVENT_HUB_NAME).getValue();
    final String serviceBusEndpoint = context.getProperty(SERVICE_BUS_ENDPOINT).getValue();
    final boolean useManagedIdentity = context.getProperty(USE_MANAGED_IDENTITY).asBoolean();
    final String connectionString;

    if(useManagedIdentity){
        connectionString = AzureEventHubUtils.getManagedIdentityConnectionString(namespace,eventHubName);
    } else {
        final String policyName = context.getProperty(ACCESS_POLICY).getValue();
        final String policyKey = context.getProperty(POLICY_PRIMARY_KEY).getValue();
        connectionString = new ConnectionStringBuilder()
                                .setEndpoint(new URI("amqps://"+namespace+serviceBusEndpoint))
                                .setEventHubName(eventHubName)
                                .setSasKeyName(policyName)
                                .setSasKey(policyKey).toString();
    }

    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;
    }

    executor = Executors.newScheduledThreadPool(4);
    setupReceiver(connectionString, executor);
}
 
Example 6
Source Project: nifi   Source File: AzureEventHubUtils.java    License: Apache License 2.0 4 votes vote down vote up
public static String getManagedIdentityConnectionString(final String namespace, final String eventHubName){
    return new ConnectionStringBuilder().setNamespaceName(namespace).setEventHubName(eventHubName)
                .setAuthentication(MANAGED_IDENTITY_POLICY).toString();
}