/** * Asynchronously produce messages with monotonically increasing String keys and values obtained from the supplied * function, and write them to the cluster. * * @param topic the name of the topic to which the messages should be written; may not be null * @param messageCount the number of messages to produce; must be positive * @param completionCallback the function to be called when the producer is completed; may be null * @param valueSupplier the value supplier; may not be null */ public void produceDocuments(String topic, int messageCount, Runnable completionCallback, Supplier<Document> valueSupplier) { AtomicLong counter = new AtomicLong(0); produceDocuments(messageCount, completionCallback, () -> { long i = counter.incrementAndGet(); String keyAndValue = Long.toString(i); return new ProducerRecord<String, Document>(topic, keyAndValue, valueSupplier.get()); }); }