亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

java - 使用Netty Demo報錯

瀏覽:73日期:2023-12-24 15:20:14

問題描述

public class TimeServer { public void bind(int port) {try { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024) .childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel arg0) throws Exception { System.out.println('初始化'); arg0.pipeline().addLast(new TimeHandler());} }); ChannelFuture future = b.bind(port).sync(); System.out.println('執行這里'); future.channel().closeFuture().sync(); System.out.println('執行這里');} catch (InterruptedException e) { e.printStackTrace();} } public static void main(String[] args) {new TimeServer().bind(10000); }}public class TimeHandler extends ChannelInboundHandlerAdapter {@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {super.channelRead(ctx, msg); ByteBuf buf=(ByteBuf) msg;////byte[] butfs = buf.array();//報錯 System.out.println(buf.readableBytes());byte[] butfs = new byte[buf.readableBytes()];buf.readBytes(butfs);System.out.println(new String(butfs,'UTF-8'));System.out.println(msg); }}

客戶端使用的是BIO的模型:

public static void main(String[] args) throws Exception {final int port = 10000;//NioServer server = new NioServer(port);//server.init();/// ========================================================// 接下來模擬3個Client并發訪問服務器int poolsize = 1;ExecutorService pool = Executors.newFixedThreadPool(poolsize);Collection<Callable> tasks = new ArrayList<Callable>(10);final String clientname = 'clientThread';for (int i = 0; i < poolsize; i++) { final int n = i; // 若每一個Client都保持使用BIO方式發送數據到Server,并讀取數據。 tasks.add(new Callable() {@Overridepublic Object call() throws Exception { Socket socket = new Socket('127.0.0.1', port); final InputStream input = socket.getInputStream(); final OutputStream out = socket.getOutputStream(); final String clientname_n = clientname + '_' + n; // BIO讀取數據線程 new Thread(clientname_n + '_read') {@Overridepublic void run() { byte[] bs = new byte[1024]; while (true) {try { Thread.sleep(1000);} catch (InterruptedException e) { e.printStackTrace();}int len = 0;try { while ((len = input.read(bs)) != -1) {System.out.println('Clinet thread ' + Thread.currentThread().getName()+ ' read: ' + new String(bs, 0, len)); }} catch (IOException e) { e.printStackTrace();} }} }.start(); // BIO寫數據線程 new Thread(clientname_n + '_write') {@Overridepublic void run() { int a = 0; while (true) {try { Thread.sleep(100);} catch (InterruptedException e) { e.printStackTrace();}String str = Thread.currentThread().getName() + ' hello, ' + a;try { out.write(str.getBytes()); out.flush(); a++;} catch (IOException e) { e.printStackTrace();} }} }.start(); return null;} });}pool.invokeAll((Collection<? extends Callable<Object>>) tasks);//server.go(); }

結果運行的時候出現了以下錯誤:

月 13, 2017 5:52:56 下午 io.netty.channel.DefaultChannelPipeline onUnhandledInboundException警告: 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.io.netty.util.IllegalReferenceCountException: refCnt: 0 at io.netty.buffer.AbstractByteBuf.ensureAccessible(AbstractByteBuf.java:1408) at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1394) at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1383) at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:850) at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:858) at test.netty.TimeHandler.channelRead(TimeHandler.java:17) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:134) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:624) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:559) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:476) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:438) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144) at java.lang.Thread.run(Thread.java:745)

請問這是為什么呢?

問題解答

回答1:

在netty4中,對象的生命周期由引用計數器控制,ByteBuf就是如此,每個對象的初始化引用計數為1,調用一次release方法,引用計數器會減1,當嘗試訪問計數器為0的,對象時,會拋出IllegalReferenceCountException,正如ensureAccessible的實現,更加詳細的解釋可以參考官方文檔

AbstractByteBuf.java

protected final void ensureAccessible() {if (refCnt() == 0) { throw new IllegalReferenceCountException(0);} }

注意TZ的TimeHandler類中的 super.channelRead(ctx, msg);這行代碼。追蹤調用路徑,

private void invokeChannelRead(Object msg) {try { ((ChannelInboundHandler) handler()).channelRead(this, msg);} catch (Throwable t) { notifyHandlerException(t);} }

最終調用的代碼是:ReferenceCountUtil.release(msg)

public static boolean release(Object msg) {if (msg instanceof ReferenceCounted) { return ((ReferenceCounted) msg).release();}return false; }

也就是每次super.channelRead(ctx, msg);后,ByteBuf就會調用release()方法,計數器減一,然后在buf.readBytes(butfs);這行代碼就會校驗ensureAccessible(),計數器為0,netty認為ByteBuf對象已經釋放,就拋出異常。

解決方案:

去掉TimeHandler中這行代碼 super.channelRead(ctx, msg);ByteBuf對象誰處理誰釋放。

回答2:

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.缺少exceptionCaught(),在在server端最后一個Handler中增加exceptionCaught()

標簽: java
相關文章:
主站蜘蛛池模板: 日本香蕉视频 | 激情三级hd中文字幕 | www.黄色网.com | 精品国产一区二区三区四 | 全免费a级毛片 | 日韩欧美亚洲一区二区综合 | 黄黄视频在线观看 | 精品国产品国语在线不卡丶 | 麻豆影视视频高清在线观看 | 久久网页| 国产精品视频免费播放 | 性夜黄 a 爽免费看 性性影院在线观看 | 精品国产免费一区二区 | 精品国产第一国产综合精品gif | 国产精品一区二区三区四区 | 91网址在线观看 | 亚洲欧美综合国产精品一区 | 国产精品亚洲高清一区二区 | 欧美 日韩 亚洲另类专区 | 视频一区二区三区自拍 | 中文字幕亚洲综合久久2 | 成人免费网站视频 | 国产大片91精品免费观看不卡 | 一级毛片看真人在线视频 | 一级黄色a | 台湾亚洲精品一区二区tv | 婷婷激情久久 | 亚洲日本va在线观看 | 国产首页精品 | 亚洲精品二区中文字幕 | 免费在线观看小视频 | jk足控福利国产在线播放 | 亚洲精品视频在线免费 | 欧美性色黄 | 色琪琪综合网站 | 国产精品入口麻豆高清在线 | 午夜看一级特黄a大片 | 久久视屏这里只有精品6国产 | 欧美成人午夜 | 婷婷国产在线 | 男人影院在线观看 |