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

百般思念 提交于 2019-12-19 12:49:10

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/35807844/kafka-consumer-simpleconsumer-reconnect-due-to-socket-error-java-nio-channels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!