简体   繁体   English

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

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

I am running a simple consumer for kafka such as this: 我正在为kafka运行一个简单的消费者,例如:

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: 这运行好几个小时,但后来我在kafka.consumer.SimpleConsumer上得到一个例外:由于套接字错误重新连接:

java.nio.channels.ClosedChannelException java.nio.channels.ClosedChannelException

and consumer stops ... has anyone faced this problem before ? 和消费者停止......以前有人遇到过这个问题吗?

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. 首先,您对EOS的测试是错误的。 Remove the limit() == 0 test. 删除limit()== 0测试。 That doesn't indicate EOS, it just indicates a zero length read, which can happen in non-blocking mode at any time. 这并不表示EOS,它只是指示零长度读取,这可以在任何时间以非阻塞模式发生。 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, eg for reading. 您可能还没有在使用之前检查就绪键是否在选择循环中有效,例如用于读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.nio.channels.ClosedChannelException - java.nio.channels.ClosedChannelException Apache Kafka:无法更新Metadata / java.nio.channels.ClosedChannelException - Apache Kafka: Failed to Update Metadata/java.nio.channels.ClosedChannelException 使用SSL和Postman的java.nio.channels.ClosedChannelException - java.nio.channels.ClosedChannelException with SSL and Postman java.nio.channels.ClosedChannelException HTTP/2 与码头 - java.nio.channels.ClosedChannelException HTTP/2 with jetty 异常-java.nio.channels.ClosedChannelException - exception - java.nio.channels.ClosedChannelException 如何从客户端关闭套接字通道,以便服务器引发java.nio.channels.ClosedChannelException - How to close a socket channel from client side so that the server throws java.nio.channels.ClosedChannelException jenkins主连接失败,出现java.nio.channels.ClosedChannelException - jenkins master connection fails with java.nio.channels.ClosedChannelException Stardog Connection.commit()引发java.nio.channels.ClosedChannelException - Stardog Connection.commit() raising java.nio.channels.ClosedChannelException java.nio.channels.ClosedChannelException-客户端关闭SSL - java.nio.channels.ClosedChannelException -Client shuts down SSL GATLING Rest API 测试 - java.nio.channels.ClosedChannelException: Z3099A62586648C1 - GATLING Rest API testing - java.nio.channels.ClosedChannelException: null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM