Until now I've started zookeeper and kafka server from command line before running my Spring Boot application, but now I need to start them directly from code.
First thing is, I've tried using ProcessBuilder inside main method:
Process process = new ProcessBuilder("C:\\kafka_2.12-2.2.0\\bin\\windows\\zookeeper-server-start.bat",
"C:\\kafka_2.12-2.2.0\\config\\zookeeper.properties").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
But this seems not working, since nothing is printed on the console and after a while the application throws a TimeOutException.
Second, I would like to have the kafka server to run after Zookeeper has started; how can one achieve this?
You will have to use
ZookeeperExecutor
to start Zookeeper API from "your" java application by giving 4 initial arguments and a thread runner in place. One example is given on ZooKeeper API documentation https://zookeeper.apache.org/doc/r3.4.13/javaExample.html and https://www.programcreek.com/java-api-examples/?api=org.apache.zookeeper.server.ZooKeeperServerMain