执行netty服务端代码,突然报如下错误
java.lang.IllegalStateException: channel or channelFactory not set
at io.netty.bootstrap.AbstractBootstrap.validate(AbstractBootstrap.java:214)
at io.netty.bootstrap.ServerBootstrap.validate(ServerBootstrap.java:195)
at io.netty.bootstrap.ServerBootstrap.validate(ServerBootstrap.java:43)
at io.netty.bootstrap.AbstractBootstrap.bind(AbstractBootstrap.java:273)
at io.netty.bootstrap.AbstractBootstrap.bind(AbstractBootstrap.java:252)
at netty.bytes.TimeServer.run(TimeServer.java:28)
at netty.bytes.TimeServer.main(TimeServer.java:38)
经过定位,发现是代码不小心删除一行代码
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) //误删了
.childHandler(new ChannelInitializer<SocketChannel>() {
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new TimeServerHandler());
};
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
以上代码中的.channel(NioServerSocketChannel.class)
被误删了