1

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?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Usr
  • 2,628
  • 10
  • 51
  • 91
  • 1
    May I ask why you need to start them from your code? – pgras Apr 02 '19 at 13:38
  • @pgras just for convenience and speed – Usr Apr 02 '19 at 13:42
  • Possible duplicate of [Simple embedded Kafka test example with spring boot](https://stackoverflow.com/questions/48753051/simple-embedded-kafka-test-example-with-spring-boot) – howie Apr 03 '19 at 23:00
  • If you want to embed a kafka in spring boot , try this https://stackoverflow.com/questions/48753051/simple-embedded-kafka-test-example-with-spring-boot – howie Apr 03 '19 at 23:01

1 Answers1

0

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

Milan Desai
  • 1,228
  • 8
  • 23
  • And for Kafka server? – Usr Apr 02 '19 at 16:01
  • you will need kafa dependecy in your maven project and have `Producer` and `Consumer` to invoke kafka from java api. one of the best example is given in [this](https://stackoverflow.com/questions/44602797/starting-zookeeper-and-kafka-servers-from-java-application) answer – Milan Desai Apr 03 '19 at 09:48