我对netty框架中ChannelOutboundHandle的"read"方法感到困惑



我不知道ChannelOutboundHandle的"read"方法。 为什么这种方法出现在 ChannelOutboundHandle 中,它的用途是什么? 因为出站处理程序用于处理输出,它们通常是 用于将数据写入线路。 而且我在使用"读取"方法时遇到了问题。我用netty写了一个EchoServer。这是我的代码:

public class EchoServer {
public static void main(String[] args) {
// this handler is sharable
final EchoServerInboundHandler echoServerInboundHandler = new EchoServerInboundHandler();
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
ServerBootstrap serverBootstrap = new ServerBootstrap();
// setup group
serverBootstrap.group(eventLoopGroup)
// setup channelFactory
.channel(NioServerSocketChannel.class)
// listening port
.localAddress(new InetSocketAddress(8080))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(echoServerInboundHandler);
pipeline.addLast(new EchoServerOutboundHandler());
}
});
try {
ChannelFuture f = serverBootstrap.bind().sync();
f.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
System.out.println(future.channel().localAddress()+" started!");
}
}
});
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
// release all threads
eventLoopGroup.shutdownGracefully().syncUninterruptibly();
}
}
}

public class EchoServerOutboundHandler extends ChannelOutboundHandlerAdapter {
@Override
public void read(ChannelHandlerContext ctx) throws Exception {
System.out.println("read");
//super.read(ctx);
}
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
System.out.println("msg:"+msg);
super.write(ctx, msg, promise);
}
@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
super.flush(ctx);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}

@ChannelHandler.Sharable
public class EchoServerInboundHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ctx.writeAndFlush(msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
System.out.println("client:"+ctx.channel().remoteAddress()+" connected!");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
System.out.println("client:"+ctx.channel().remoteAddress()+" disconnected!");
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
System.out.println("EchoServerInboundHandler registered");
super.channelRegistered(ctx);
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
System.out.println("EchoServerInboundHandler unregistered");
super.channelUnregistered(ctx);
}
}

如您所见,我在EchoServerOutboundHandlerread方法中注释了super.read(ctx);,这会导致无法将数据写入客户端的问题。 而且我还发现当客户端建立连接时会调用此方法。

至少,您需要提供与父对象在 super(( 方法中执行的代码相同的代码,这是ctx.read()

这就是JavaDoc所说的...

呼叫ChannelHandlerContext.read()...

否则,您所做的只是自己打印一些东西,而不是使用 Netty 来处理任何事情。

最新更新