/** * Set the configuration properties for each of the brokers. This method does nothing if the supplied properties are null or * empty. * * @param properties the Kafka configuration properties * @return this instance to allow chaining methods; never null * @throws IllegalStateException if the cluster is running */ public KafkaCluster withKafkaConfiguration(Properties properties) { if (running) throw new IllegalStateException("Unable to add a broker when the cluster is already running"); if (properties != null && !properties.isEmpty()) { kafkaConfig = new Properties(); kafkaConfig.putAll(properties); kafkaServers.values().forEach(kafka -> kafka.setProperties(kafkaConfig)); } return this; }
/** * Add a number of new Kafka broker to the cluster. The broker IDs will be generated. * * @param count the number of new brokers to add * @return this instance to allow chaining methods; never null * @throws IllegalStateException if the cluster is running */ public KafkaCluster addBrokers(int count) { if (running) throw new IllegalStateException("Unable to add a broker when the cluster is already running"); AtomicLong added = new AtomicLong(); while (added.intValue() < count) { kafkaServers.computeIfAbsent(Integer.valueOf(added.intValue() + 1), id -> { added.incrementAndGet(); KafkaServer server = new KafkaServer(zkServer::getConnection, id); if (dataDir != null) server.setStateDirectory(dataDir); if (kafkaConfig != null) server.setProperties(kafkaConfig); if (startingKafkaPort >= 0) server.setPort((int) this.nextKafkaPort.getAndIncrement()); return server; }); } return this; }