Source:
<code>public static void main(String[] args) { Flux<EventData> telemetryEvents = Flux.just( new EventData("Roast beef".getBytes(UTF_8)), new EventData("Cheese".getBytes(UTF_8)), new EventData("Tofu".getBytes(UTF_8)), new EventData("Turkey".getBytes(UTF_8))); System.setProperty("AZURE_CLIENT_ID", "<<insert-service-principal-client-id>>"); System.setProperty("AZURE_CLIENT_ID", "<<insert-service-principal-client-application-secret>>"); System.setProperty("AZURE_TENANT_ID", "<<insert-service-principal-tenant-id>>"); TokenCredential credential = new DefaultAzureCredentialBuilder() .build(); EventHubProducerAsyncClient producer = new EventHubClientBuilder() .credential( "<<fully-qualified-namespace>>", "<<event-hub-name>>", credential) .buildAsyncProducerClient(); final AtomicReference<EventDataBatch> currentBatch = new AtomicReference<>( producer.createBatch().block()); telemetryEvents.subscribe(event -> { final EventDataBatch batch = currentBatch.get(); if (!batch.tryAdd(event)) { producer.createBatch().map(newBatch -> { currentBatch.set(newBatch); <START> newBatch.tryAdd(event); <END> return producer.send(batch); }).block(); } }, error -> System.err.println("Error received:" + error), () -> { final EventDataBatch batch = currentBatch.getAndSet(null); if (batch != null) { producer.send(batch).block(); } }); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } finally { producer.close(); } }</code><technical_language>if this tryAdd returns false i.e. if event is big for batch? logging error report user. silently drop event</technical_language>

Target:
public static void main(String[] args) { Flux<EventData> telemetryEvents = Flux.just( new EventData("Roast beef".getBytes(UTF_8)), new EventData("Cheese".getBytes(UTF_8)), new EventData("Tofu".getBytes(UTF_8)), new EventData("Turkey".getBytes(UTF_8))); System.setProperty("AZURE_CLIENT_ID", "<<insert-service-principal-client-id>>"); System.setProperty("AZURE_CLIENT_ID", "<<insert-service-principal-client-application-secret>>"); System.setProperty("AZURE_TENANT_ID", "<<insert-service-principal-tenant-id>>"); TokenCredential credential = new DefaultAzureCredentialBuilder() .build(); EventHubProducerAsyncClient producer = new EventHubClientBuilder() .credential( "<<fully-qualified-namespace>>", "<<event-hub-name>>", credential) .buildAsyncProducerClient(); final AtomicReference<EventDataBatch> currentBatch = new AtomicReference<>( producer.createBatch().block()); final Mono<Void> sendOperation = telemetryEvents.flatMap(event -> { final EventDataBatch batch = currentBatch.get(); if (batch.tryAdd(event)) { return Mono.empty(); } return Mono.when( producer.send(batch), producer.createBatch().map(newBatch -> { currentBatch.set(newBatch); if (!newBatch.tryAdd(event)) { throw Exceptions.propagate(new IllegalArgumentException(String.format( "Event is too large for an empty batch. Max size: %s. Event: %s", newBatch.getMaxSizeInBytes(), event.getBodyAsString()))); } return newBatch; })); }).then() .doFinally(signal -> { final EventDataBatch batch = currentBatch.getAndSet(null); if (batch != null) { producer.send(batch).block(OPERATION_TIMEOUT); } }); try { sendOperation.block(OPERATION_TIMEOUT); } finally { producer.close(); } }