com.microsoft.azure.eventhubs.EventPosition Java Examples

The following examples show how to use com.microsoft.azure.eventhubs.EventPosition. 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: EventHubSystemConsumer.java    From samza with Apache License 2.0 5 votes vote down vote up
private void renewPartitionReceiver(SystemStreamPartition ssp) {
  String streamId = config.getStreamId(ssp.getStream());
  EventHubClientManager eventHubClientManager = perPartitionEventHubManagers.get(ssp);
  String offset = streamPartitionOffsets.get(ssp);
  Integer partitionId = ssp.getPartition().getPartitionId();
  String consumerGroup = config.getStreamConsumerGroup(ssp.getSystem(), streamId);

  try {
    // Close current receiver
    streamPartitionReceivers.get(ssp).close().get(DEFAULT_SHUTDOWN_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);

    // Recreate receiver
    PartitionReceiver receiver = eventHubClientManager.getEventHubClient()
        .createReceiverSync(consumerGroup, partitionId.toString(),
            EventPosition.fromOffset(offset, !offset.equals(EventHubSystemConsumer.START_OF_STREAM)));

    receiver.setPrefetchCount(prefetchCount);

    // Timeout for EventHubClient receive
    receiver.setReceiveTimeout(DEFAULT_EVENTHUB_RECEIVER_TIMEOUT);

    // Create and start receiver thread with handler
    receiver.setReceiveHandler(streamPartitionHandlers.get(ssp));
    streamPartitionReceivers.put(ssp, receiver);
  } catch (Exception e) {
    eventHubNonTransientError.set(new SamzaException(
        String.format("Failed to recreate receiver for EventHubs after ReceiverHandlerError (ssp=%s)", ssp), e));
  }
}
 
Example #3
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 #4
Source File: AzureIoTService.java    From para with Apache License 2.0 5 votes vote down vote up
private static EventHubClient receiveEventsAsync(final String partitionId) {
	EventHubClient client = null;
	try {
		client = EventHubClient.createSync(EVENTHUB_CONN_STR, Para.getExecutorService());
		client.createReceiver(EventHubClient.DEFAULT_CONSUMER_GROUP_NAME, partitionId,
					EventPosition.fromEnqueuedTime(Instant.now())).
				thenAccept(new Receiver(partitionId));
	} catch (Exception e) {
		logger.warn("Couldn't start receiving messages from Azure cloud: {}", e.getMessage());
	}
	return client;
}
 
Example #5
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 #6
Source File: EventHubSystemConsumer.java    From samza with Apache License 2.0 4 votes vote down vote up
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));
  }
}