Java Netty与JDK版本冲突
我正在使用教程学习netty工具,但我遇到了一个问题,我认为这可能与netty jar包和JDK的版本有关。 现在,我的jdk是1.8,Netty版本是Netty-all-4.0.0 Final.jar,我从Netty官方网站下载的 下面是错误代码,,我在错误行之后使用了注释。因为我不知道如何在代码段中突出显示,所以您可能需要仔细注意注释,幸运的是只有两行Java Netty与JDK版本冲突,java,netty,Java,Netty,我正在使用教程学习netty工具,但我遇到了一个问题,我认为这可能与netty jar包和JDK的版本有关。 现在,我的jdk是1.8,Netty版本是Netty-all-4.0.0 Final.jar,我从Netty官方网站下载的 下面是错误代码,,我在错误行之后使用了注释。因为我不知道如何在代码段中突出显示,所以您可能需要仔细注意注释,幸运的是只有两行 EventLoopGroup pGroup = new NioEventLoopGroup(); EventLoop
EventLoopGroup pGroup = new NioEventLoopGroup();
EventLoopGroup cGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(pGroup, cGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.option(ChannelOption.SO_SNDBUF, 32*1024)
.option(ChannelOption.SO_RCVBUF, 32*1024)
.option(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() { // there is an error, which indicates that the generic <SocketChannel> is not a valid substitute according the eclipse automatic prompt
@Override
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(new ServerHandler()); // there are two errors about the pipeline method and ServerHander construtor
}
});
ChannelFuture cf1 = b.bind().sync();
cf1.channel().closeFuture().sync();
pGroup.shutdownGracefully();
cGroup.shutdownGracefully();
EventLoopGroup pGroup=new NioEventLoopGroup();
EventLoopGroup cGroup=新的NioEventLoopGroup();
ServerBootstrap b=新的ServerBootstrap();
b、 组(pGroup,cGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,1024)
.option(ChannelOption.SO_SNDBUF,32*1024)
.option(ChannelOption.SO_RCVBUF,32*1024)
.option(ChannelOption.SO_KEEPALIVE,true)
.childHandler(新的ChannelInitializer(){//出现错误,根据eclipse自动提示,这表明泛型不是有效的替代项
@凌驾
受保护的无效初始化通道(SocketChannel sc)引发异常{
sc.pipeline().addLast(new ServerHandler());//关于pipeline方法和ServerHander construtor有两个错误
}
});
ChannelFuture cf1=b.bind().sync();
通道().closeFuture().sync();
pGroup.shutdownGracefully();
cGroup.shutdownGracefully();
我怀疑您在此处导入了错误的SocketChannel
。这需要是io.netty.channel.socket.SocketChannel
,但您很可能使用了java.nio。SocketChannel
这对jdkI来说不是问题,我想是的,因为代码很旧,他的jdk版本可能比我老。第二个错误行,关于处理程序的第二个错误不是错误,因为我还没有定义相应的类。你知道,在我的pc上安装另一个jdk对我来说不是优先选择。我将尝试修复它。我正在使用netty 4和jdk 8,没有问题,但是netty 4有很多分支,您选择了哪个版本?您是否像我为您展示的那样使用了它?抱歉,确切地说,我仍然有一个问题,那就是ChannelHandlerAdapter没有channelRead方法,在我定义ServerHandler类来处理来自客户端的异步消息时,只有handlerAdd和handlerRemove方法要重写。是否要使用ChannelInboundHandler