编程语言
首页 > 编程语言> > kafka.consumer.SimpleConsumer:由于套接字错误而重新连接:java.nio.channels.ClosedChannelException

kafka.consumer.SimpleConsumer:由于套接字错误而重新连接:java.nio.channels.ClosedChannelException

作者:互联网

我正在为kafka运行一个简单的消费者,例如:

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

这可以正常运行几个小时,但出现异常
稍后的
kafka.consumer.SimpleConsumer:由于套接字错误而重新连接:

java.nio.channels.ClosedChannelException

消费者停止了……以前有人遇到过这个问题吗?

解决方法:

一个稍有不同的问题,但也许具有相同的根本原因和解决方案是answered here,相关部分:

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.

标签:kafka-consumer-api,java,apache-kafka,sockets,nio
来源: https://codeday.me/bug/20191010/1886675.html