kafka java producer stuck in producing message
Asked Answered
C

4

6

I am implementing apache kafka producer using java api. Apache Kafka is installed on localhost. Zookeeper is also running but still producer.send() function stuck in sending message and message is not published.

I have already created "fast-messages" topic.

Below is the code.

package com.hsahu.kafka.producer;

import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

public class KafkaProducerExample {
public static void main(String[] args) {

    Properties props = new Properties();

    props.put("bootstrap.servers", "localhost:9092");
    props.put("acks", "all");
    props.put("retries", 0);
    props.put("batch.size", 16384);
    props.put("linger.ms", 0);
    props.put("buffer.memory", 33554432);
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

    KafkaProducer<String, String> producer = new KafkaProducer<>(props);

    try {
        producer.send(new ProducerRecord<String, String>("fast-messages", "This is a dummy message"));
    } catch(Exception ex) {
        System.out.println(ex);
    }

    System.out.println("message publisher");

    producer.close();
}

}

What should i do ? is my code wrong or any properties not set correctly or missing ?

Classic answered 28/8, 2016 at 20:23 Comment(0)
C
5

There was't any problem in the code. only api version and kafka server version were mismatched. So i only corrected the api version and now producer is working.

Classic answered 31/8, 2016 at 5:24 Comment(1)
I concur. I was using kafka client 0.10; however, my broker version was .9. I upgraded to .10 and everything worked great.Visayan
L
1

test below

if version above 0.9 you need config "advertised.host.name" in broker

Loseff answered 29/8, 2016 at 2:37 Comment(0)
A
0

Can you try producer.flush() instead of producer.close(). Flush() blocks untill the messages are sent to the kafka broker ??? I don't see anything strange other than that..

Antilebanon answered 29/8, 2016 at 6:2 Comment(0)
B
0

In my case, it was because the topic was not created. I messed up with bootstrap-servers configuration for producer (was using different env, where there was no such topic). After fixing that it worked.

Bobbyebobbysocks answered 3/12, 2018 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.