/** * Set the port numbers for Zookeeper and the Kafka brokers. * * @param zkPort the port number that Zookeeper should use; may be -1 if an available port should be discovered * @param firstKafkaPort the port number for the first Kafka broker (additional brokers will use subsequent port numbers); * may be -1 if available ports should be discovered * @return this instance to allow chaining methods; never null * @throws IllegalStateException if the cluster is running */ public KafkaCluster withPorts(int zkPort, int firstKafkaPort) { if (running) throw new IllegalStateException("Unable to add a broker when the cluster is already running"); this.zkServer.setPort(zkPort); this.startingKafkaPort = firstKafkaPort; if (this.startingKafkaPort >= 0) { this.nextKafkaPort.set(this.startingKafkaPort); kafkaServers.values().forEach(kafka -> kafka.setPort((int) this.nextKafkaPort.getAndIncrement())); } return this; }