Coder Social home page Coder Social logo

java-kafka-standalone-server's Introduction

java-kafka-standalone-server

A kafka standalone server for integration testing

Supported version: kafka-0.10.0.2 (scala 2.11)

Feature

  • Standalone kafka server that can run without additional installation on Windows, Linux and MacOS.
  • Allow shut down of the kafka server remotely via web api call

Usage

The jars have been prebuilt and available in the root directory of the project, but the make.ps1 and make.sh can be used to rebuild the jars if you need to do some modification on your own.

Run Kafka standalone server

copy the kafka-standalone.jar to your directory and run the following command:

java -jar kafka-standalone.jar

This will start the kafka server at port 9092 (zk client port at 2181) and start another web server at port 9095.

To check whether the kafka server is alive, call the following url:

http://localhost:9095/ping

To kill the kafka server remotely, just call the following url:

http://localhost:9095/kill

Test Kafka standalone server

The java-kafka-client-demo contains demo code on how to produce and consume topics in kafka.

To test the kafka standalone server, with it still running, run the following two jars:

java -jar kafka-producer-demo.jar
java -jar kafka-consumer-demo.jar

The producer codes in kafka-producer-demo.jar are shown below:

import com.github.chen0040.kafka.clients.*;
public static void main(String[] args) {
    String topic = "kafkatest";
    KafkaProducerContract producer = KafkaProducer.getInstance();
    producer.setBrokers("localhost:9092");
    
    while(true) {
        String testString = "TEST " + new Date(System.currentTimeMillis());
        System.out.println("produced: " + testString);
        producer.write(topic, testString);
        try {
            sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

The consumer codes in java-kafka-consumer-demo.jar are shown below:

import com.github.chen0040.kafka.clients.*;
public static void main(String[] args) {
    String topic = "kafkatest";
    List<String> topics = new ArrayList<>();
    topics.add(topic);

    String brokers = "localhost:9092";
    KafkaConsumer consumer = new KafkaConsumer(brokers, 1, "es-group", topics);


    ConsumerListener listener = (data) -> System.out.println("consumer received: " + data);

    consumer.addListener(listener);
    Thread thread = new Thread(consumer);
    thread.start();

    Runtime.getRuntime().addShutdownHook(new Thread(consumer::shutdown));

    while(true) {
        try {
            sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

Note

In case you want to modify the behavior the kafka server and want to rebuild, you can run the make.ps1 on Windows or make.sh on Linux or Mac.

java-kafka-standalone-server's People

Contributors

chen0040 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.