/** * Asynchronously consume all messages on the given topic from the cluster. * * @param topicName the name of the topic; may not be null * @param count the expected number of messages to read before terminating; may not be null * @param timeout the maximum time that this consumer should run before terminating; must be positive * @param unit the unit of time for the timeout; may not be null * @param completion the function to call when all messages have been consumed; may be null * @param consumer the function to consume the messages; may not be null */ public void consumeIntegers(String topicName, int count, long timeout, TimeUnit unit, Runnable completion, BiPredicate<String, Integer> consumer) { AtomicLong readCounter = new AtomicLong(); consumeIntegers(continueIfNotExpired(() -> readCounter.get() < count, timeout, unit), completion, Collections.singleton(topicName), record -> { if (consumer.test(record.key(), record.value())) readCounter.incrementAndGet(); }); }
@Test @SkipLongRunning public void shouldStartClusterAndAllowProducersAndConsumersToUseIt() throws Exception { Testing.Debug.enable(); final String topicName = "topicA"; final CountDownLatch completion = new CountDownLatch(2); final int numMessages = 100; final AtomicLong messagesRead = new AtomicLong(0); // Start a cluster and create a topic ... cluster.addBrokers(1).startup(); cluster.createTopics(topicName); // Consume messages asynchronously ... Stopwatch sw = Stopwatch.reusable().start(); cluster.useTo().consumeIntegers(topicName, numMessages, 10, TimeUnit.SECONDS, completion::countDown, (key, value) -> { messagesRead.incrementAndGet(); return true; }); // Produce some messages asynchronously ... cluster.useTo().produceIntegers(topicName, numMessages, 1, completion::countDown); // Wait for both to complete ... if (completion.await(10, TimeUnit.SECONDS)) { sw.stop(); Testing.debug("Both consumer and producer completed normally in " + sw.durations()); } else { Testing.debug("Consumer and/or producer did not completed normally"); } assertThat(messagesRead.get()).isEqualTo(numMessages); }
cluster.useTo().consumeIntegers(topicName, numMessages, 10, TimeUnit.SECONDS, completion::countDown, (key, value) -> { messagesRead.incrementAndGet(); return true;
cluster.useTo().consumeIntegers(topicName, numMessages, 10, TimeUnit.SECONDS, completion::countDown, (key, value) -> { messagesRead.incrementAndGet(); return true;
/** * Asynchronously consume all messages on the given topic from the cluster. * * @param topicName the name of the topic; may not be null * @param count the expected number of messages to read before terminating; may not be null * @param timeout the maximum time that this consumer should run before terminating; must be positive * @param unit the unit of time for the timeout; may not be null * @param completion the function to call when all messages have been consumed; may be null */ public void consumeIntegers(String topicName, int count, long timeout, TimeUnit unit, Runnable completion) { consumeIntegers(topicName, count, timeout, unit, completion, (key, value) -> true); }