我有一个客户端需要向服务器发送大量小消息(每条消息大约8k),现在单个线程可以一个接一个地推送大约120万条消息,而不会在接收这些消息之间产生延迟
如何将我的netty客户端配置为" buffer"写入所以它以给定的缓冲区大小自动刷新并且不会创建OOM错误吗?
作为一个简单的例子,让我们说我想逐行发送文件的内容来模拟这些消息的接收。
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder());
}
});
// Start the connection attempt.
Channel ch = b.connect("localhost", 6000).sync().channel();
ChannelFuture lastWriteFuture = null;
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(new File(
"data.txt"))));
int i = 0;
while (true) {
// This simulates the incoming data
String line = in.readLine();
if (line == null) {
break;
}
lastWriteFuture = ch.write(line + '\n');
// TODO : Investigate why we need this, otherwise it throws OOM errors...
if (i++ % 10000 == 0) {
ch.flush();
lastWriteFuture.awaitUninterruptibly();
}
}
// This part is here only because it's a simulation, there is no notion of "end of data" in our normal process, it's continuous send of data.
ch.flush();
// Wait until all messages are flushed before closing the channel.
if (lastWriteFuture != null) {
lastWriteFuture.sync();
}
} finally {
// The connection is closed automatically on shutdown.
group.shutdownGracefully();
}
我想弄清楚的是,如何在不需要刷新每条X消息的情况下进行连续写入?
我已经看过Norman的Maurer谈论VoidChannelPromise,但是不能在这里使用,因为我最终也需要SslHandler。
我是否需要以及如何实施某些节流?
另外,Netty 3有BufferedWriteHandler,它似乎不再存在于netty 4.0中,我忽略了一些我可以设置为&#34;启用&#34;缓冲?
实现这一目标的任何例子?
谢谢,