PartitionReceiver getReceiver(final ProcessContext context, final String partitionId) throws IOException, ServiceBusException, 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, configuredEnqueueTime == null ? Instant.now() : configuredEnqueueTime).get(); receiver.setReceiveTimeout(receiverFetchTimeout == null ? Duration.ofMillis(60000) : receiverFetchTimeout); partitionToReceiverMap.put(partitionId, receiver); return receiver; } }
/** * Synchronous version of {@link #createReceiver(String, String, EventPosition)}. * * @param consumerGroupName the consumer group name that this receiver should be grouped under. * @param partitionId the partition Id that the receiver belongs to. All data received will be from this partition only. * @param eventPosition the position to start receiving the events from. See {@link EventPosition} * @return PartitionReceiver instance which can be used for receiving {@link EventData}. * @throws EventHubException if Service Bus service encountered problems during the operation. */ default PartitionReceiver createReceiverSync(final String consumerGroupName, final String partitionId, final EventPosition eventPosition) throws EventHubException { return ExceptionUtil.sync(() -> this.createReceiver(consumerGroupName, partitionId, eventPosition).get()); }
/** * Synchronous version of {@link #createReceiver(String, String, EventPosition)}. * * @param consumerGroupName the consumer group name that this receiver should be grouped under. * @param partitionId the partition Id that the receiver belongs to. All data received will be from this partition only. * @param eventPosition the position to start receiving the events from. See {@link EventPosition} * @param receiverOptions the set of options to enable on the event hubs receiver * @return PartitionReceiver instance which can be used for receiving {@link EventData}. * @throws EventHubException if Service Bus service encountered problems during the operation. */ default PartitionReceiver createReceiverSync(final String consumerGroupName, final String partitionId, final EventPosition eventPosition, final ReceiverOptions receiverOptions) throws EventHubException { return ExceptionUtil.sync(() -> this.createReceiver(consumerGroupName, partitionId, eventPosition, receiverOptions).get()); }
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; }
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; }