com.microsoft.azure.eventhubs.EventHubException Java Examples

The following examples show how to use com.microsoft.azure.eventhubs.EventHubException. 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 File: EventHubConsoleConsumer.java    From samza with 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 File: SamzaEventHubClientManager.java    From samza with 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 File: PutAzureEventHub.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * @param namespace name of the Eventhub namespace (part of the domain name)
 * @param eventHubName name of the eventhub, a message broker entity. Like topic.
 * @param policyName technically it is username bound to eventhub namespace or hub and privileges.
 * @param policyKey password belonging to the above policy
 * @param executor thread executor to perform the client connection.
 * @return An initialized eventhub client based on supplied parameters.
 * @throws ProcessException when creation of event hub fails due to formatting of conection string. Authorization or even network connectivity.
 */
protected EventHubClient createEventHubClient(
    final String namespace,
    final String eventHubName,
    final String policyName,
    final String policyKey,
    final ScheduledExecutorService executor)
    throws ProcessException{

    try {
        EventHubClientImpl.USER_AGENT = "ApacheNiFi-azureeventhub/3.1.1";
        final String connectionString;
        if(policyName == AzureEventHubUtils.MANAGED_IDENTITY_POLICY) {
            connectionString = AzureEventHubUtils.getManagedIdentityConnectionString(namespace, eventHubName);
        } else{
            connectionString = getConnectionString(namespace, eventHubName, policyName, policyKey);
        }
        return EventHubClient.createFromConnectionStringSync(connectionString, executor);
    } catch (IOException | EventHubException | IllegalConnectionStringFormatException e) {
        getLogger().error("Failed to create EventHubClient due to {}", new Object[]{e.getMessage()}, e);
        throw new ProcessException(e);
    }
}
 
Example #4
Source File: GetAzureEventHub.java    From nifi with Apache License 2.0 6 votes vote down vote up
@OnStopped
public void tearDown() throws ProcessException {
    for (final PartitionReceiver receiver : partitionToReceiverMap.values()) {
        if (null != receiver) {
            receiver.close();
        }
    }

    partitionToReceiverMap.clear();
    try {
        if (null != eventHubClient) {
            eventHubClient.closeSync();
        }
        executor.shutdown();
    } catch (final EventHubException e) {
        throw new ProcessException(e);
    }
}
 
Example #5
Source File: EventHubConsoleConsumer.java    From samza with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)
    throws EventHubException, IOException, ExecutionException, InterruptedException {
  Options options = new Options();
  options.addOption(
      CommandLineHelper.createOption(OPT_SHORT_EVENTHUB_NAME, OPT_LONG_EVENTHUB_NAME, OPT_ARG_EVENTHUB_NAME, true,
          OPT_DESC_EVENTHUB_NAME));

  options.addOption(CommandLineHelper.createOption(OPT_SHORT_NAMESPACE, OPT_LONG_NAMESPACE, OPT_ARG_NAMESPACE, true,
      OPT_DESC_NAMESPACE));

  options.addOption(CommandLineHelper.createOption(OPT_SHORT_KEY_NAME, OPT_LONG_KEY_NAME, OPT_ARG_KEY_NAME, true,
      OPT_DESC_KEY_NAME));

  options.addOption(
      CommandLineHelper.createOption(OPT_SHORT_TOKEN, OPT_LONG_TOKEN, OPT_ARG_TOKEN, true, OPT_DESC_TOKEN));

  CommandLineParser parser = new BasicParser();
  CommandLine cmd;
  try {
    cmd = parser.parse(options, args);
  } catch (Exception e) {
    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp(String.format("Error: %s%neh-console-consumer.sh", e.getMessage()), options);
    return;
  }

  String ehName = cmd.getOptionValue(OPT_SHORT_EVENTHUB_NAME);
  String namespace = cmd.getOptionValue(OPT_SHORT_NAMESPACE);
  String keyName = cmd.getOptionValue(OPT_SHORT_KEY_NAME);
  String token = cmd.getOptionValue(OPT_SHORT_TOKEN);

  consumeEvents(ehName, namespace, keyName, token);
}
 
Example #6
Source File: EventHubSystemConsumer.java    From samza with Apache License 2.0 5 votes vote down vote up
@Override
public void onError(Throwable throwable) {
  errorRate.inc();
  aggReadErrors.inc();

  if (throwable instanceof EventHubException) {
    EventHubException busException = (EventHubException) throwable;

    if (busException.getIsTransient()) {
      LOG.warn(
          String.format("Received transient exception from EH client. Renew partition receiver for ssp: %s", ssp),
          throwable);
      try {
        // Add a fixed delay so that we don't keep retrying when there are long-lasting failures
        TimeUnit.SECONDS.sleep(2);
      } catch (InterruptedException e) {
        LOG.warn("Interrupted during sleep before renew", e);
      }
      // Retry creating a receiver since error likely due to timeout
      renewPartitionReceiver(ssp);
      return;
    }
  }

  LOG.error(String.format("Received non transient exception from EH client for ssp: %s", ssp), throwable);
  // Propagate non transient or unknown errors
  eventHubNonTransientError.set(throwable);
}
 
Example #7
Source File: ITestEventHubSystemProducer.java    From samza with Apache License 2.0 5 votes vote down vote up
@Test
public void testReceive() throws EventHubException {
  EventHubClientManagerFactory clientFactory = new EventHubClientManagerFactory();
  EventHubClientManager wrapper = clientFactory
          .getEventHubClientManager(SYSTEM_NAME, STREAM_NAME1, new EventHubConfig(createEventHubConfig()));
  wrapper.init();
  EventHubClient client = wrapper.getEventHubClient();
  PartitionReceiver receiver =
          client.createReceiverSync(EventHubClient.DEFAULT_CONSUMER_GROUP_NAME, "0",
                  EventPosition.fromStartOfStream());
  receiveMessages(receiver, 300);
}
 
Example #8
Source File: ITestEventHubSystemProducer.java    From samza with Apache License 2.0 5 votes vote down vote up
private void receiveMessages(PartitionReceiver receiver, int numMessages) throws EventHubException {
  int count = 0;
  while (count < numMessages) {

    Iterable<EventData> messages = receiver.receiveSync(100);
    if (messages == null) {
      break;
    }
    for (EventData data : messages) {
      count++;
      LOG.info("Data" + new String(data.getBytes()));
    }
  }
}
 
Example #9
Source File: TestEventHubSystemAdmin.java    From samza with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartpointResolverShouldResolveTheStartpointTimestampToCorrectOffset() throws EventHubException {
  // Initialize variables required for testing.
  EventHubSystemAdmin mockEventHubSystemAdmin = Mockito.mock(EventHubSystemAdmin.class);
  EventHubConfig eventHubConfig = Mockito.mock(EventHubConfig.class);
  SystemStreamPartition systemStreamPartition = new SystemStreamPartition("test-system", "test-stream", new Partition(0));
  String mockedOffsetToReturn = "100";

  // Setup the mock variables.
  EventHubClientManager mockEventHubClientManager = Mockito.mock(EventHubClientManager.class);
  EventHubClient mockEventHubClient = Mockito.mock(EventHubClient.class);
  PartitionReceiver mockPartitionReceiver = Mockito.mock(PartitionReceiver.class);
  EventData mockEventData = Mockito.mock(EventData.class);
  EventData.SystemProperties mockSystemProperties = Mockito.mock(EventData.SystemProperties.class);

  // Configure the mock variables to return the appropriate values.
  Mockito.when(mockEventHubSystemAdmin.getOrCreateStreamEventHubClient("test-stream")).thenReturn(mockEventHubClientManager);
  Mockito.when(mockEventHubClientManager.getEventHubClient()).thenReturn(mockEventHubClient);
  Mockito.when(mockEventHubClient.createReceiverSync(Mockito.anyString(), Mockito.anyString(), Mockito.any())).thenReturn(mockPartitionReceiver);
  Mockito.when(mockPartitionReceiver.receiveSync(1)).thenReturn(Arrays.asList(mockEventData));
  Mockito.when(mockEventData.getSystemProperties()).thenReturn(mockSystemProperties);
  Mockito.when(mockSystemProperties.getOffset()).thenReturn(mockedOffsetToReturn);

  // Test the Offset resolver.
  EventHubSamzaOffsetResolver resolver = new EventHubSamzaOffsetResolver(mockEventHubSystemAdmin, eventHubConfig);
  String resolvedOffset = resolver.visit(systemStreamPartition, new StartpointTimestamp(100L));
  Assert.assertEquals(mockedOffsetToReturn, resolvedOffset);
}
 
Example #10
Source File: EventHubCommon.java    From datacollector with 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 #11
Source File: GetAzureEventHub.java    From nifi with Apache License 2.0 5 votes vote down vote up
protected void setupReceiver(final String connectionString, final ScheduledExecutorService executor) throws ProcessException {
    try {
        EventHubClientImpl.USER_AGENT = "ApacheNiFi-azureeventhub/3.1.1";
        eventHubClient = EventHubClient.createFromConnectionStringSync(connectionString, executor);
    } catch (IOException | EventHubException e) {
        throw new ProcessException(e);
    }
}
 
Example #12
Source File: GetAzureEventHub.java    From nifi with Apache License 2.0 5 votes vote down vote up
PartitionReceiver getReceiver(final ProcessContext context, final String partitionId) throws IOException, EventHubException, ExecutionException, InterruptedException {
    PartitionReceiver existingReceiver = partitionToReceiverMap.get(partitionId);
    if (existingReceiver != null) {
        return existingReceiver;
    }

    // we want to avoid allowing multiple threads to create Receivers simultaneously because that could result in
    // having multiple Receivers for the same partition. So if the map does not contain a receiver for this partition,
    // we will enter a synchronized block and check again (because once we enter the synchronized block, we know that no
    // other thread is creating a client). If within the synchronized block, we still do not have an entry in the map,
    // it is up to use to create the receiver, initialize it, and then put it into the map.
    // We do not use the putIfAbsent method in order to do a CAS operation here because we want to also initialize the
    // receiver if and only if it is not present in the map. As a result, we need to initialize the receiver and add it
    // to the map atomically. Hence, the synchronized block.
    synchronized (this) {
        existingReceiver = partitionToReceiverMap.get(partitionId);
        if (existingReceiver != null) {
            return existingReceiver;
        }

        final String consumerGroupName = context.getProperty(CONSUMER_GROUP).getValue();

        final PartitionReceiver receiver = eventHubClient.createReceiver(
                consumerGroupName,
                partitionId,
                EventPosition.fromEnqueuedTime(
                        configuredEnqueueTime == null ? Instant.now() : configuredEnqueueTime)).get();

        receiver.setReceiveTimeout(receiverFetchTimeout == null ? Duration.ofMillis(60000) : receiverFetchTimeout);
        partitionToReceiverMap.put(partitionId, receiver);
        return receiver;

    }
}
 
Example #13
Source File: GetAzureEventHubTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected PartitionReceiver getReceiver(final ProcessContext context, final String partitionId) throws IOException, EventHubException, ExecutionException, InterruptedException {
    if(getReceiverThrow){
        throw new IOException("Could not create receiver");
    }
    return null;
}
 
Example #14
Source File: GetAzureEventHub.java    From nifi with Apache License 2.0 3 votes vote down vote up
/**
 * This method is here to try and isolate the Azure related code as the PartitionReceiver cannot be mocked
 * with PowerMock due to it being final. Unfortunately it extends a base class and does not implement an interface
 * so even if we create a MockPartitionReciver, it will not work as the two classes are orthogonal.
 *
 * @param context     - The processcontext for this processor
 * @param partitionId - The partition ID to retrieve a receiver by.
 * @return - Returns the events received from the EventBus.
 * @throws ProcessException -- If any exception is encountered, receiving events it is wrapped in a ProcessException
 *                          and then that exception is thrown.
 */
protected Iterable<EventData> receiveEvents(final ProcessContext context, final String partitionId) throws ProcessException {
    final PartitionReceiver receiver;
    try {
        receiver = getReceiver(context, partitionId);
        return receiver.receive(receiverFetchSize).get();
    } catch (final EventHubException | IOException | ExecutionException | InterruptedException e) {
        throw new ProcessException(e);
    }
}