netty 异常对象传递
?????????
???????????????
???????????????????????????????
自定义对象
??????????
DivideRequest
@Data
public class DivideRequest implements Serializable {
private Integer dividend; //被除数
private Integer divisor; //除数
}
????????
DivideResponse
@Data
public class DivideResponse implements Serializable {
private Integer dividend;
private Integer divisor;
private Double result; //正常处理结果
private Boolean hasError; //在响应中标识是否有异常
private String errorMessage; //如果有异常,返回响应信息
}
???????????
????????????????
???????????????????????????????
服务端
???
CustomServerHandler
public class CustomServerHandler extends SimpleChannelInboundHandler<DivideRequest> {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, DivideRequest divideRequest) throws Exception {
System.out.println("服务端收到客户端的数据:"+divideRequest);
DivideResponse response = new DivideResponse();
response.setDividend(divideRequest.getDividend());
response.setDivisor(divideRequest.getDivisor());
if (divideRequest.getDivisor().equals(0)){
response.setHasError(true);
response.setErrorMessage("除数不能等于0");
}else{
response.setHasError(false);
response.setResult(1.0*divideRequest.getDividend()/divideRequest.getDivisor());
}
channelHandlerContext.channel().writeAndFlush(response);
}
}
???????
NettyServer
public class NettyServer {
public static void startServer(int port){
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline channelPipeline = socketChannel.pipeline();
channelPipeline.addLast(new ObjectEncoder());
channelPipeline.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(this.getClass().getClassLoader())));
channelPipeline.addLast(new CustomServerHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
channelFuture.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) {
startServer(8000);
}
}
????????
???????????????????
???????????????????????????????
客户端
??
CustomClientHandler
public class CustomClientHandler extends SimpleChannelInboundHandler<DivideResponse> {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("客户端开始发送数据");
DivideRequest request = new DivideRequest();
request.setDividend(10);
request.setDivisor(3);
ctx.channel().writeAndFlush(request);
DivideRequest request2 = new DivideRequest();
request2.setDividend(10);
request2.setDivisor(0);
ctx.channel().writeAndFlush(request2);
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, DivideResponse divideResponse) throws Exception {
System.out.println("客户端接到响应数据:"+divideResponse);
if (divideResponse.getHasError()){
throw new Exception(divideResponse.getErrorMessage());
}else {
System.out.println(divideResponse.getDividend()+"/"+ divideResponse.getDivisor()+"="+ divideResponse.getResult());
}
}
}
???????
NettyClient
public class NettyClient {
public static void connect(String host, int port){
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline channelPipeline = socketChannel.pipeline();
channelPipeline.addLast(new ObjectEncoder());
channelPipeline.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(this.getClass().getClassLoader())));
channelPipeline.addLast(new CustomClientHandler());
}
});
ChannelFuture channelFuture = bootstrap.connect(host,port).sync();
channelFuture.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}finally {
eventLoopGroup.shutdownGracefully();
}
}
public static void main(String[] args) {
String host = "localhost";
int port = 8000;
connect(host, port);
}
}
????????
????????????????
???????????????????????????????
使用测试
??
点击运行后,服务端输出:
09:47:56.091 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
09:47:56.091 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.chunkSize: 32
09:47:56.091 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.blocking: false
09:47:56.103 [nioEventLoopGroup-3-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkAccessible: true
09:47:56.103 [nioEventLoopGroup-3-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkBounds: true
09:47:56.108 [nioEventLoopGroup-3-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@267a5af8
服务端收到客户端的数据:DivideRequest(dividend=10, divisor=3)
服务端收到客户端的数据:DivideRequest(dividend=10, divisor=0)
?????????????
点击运行后,客户端输出:
09:47:55.842 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimIntervalMillis: 0
09:47:55.842 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.useCacheForAllThreads: false
09:47:55.842 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedByteBuffersPerChunk: 1023
09:47:55.856 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
09:47:55.856 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
09:47:55.856 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
客户端开始发送数据
09:47:55.993 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
09:47:55.993 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
09:47:55.993 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.chunkSize: 32
09:47:55.993 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.blocking: false
09:47:56.004 [nioEventLoopGroup-2-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkAccessible: true
09:47:56.005 [nioEventLoopGroup-2-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkBounds: true
09:47:56.008 [nioEventLoopGroup-2-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@6b4b6cf0
客户端接到响应数据:DivideResponse(dividend=10, divisor=3, result=3.3333333333333335, hasError=false, errorMessage=null)
10/3=3.3333333333333335
客户端接到响应数据:DivideResponse(dividend=10, divisor=0, result=null, hasError=true, errorMessage=除数不能等于0)
09:47:56.178 [nioEventLoopGroup-2-1] WARN io.netty.channel.DefaultChannelPipeline - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
java.lang.Exception: 除数不能等于0
at com.example.demo.invoke.CustomClientHandler.channelRead0(CustomClientHandler.java:27)
at com.example.demo.invoke.CustomClientHandler.channelRead0(CustomClientHandler.java:6)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:327)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:299)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:722)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:995)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
????????
?????????????????
|