这种方法来自EchoClientHandler会覆盖什么?
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(firstMessage);
}
编译错误:
-do-compile:
[mkdir] Created dir: /home/thufir/NetBeansProjects/NettyEcho/build/empty
[mkdir] Created dir: /home/thufir/NetBeansProjects/NettyEcho/build/generated-sources/ap-source-output
[javac] Compiling 4 source files to /home/thufir/NetBeansProjects/NettyEcho/build/classes
[javac] /home/thufir/NetBeansProjects/NettyEcho/src/io/netty/example/echo/EchoClientHandler.java:27: error: method does not override or implement a method from a supertype
[javac] @Override
[javac] ^
来自github的代码:
package io.netty.example.echo;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
/**
* Handler implementation for the echo client. It initiates the ping-pong
* traffic between the echo client and server by sending the first message to
* the server.
*/
public class EchoClientHandler extends ChannelHandlerAdapter {
private final ByteBuf firstMessage;
/**
* Creates a client-side handler.
*/
public EchoClientHandler() {
firstMessage = Unpooled.buffer(EchoClient.SIZE);
for (int i = 0; i < firstMessage.capacity(); i ++) {
firstMessage.writeByte((byte) i);
}
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(firstMessage);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ctx.write(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();
}
}
当我查看ChannelHandlerAdapter时,我看不到这些方法......
答案 0 :(得分:1)
你正在混淆版本。
在Netty 4中,你有EchoClientHandler
:
public class EchoClientHandler extends ChannelInboundHandlerAdapter
在Netty 4中,ChannelInboundHandlerAdapter
有channelActive
方法。
您的链接适用于Netty 5中的EchoClientHandler
,其中ChannelHandlerAdapter
已更新,并且有更多方法,包括channelActive
。