user5774816
user5774816

Reputation:

kafka.consumer.SimpleConsumer: Reconnect due to socket error: java.nio.channels.ClosedChannelException

I am running a simple consumer for kafka such as this:

int timeout = 80000;
int bufferSize = 64*1024;
consumer = new SimpleConsumer(host, port,timeout, bufferSize, clientName);

This runs fine for a couple of hours but I get an exception later on kafka.consumer.SimpleConsumer: Reconnect due to socket error:

java.nio.channels.ClosedChannelException

and consumer stops ... has anyone faced this problem before ?

Upvotes: 12

Views: 7194

Answers (1)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

A slightly different question, but perhaps with the same root cause and solution was answered here, the relevant part:

You have closed the channel and are still trying to use it.

There are several issues with your code.

First, your test for EOS is faulty. Remove the limit() == 0 test. That doesn't indicate EOS, it just indicates a zero length read, which can happen in non-blocking mode at any time. It doesn't mean the peer has closed his end of the connection, and it doesn't mean you should close your end.

Second, closing a channel closes the socket as well. You should close the channel only, not the socket.

Third, closing a channel cancels the key. You don't need to follow every close with a cancel.

You may also have failed to check whether a ready key is valid in the select loop before using it, e.g. for reading.

Upvotes: 1

Related Questions