bin/kafka-topics.sh --create --topic test-producer --bootstrap-server <broker-id>:9092
bin/kafka-topics.sh --list --bootstrap-server <broker-id>:9092
bin/kafka-topics.sh --describe --topic test-producer --bootstrap-server <broker-id>:9092
bin/kafka-console-producer.sh --topic test-producer --bootstrap-server <broker-id>:9092
bin/kafka-console-consumer.sh --topic test-producer --from-beginning --bootstrap-server <broker-id>:9092
bin/kafka-topics.sh --delete --topic test-producer --bootstrap-server <broker-id>:9092
bin/kafka-console-consumer.sh --topic nse-stock-may --bootstrap-server <broker-id>:9092
-
Implement the
Runnableinterface -
Load the Data file inside the
runmethod
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(fileLocation).getFile());
int counter = 0;- Read the line using
Scannerclass and send the message to Broker
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
producer.send(new ProducerRecord<>(topicName, null, line));
counter++;
}
logger.info("Successfully sent " + counter + " messages from " + fileLocation);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}- Create new Threads to send messages concurrently
Thread[] dispatchers = new Thread[AppConfig.dataFiles.length];
logger.info("Starting Dispatcher threads...");
for (int i = 0; i < AppConfig.dataFiles.length; i++) {
dispatchers[i] = new Thread(new Executor(producer, AppConfig.topicName, AppConfig.dataFiles[i]));
dispatchers[i].start();
}- Stop the threads gracefully
try {
for (Thread t : dispatchers)
t.join();
} catch (InterruptedException e) {
logger.error("Main Thread Interrupted");
}- Shutdown the Producer instance gracefully
finally {
producer.close();
logger.info("Finished Dispatcher Demo");
}- Verify the message produced by running the console consumer
bin/kafka-console-consumer.sh --topic nse-stock-may --bootstrap-server <broker-id>:9092
- Delete the
nse-stock-maytopic
bin/kafka-topics.sh --delete --topic nse-stock-may --bootstrap-server <broker-id>:9092