Ram Rikhi
Ram Rikhi

Reputation: 11

Why does my Kafka producer unable to publish message?

I'm unable to publish a message to kafka topic, unable to get any response from kafka producer, It's completely stuck the application

Kafka Producer service code

@Service(value = "bookServiceImpl")
public class BookServiceImpl implements IBookService{
    @Autowired
    private KafkaTemplate<String,Book> kafkaTemplate;

    @Override
    public String sendBooksDetails(List<Book> booksList) {
        String msg = "";
        int index = 0;
        try {
            if (!booksList.isEmpty()){
                for (Book book:booksList) {
                    kafkaTemplate.send(KafkaConstants.TOPIC,book);
                }
            }
            msg = "Books are added to kafka topic successfully";
        }catch (Exception e){
            e.printStackTrace();
            msg = "Unable to publish message";
        }
        return msg;
    }
}

Kafka Producer Configuration code

@Configuration
public class KafkaProducerConfig {

    @Bean
    public ProducerFactory<String, Book> producerFactory(){
        Map<String,Object> configProps = new HashMap<>();
        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KafkaConstants.HOST);
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);

        return new DefaultKafkaProducerFactory<>(configProps);
    }

    @Bean
    public KafkaTemplate<String,Book> kafkaTemplate(){
        return new KafkaTemplate<>(producerFactory());
    }
}

java.lang.IllegalArgumentException: Invalid character found in method name [0x000x000x00.0x000x120x000x030x000x000x000x000x000x0aproducer-10x000x12apache-kafka-java0x062.6.0...]. HTTP method names must be tokens

2021-05-30 13:29:13.209[0;39m [32m INFO[0;39m [35m2472[0;39m [2m---[0;39m [2m[nio-8084-exec-2][0;39m [36mo.apache.coyote.http11.Http11Processor [0;39m [2m:[0;39m Error parsing HTTP request header Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.

java.lang.IllegalArgumentException: Invalid character found in method name [0x000x000x00.0x000x120x000x030x000x000x000x000x000x0aproducer-10x000x12apache-kafka-java0x062.6.0...]. HTTP method names must be tokens at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:417) ~[tomcat-embed-core-9.0.45.jar:9.0.45] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:261) ~[tomcat-embed-core-9.0.45.jar:9.0.45] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.45.jar:9.0.45] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) ~[tomcat-embed-core-9.0.45.jar:9.0.45] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707) ~[tomcat-embed-core-9.0.45.jar:9.0.45] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.45.jar:9.0.45] at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na] at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.45.jar:9.0.45] at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]

[2m2021-05-30 13:29:15.276[0;39m [33m WARN[0;39m [35m2472[0;39m [2m---[0;39m [2m[ad | producer-1][0;39m [36morg.apache.kafka.clients.NetworkClient [0;39m [2m:[0;39m [Producer clientId=producer-1] Bootstrap broker localhost:8084 (id: -1 rack: null) disconnected [2m2021-05-30 13:29:15.531[0;39m [33m WARN[0;39m [35m2472[0;39m [2m---[0;39m [2m[ad | producer-1][0;39m [36morg.apache.kafka.clients.NetworkClient [0;39m [2m:[0;39m [Producer clientId=producer-1] Bootstrap broker localhost:8084 (id: -1 rack: null) disconnected

Upvotes: 1

Views: 3412

Answers (2)

Chandan
Chandan

Reputation: 740

I think your issue has more to do with how kafka was setup than the code that you shared. I also believe when you deploy kafka and spring boot application on same server then you should not face this challenge anymore.

But, To fix this can you try these steps:

  1. Check this file <kafka-home>/config/server.properties
  2. Uncomment the line - listeners=PLAINTEXT://:9092
  3. Uncomment the line - advertised.listeners=PLAINTEXT://your.host.name:9092 and set it with proper IP address like - advertised.listeners=PLAINTEXT://192.168.1.113:9092
  4. Restart kafka now and test it out

I am hopeful it will work now

Upvotes: 1

Ran Lupovich
Ran Lupovich

Reputation: 1821

This exception can occur when you try to execute HTTPS request from client on endpoint which isn't HTTPS enabled. Client will encrypt request data when server is expecting raw data.

Upvotes: 0

Related Questions