Beispiel #1
0
  public EmbeddedKafka(Map<String, String> customProps) throws Exception {
    super();
    Map<String, String> defaultProps = Maps.newHashMap();
    defaultProps.put("broker.id", "0");
    defaultProps.put("host.name", "127.0.0.1");
    defaultProps.put("port", "9092");
    defaultProps.put("advertised.host.name", "127.0.0.1");
    defaultProps.put("advertised.port", "9092");
    defaultProps.put("log.dir", createTempDir().getAbsolutePath());
    defaultProps.put("zookeeper.connect", package$.MODULE$.ZookeeperConnectionString());
    defaultProps.put("replica.high.watermark.checkpoint.interval.ms", "5000");
    defaultProps.put("log.flush.interval.messages", "1");
    defaultProps.put("replica.socket.timeout.ms", "500");
    defaultProps.put("controlled.shutdown.enable", "false");
    defaultProps.put("auto.leader.rebalance.enable", "false");

    Properties props = new Properties();
    props.putAll(defaultProps);
    props.putAll(customProps);

    final KafkaConfig kafkaConfig = new KafkaConfig(props);

    zookeeper = new EmbeddedZookeeper((String) props.get("zookeeper.connect"));
    awaitCond(aVoid -> zookeeper.isRunning(), 3000, 100);

    server = new KafkaServer(kafkaConfig, SystemTime$.MODULE$);
    Thread.sleep(2000);

    log.info("Starting the Kafka server at {}", kafkaConfig.zkConnect());
    server.startup();
    Thread.sleep(2000);
  }
Beispiel #2
0
 public void shutdown() throws Exception {
   log.info("Shutting down Kafka server");
   // https://issues.apache.org/jira/browse/KAFKA-1887
   server.kafkaController().shutdown();
   server.shutdown();
   server.awaitShutdown();
   final Seq<String> logDirs = server.config().logDirs();
   for (String f : JavaConversions.asJavaCollection(logDirs)) {
     try {
       deleteRecursively(new File(f));
     } catch (IOException e) {
       log.warn("Cannot delete file: " + f, e.getMessage());
     }
   }
   ;
   zookeeper.shutdown();
   awaitCond(aVoid -> !zookeeper.isRunning(), 2000, 100);
   log.info("ZooKeeper server shut down.");
   Thread.sleep(2000);
 }