使用netty4.x用戶端接收較大資料量報文時發生的讀取不完整bug修複記錄

來源:互聯網
上載者:User

標籤:讀取   pen   targe   rem   調用   ddl   this   bootstrap   解決   

1、先說問題

背景:服務是運行在Linux上的安全網關提供的,TCP協議發送 通過二進位編碼的xml字串 報文,報文頭的第一個欄位是int類型的表示位元組序標記,第二個欄位是int類型的表示整個報文長度。

現象:資料量較小時完全可以正常收發報文,當服務端發送的報文資料量較大時(本例是將近600k)機率性出現接收資料直接調用readComplete()方法而沒有走channelRead()

跟蹤:跟蹤代碼發現出問題時context 的 read() 方法執行中讀取到一百多k(有時兩百多也可能三百多,總之是還沒讀取到全部資料)時某次讀到的資料本應該是1024位元組(填滿預設分配的ByteBuf)卻唯讀到了576位元組;

    netty架構代碼中判斷如果當前讀到的位元組數小於ByteBuf的size則認為是讀取完成,因此調用了readComplete()方法,出錯。。。

解決方案:在ClientHandler類添加一個標記flag,用於是否正常讀取資料判斷。channelRead()方法正常調用則將其置為true;readComplete方法中添加一個判斷只有當flag為true時關閉context否則繼續調用ctx.read()。

2、再上核心代碼

Client:

 1 ClientHandler clientHandler = new ClientHandler(this); 2             bootstrap.group(eventLoop) 3                     .channel(NioSocketChannel.class) 4                     .option(ChannelOption.TCP_NODELAY, true) 5                     .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000) 6                     .option(ChannelOption.MAX_MESSAGES_PER_READ, Integer.MAX_VALUE) 7                     .handler(new ClientChannelInitializer(clientHandler)); 8              9             ChannelFuture f = bootstrap.connect(host, port).sync();10 11             f.channel().closeFuture().sync();
 1 private class ClientChannelInitializer extends ChannelInitializer<SocketChannel> { 2         private ClientHandler clientHandler; 3  4         public ClientChannelInitializer(ClientHandler clientHandler) { 5             this.clientHandler = clientHandler; 6         } 7  8         @Override 9         protected void initChannel(SocketChannel socketChannel) throws Exception {10 11             socketChannel.pipeline().addLast(new SplDecoder());12             socketChannel.pipeline().addLast(clientHandler);13             channel = socketChannel;14         }15     }
// 解決問題前 initChannel的實現是這樣的,使用了netty內部的長度欄位解碼器
@Override
protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,4,4,-8,0)); ch.pipeline().addLast(clientHandler);}

 

ClientHandler:

public class ClientHandler extends ChannelInboundHandlerAdapter {
 1 @Override 2     public void channelActive(ChannelHandlerContext context) throws Exception { 3         logger.info("Ready to send request..."); 4         ByteBuffer result = getByteBuffer(); 5         ByteBuf buf = Unpooled.buffer(result.remaining()); 6         buf.writeBytes(result); 7  8         context.writeAndFlush(buf); 9     }10 11     @Override12     public void channelRead(ChannelHandlerContext context, Object msg) throws Exception {13         logger.info("Get server response...");14 15         String[] result = (String[]) msg;16 17         logger.debug("response xml is : " + result[1]);18         client.setResponse(result);19 20         ok = true;21     }22 23     @Override24     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {25         ctx.flush();26         if (ok) {27             ctx.close();28         } else {29             ctx.read();30         }31     }

3、最後說解決過程

起初我懷疑是使用netty的定長欄位解碼器LengthFieldBasedFrameDecoder參數不當引起的,因為自認為對它理解不深;於是自己寫了一個繼承byteToMessageDecoder的解碼器可以實現解決拆包問題和解碼功能,但是問題依然機率性出現...

後來抱著試試看的態度在ClientHandler裡面添加了一個執行個體屬性ok(預設false),在正常執行channelRead()方法後將其置為true,readComplete()方法中做判斷如果ok==false調用ctx.read(),運行發現完美解決問題

因為調用read()方法是繼續讀取資料而不是重新讀取(因為此時ctx和channel、pipline等資料狀態都沒變)!

在SplDecoder類中添加當前讀取資料列印資訊:“logger.debug("讀取資料:本次" + readableBytes + ";累計" + currentLength + ";總共" + total);”;

在ctx.read()前面添加列印錯誤資訊“****** 讀取資料不完整,再次讀取......”

運行正常和出錯時的控制台列印資訊如下(由於實際列印行數太多,我用"......"代替了部分重複行):

 1 Connected to the target VM, address: ‘127.0.0.1:62194‘, transport: ‘socket‘ 2 log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory). 3 log4j:WARN Please initialize the log4j system properly. 4 讀取資料:本次1024;累計1024;總共574842 5 讀取資料:本次1024;累計2048;總共574842 6 讀取資料:本次1024;累計3072;總共574842 7 ...... 8 讀取資料:本次1024;累計572416;總共574842 9 讀取資料:本次1024;累計573440;總共57484210 讀取資料:本次1024;累計574464;總共57484211 讀取資料:本次378;累計574842;總共57484212 0 ~~ null13 Disconnected from the target VM, address: ‘127.0.0.1:62194‘, transport: ‘socket‘14 15 Process finished with exit code 0
正常時結果
 1 Connected to the target VM, address: ‘127.0.0.1:62068‘, transport: ‘socket‘ 2 log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory). 3 log4j:WARN Please initialize the log4j system properly. 4 讀取資料:本次1024;累計1024;總共574842 5 讀取資料:本次1024;累計2048;總共574842 6 讀取資料:本次1024;累計3072;總共574842 7 讀取資料:本次1024;累計4096;總共574842 8 讀取資料:本次1024;累計5120;總共574842 9 讀取資料:本次1024;累計6144;總共57484210 讀取資料:本次1024;累計7168;總共57484211 讀取資料:本次1024;累計8192;總共57484212 讀取資料:本次1024;累計9216;總共57484213 讀取資料:本次1024;累計10240;總共57484214 讀取資料:本次1024;累計11264;總共57484215 讀取資料:本次1024;累計12288;總共57484216 讀取資料:本次1024;累計13312;總共57484217 讀取資料:本次576;累計13888;總共57484218 ****** 讀取資料不完整,再次讀取......19 讀取資料:本次16384;累計30272;總共57484220 讀取資料:本次16384;累計46656;總共57484221 讀取資料:本次16384;累計63040;總共57484222 ......23 讀取資料:本次16384;累計554560;總共57484224 讀取資料:本次16384;累計570944;總共57484225 讀取資料:本次3898;累計574842;總共57484226 0 ~~ null27 Disconnected from the target VM, address: ‘127.0.0.1:62068‘, transport: ‘socket‘28 29 Process finished with exit code 0
出錯時結果

 結果最後列印0~~null表示正常結束(返回code為0錯誤資訊為null)。

附:

問題處理前控制台列印結果

1 "C:\Program Files...2 log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory).3 log4j:WARN Please initialize the log4j system properly.4 -1 ~~ 服務異常;Detail:java.lang.NullPointerException5 6 Process finished with exit code 0
問題處理前控制台列印資訊

由於沒有執行channelRead()方法,所以我擷取到的資料沒能執行賦值操作,報了null 指標異常。

結果最後列印 -1 ~~ 服務異常;Detail:java.lang.NullPointerException 表示發生了異常(返回code為-1;錯誤資訊為"服務異常;Detail:java.lang.NullPointerException")。

使用netty4.x用戶端接收較大資料量報文時發生的讀取不完整bug修複記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.