gpt4 book ai didi

java - 扩展 ChannelHandlerAdapter,但不重写或实现 ChannelHandlerAdapter 中的方法

转载 作者:行者123 更新时间:2023-12-01 12:44:10 26 4
gpt4 key购买 nike

这个方法来自 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时,我没有看到这些方法...

最佳答案

您混淆了版本。

在 Netty 4 中,您有 EchoClientHandler :

public class  EchoClientHandler extends ChannelInboundHandlerAdapter 

在 Netty 4 中,ChannelInboundHandlerAdapter有一个 channelActive 方法。

您的链接适用于 EchoClientHandler在 Netty 5 中,ChannelHandlerAdapter已更新并具有更多方法,包括 channelActive

关于java - 扩展 ChannelHandlerAdapter,但不重写或实现 ChannelHandlerAdapter 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24844042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com