根据io.netty.channel.oio 的documentation,如果您没有很多客户,您可以使用它。在这种情况下,每个连接都将在单独的线程中处理,并在后台使用 Java 旧的阻塞 IO。看看OioByteStreamChannel::activate:
/**
* Activate this instance. After this call {@link #isActive()} will return {@code true}.
*/
protected final void activate(InputStream is, OutputStream os) {
if (this.is != null) {
throw new IllegalStateException("input was set already");
}
if (this.os != null) {
throw new IllegalStateException("output was set already");
}
if (is == null) {
throw new NullPointerException("is");
}
if (os == null) {
throw new NullPointerException("os");
}
this.is = is;
this.os = os;
}
如您所见,此处将使用 oio Streams。
根据您的评论。您可以在将处理程序添加到管道时指定 EventExecutorGroup,如下所示:
new ChannelInitializer<Channel> {
public void initChannel(Channel ch) {
ch.pipeline().addLast(new YourHandler());
}
}
我们来看看AbstractChannelHandlerContext:
@Override
public EventExecutor executor() {
if (executor == null) {
return channel().eventLoop();
} else {
return executor;
}
}
在这里我们看到,如果您不注册 EventExecutor,它将使用您在创建 ServerBootstrap 时指定的子事件组。
new ServerBootstrap()
.group(new OioEventLoopGroup(), new OioEventLoopGroup())
//acceptor group //child group
下面是调用从通道读取的方法AbstractChannelHandlerContext::invokeChannelRead:
static void invokeChannelRead(final AbstractChannelHandlerContext next, Object msg) {
final Object m = next.pipeline.touch(ObjectUtil.checkNotNull(msg, "msg"), next);
EventExecutor executor = next.executor();
if (executor.inEventLoop()) {
next.invokeChannelRead(m);
} else {
executor.execute(new Runnable() { //Invoked by the EventExecutor you specified
@Override
public void run() {
next.invokeChannelRead(m);
}
});
}
}