@Override public void stop() throws Exception { kafkaCluster.shutdown(); } }
/** * 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 consumeStrings(String topicName, int count, long timeout, TimeUnit unit, Runnable completion) { consumeStrings(topicName, count, timeout, unit, completion, (key, value) -> true); }
/** * Get the Zookeeper port. * * @return the Zookeeper port */ public int zkPort() { return zkServer.getPort(); }
/** * Perform the supplied function on each directory used by this cluster. * * @param consumer the consumer function; may not be null */ void onEachDirectory(java.util.function.Consumer<File> consumer) { consumer.accept(zkServer.getSnapshotDirectory()); consumer.accept(zkServer.getLogDirectory()); kafkaServers.values().forEach(server -> consumer.accept(server.getStateDirectory())); }
/** * Perform the supplied function on each directory used by this server. * * @param consumer the consumer function; may not be null */ void onEachDirectory(Consumer<File> consumer) { consumer.accept(getSnapshotDirectory()); consumer.accept(getLogDirectory()); }
/** * Create the specified topics. * * @param topics the names of the topics to create * @throws IllegalStateException if the cluster is not running */ public void createTopics(Set<String> topics) { createTopics(topics.toArray(new String[topics.size()])); }
/** * Create the specified topics. * * @param numPartitions the number of partitions for each topic * @param replicationFactor the replication factor for each topic * @param topics the names of the topics to create */ public void createTopics(int numPartitions, int replicationFactor, String... topics) { for (String topic : topics) { if ( topic != null ) createTopic(topic, numPartitions, replicationFactor); } }
@Override public String toString() { return "ZookeeperServer{" + getConnection() + "}"; } }
/** * 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 consumeDocuments(String topicName, int count, long timeout, TimeUnit unit, Runnable completion) { consumeDocuments(topicName, count, timeout, unit, completion, (key, value) -> true); }
/** * Create the specified topics. * * @param topics the names of the topics to create */ public void createTopics(String... topics) { createTopics(1,1,topics); }
/** * 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); }
/** * Shutdown the embedded Zookeeper server and delete all data. */ public void shutdown() { shutdown(true); }
@Override public long hiResClockMs() { return nanoseconds(); }
@Override public String toString() { return "KafkaServer{" + getConnection() + "}"; }
/** * Obtain the interface for using this cluster. * * @return the usage interface; never null * @throws IllegalStateException if the cluster is not running */ public Usage useTo() { if (!running) throw new IllegalStateException("Unable to use the cluster it is not running"); return new Usage(); }
/** * Perform the supplied function on each directory used by this server. * * @param consumer the consumer function; may not be null */ void onEachDirectory(Consumer<File> consumer) { consumer.accept(getStateDirectory()); }
private void shutdownReliably(KafkaServer server) { try { server.shutdown(deleteDataUponShutdown); } catch (Throwable t) { LOGGER.error("Error while shutting down {}", server, t); } }
/** * Write to the topic with the given name a record with the specified key and value. The message is flushed immediately. * * @param topic the name of the topic; may not be null * @param key the key; may not be null * @param value the value; may not be null * @return this producer instance to allow chaining methods together */ default InteractiveProducer<K, V> write(String topic, K key, V value) { return write(new ProducerRecord<>(topic, key, value)); }
/** * Block until a record can be read from this consumer's topic, and return the value in that record. * * @return the value; never null * @throws InterruptedException if the thread is interrupted while blocking */ default V nextValue() throws InterruptedException { return nextRecord().value(); }
/** * Create the specified topics. * * @param numPartitions the number of partitions for each topic * @param replicationFactor the replication factor for each topic * @param topics the names of the topics to create */ public void createTopics(int numPartitions, int replicationFactor, Set<String> topics) { createTopics(numPartitions, replicationFactor, topics.toArray(new String[topics.size()])); }