启动中出现
java.lang.NullPointerException: childHandler
at io.netty.bootstrap.ServerBootstrap.childHandler(ServerBootstrap.java:134)
at com.example.demo.server.NettyServer.startServer(NettyServer.java:44)
at com.example.demo.server.NettyServer.run(NettyServer.java:72)
at java.lang.Thread.run(Thread.java:748)下面是demo全部源码NettyServer 源码
package com.example.demo.server;import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.annotation.PreDestroy;
import javax.annotation.Resource;@Component(value = "nettyServer")
public class NettyServer implements Runnable { @Resource
private NettyServerInitializer nettyServerInitializer; @Resource
private NettyServerHandler nettyServerHandler; private int port = 7000; private EventLoopGroup boss = new NioEventLoopGroup();
private EventLoopGroup worker = new NioEventLoopGroup(); private ChannelFuture cf = null; /**
* 启动server
*/
public void startServer() {
try {
System.out.println("go!");
ServerBootstrap b = new ServerBootstrap();
b.group(boss, worker);
b.channel(NioServerSocketChannel.class);
b.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, 2048, 65536));
b.childHandler(nettyServerInitializer);
cf = b.bind(port).sync();
cf.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
} @PreDestroy
public void stop() {
try {
// 监听服务器关闭监听
if (cf != null)
cf.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
} @Override
public void run() {
try {
this.startServer();
} catch (Exception e) {
e.printStackTrace();
}
}
}NettyClient源码
package com.example.demo.server;import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import org.springframework.stereotype.Component;import javax.annotation.Resource;@Component
public class NettyServerInitializer extends ChannelInitializer<SocketChannel> { @Resource
private NettyServerHandler nettyServerHandler; @Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ByteArrayDecoder());
pipeline.addLast(new ByteArrayEncoder());
pipeline.addLast(nettyServerHandler);
}
}NettyHandler源码package com.example.demo.server;import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.springframework.stereotype.Component;@Component
@ChannelHandler.Sharable
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("server channel active... ");
} @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String body = (String)msg;
System.out.println("Server 接收到 :" + body );
String response = "返回给客户端的响应:" + body ;
ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes()));
} @Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("server: 读完了");
ctx.flush();
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}启动app程序源码package com.example.demo;import com.example.demo.client.NettyClient;
import com.example.demo.server.NettyServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DemoApplication { public static void runNettyServer(){
NettyServer nettyServer = new NettyServer();
Thread thread = new Thread(nettyServer);
thread.start();
System.out.println("Netty Server Start Success!");
} public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
try {
runNettyServer();
}catch (Exception e){
e.printStackTrace();
}
}}
java.lang.NullPointerException: childHandler
at io.netty.bootstrap.ServerBootstrap.childHandler(ServerBootstrap.java:134)
at com.example.demo.server.NettyServer.startServer(NettyServer.java:44)
at com.example.demo.server.NettyServer.run(NettyServer.java:72)
at java.lang.Thread.run(Thread.java:748)下面是demo全部源码NettyServer 源码
package com.example.demo.server;import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.annotation.PreDestroy;
import javax.annotation.Resource;@Component(value = "nettyServer")
public class NettyServer implements Runnable { @Resource
private NettyServerInitializer nettyServerInitializer; @Resource
private NettyServerHandler nettyServerHandler; private int port = 7000; private EventLoopGroup boss = new NioEventLoopGroup();
private EventLoopGroup worker = new NioEventLoopGroup(); private ChannelFuture cf = null; /**
* 启动server
*/
public void startServer() {
try {
System.out.println("go!");
ServerBootstrap b = new ServerBootstrap();
b.group(boss, worker);
b.channel(NioServerSocketChannel.class);
b.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, 2048, 65536));
b.childHandler(nettyServerInitializer);
cf = b.bind(port).sync();
cf.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
} @PreDestroy
public void stop() {
try {
// 监听服务器关闭监听
if (cf != null)
cf.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
} @Override
public void run() {
try {
this.startServer();
} catch (Exception e) {
e.printStackTrace();
}
}
}NettyClient源码
package com.example.demo.server;import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import org.springframework.stereotype.Component;import javax.annotation.Resource;@Component
public class NettyServerInitializer extends ChannelInitializer<SocketChannel> { @Resource
private NettyServerHandler nettyServerHandler; @Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ByteArrayDecoder());
pipeline.addLast(new ByteArrayEncoder());
pipeline.addLast(nettyServerHandler);
}
}NettyHandler源码package com.example.demo.server;import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.springframework.stereotype.Component;@Component
@ChannelHandler.Sharable
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("server channel active... ");
} @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String body = (String)msg;
System.out.println("Server 接收到 :" + body );
String response = "返回给客户端的响应:" + body ;
ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes()));
} @Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("server: 读完了");
ctx.flush();
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}启动app程序源码package com.example.demo;import com.example.demo.client.NettyClient;
import com.example.demo.server.NettyServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DemoApplication { public static void runNettyServer(){
NettyServer nettyServer = new NettyServer();
Thread thread = new Thread(nettyServer);
thread.start();
System.out.println("Netty Server Start Success!");
} public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
try {
runNettyServer();
}catch (Exception e){
e.printStackTrace();
}
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货