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

标签 java oop annotations overriding netty

这个方法来自 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/

相关文章:

java - 如何从 Struts 2 标签访问 servlet 应用上下文?

jquery - 如何从jquery选择器获取 "real"对象

c# - 我想在 mschart 中查看剩余行注释

java - 我无法理解对象引用在 Java 中的工作方式?

c++ - 如何正确地将变量传入和传出非默认构造函数?

spring - 多事务管理器注解配置

php - Symfony2 注释继承

java - DialogResult 或如何制作自定义对话框

java - maven 程序集 - jar 错过了自己的类(class)

java - 使用多个类同时获取所有输入时出错