我正在使用Apache HTTP异步客户端发出http请求(通过使用来自Kafka总线的数据)并处理回调中的响应。
我在日志中发现了一些有趣的回调行为,并想知道hwy就是这样。
以下是代码的一部分(使用来自kafka和make http请求的数据):
while (true) {
ConsumerRecords<String, MyMessage> records = kafkaConsumer.poll(1000);
if (records.isEmpty()) {
logger.info("Polling Empty ....");
continue;
}
int numberOfRecords = records.count();
final CountDownLatch batchLatch = new CountDownLatch(numberOfRecords);
for (ConsumerRecord<String, MyMessage> record: records) {
final HttpPost request = new HttpPost(endpoint);
// set headers
// set entity
request.setEntity(messageToEntity(record.value()));
httpClient.execute(request, someCallback);
}
batchLatch.await();
kafkaConsumer.commitAsync();
}
以下是回调的示例代码:
public class SomeCallback {
// ...
@Override
public void completed(final HttpResponse response) {
// do something
logger.info("{} - blablabla", status, ...);
latch.countDown();
}
@Override
public void failed(final Exception ex) {
// do something
logger.error("{} - blablabla", FAILED, ..., ex);
latch.countDown();
}
@Override
public void cancelled() {
// do something
logger.error("{} - blablabla!", CANCELLED, ...);
latch.countDown();
}
}
所以情况是,在某个时间点,我启动了程序,它开始使用来自kafka的数据。每次轮询的最大记录数为500。
由于Kafka上已有许多未消耗的数据,该程序面临着高吞吐量。
现在,日志看起来像这样:
[INFO ] 2018-04-29 04:11:29.234 [main] MyClass - Polling got records: 500
[INFO ] 2018-04-29 04:11:30.362 [I/O dispatcher 1] SUCCESS blablabla
[INFO ] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 2] SUCCESS blablabla
[INFO ] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 1] SUCCESS blablabla
[INFO ] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 1] SUCCESS blablabla
[INFO ] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 2] SUCCESS blablabla
...
...
[INFO ] 2018-04-29 06:28:35.003 [main] MyClass - Polling got records: 500
[INFO ] 2018-04-29 06:28:35.363 [I/O dispatcher 386] SUCCESS blablabla
[INFO ] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 385] SUCCESS blablabla
[INFO ] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 386] SUCCESS blablabla
[INFO ] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 385] SUCCESS blablabla
[INFO ] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 386] SUCCESS blablabla
...
[INFO ] 2018-04-29 06:31:35.003 [main] MyClass - Polling got records: 500
[INFO ] 2018-04-29 06:32:12.418 [I/O dispatcher 405] SUCCESS blablabla
[INFO ] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 406] SUCCESS blablabla
[INFO ] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 405] SUCCESS blablabla
[INFO ] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 406] SUCCESS blablabla
[INFO ] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 405] SUCCESS blablabla
...
[ERROR] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 405] FAILED blablabla! org.apache.http.ConnectionClosedException: Connection closed unexpectedly
[ERROR] 2018-04-29 xx:xx:xx.xxx [I/O dispatcher 406] FAILED blablabla! org.apache.http.ConnectionClosedException: Connection closed unexpectedly
// there the program stopped
这是我无法理解的:
感谢Oleg的评论,我发现不断增加的I / O调度程序数量是因为每次我从Kafka获取数据时,都会创建一个新的http客户端。然后,会有许多占用IO和资源的闲置客户端。这也是该计划停止的原因。
我仍然有一些问题:
答案 0 :(得分:2)
它代表一个帖子名称。
可以使用IOReactorConfig
少数I / O调度程序管理更多的连接。
对于绝大多数应用程序而言,每个CPU核心的一个I / O调度线程很可能是一个合理的默认值,而且不应该更改它。