Home > Back-end >  Netty frequently triggered when data volume many channelInactivice is why, online waiting for
Netty frequently triggered when data volume many channelInactivice is why, online waiting for

Time:09-30

If without sending data connection request just can see the number of connections in more than 3 w
Run Windows environment, my code is as follows:
Private void the start () {
LogHelper. Info (" Start server... ");
System. Out.println (" Start server... ");
EventLoopGroup bossGroup=new NioEventLoopGroup ();
EventLoopGroup workerGroup=new NioEventLoopGroup ();
ServerBootstrap bootstrap=new ServerBootstrap ();
Try {
The bootstrap. Group (bossGroup workerGroup);
The bootstrap. Channel (NioServerSocketChannel. Class);
//final String port=ResourceUtil. GetKey (" local_tcp_port ");
//final String webPort=ResourceUtil. GetKey (" local_web_port ");
The bootstrap. ChildHandler (new ChannelInitializer () {
@ Override
Protected void initChannel Channel (Channel) {
Try {
//ByteBuf delimiter=Unpooled. CopiedBuffer (". "getBytes ());
//ByteBuf bb=Unpooled. Buffer (0, 2048);
Channel. The config (.) setRecvByteBufAllocator (new AdaptiveRecvByteBufAllocator (4, 2048, Integer. MAX_VALUE));

ChannelPipeline pipeline=channel. The pipeline ();
//IdleStateHandler heartbeat, if the timeout triggers userEventTrigger in the Handle () method
Pipeline. AddLast (new IdleStateHandler (15, 0, 0, TimeUnit. MINUTES));//heart 5 minutes
//pipeline. AddLast (new CustomDecoder (2048,0,0,0,0, false));
Pipeline. AddLast (new MyDecoder (2048));
////filter encoding
//pipeline. AddLast (" decoder, "new ByteArrayDecoder ());
////filter encoding
//pipeline. AddLast (" encoder ", new ByteArrayEncoder ());

InetSocketAddress inet=(InetSocketAddress) channel. LocalAddress ();
If (inet. GetPort ()==Integer. The parseInt (SMSTool. Local_tcp_port))
Pipeline. AddLast (new ServerHandler ());
//else if (inet. GetPort ()==Integer. The parseInt (SMSTool. Local_web_port))
//pipeline. AddLast (new WebServerHandler (initCallQueue));
} the catch (Exception e) {
//TODO: handle the exception
//LogHelper getIntance (DSD) equals (e. oString ());
LogHelper. Error (e. oString ());
}

}
}). Option (ChannelOption SO_BACKLOG, 1024)
//option (ChannelOption RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator (65535))
ChildOption (ChannelOption SO_KEEPALIVE, true);


//bind port, and returns the ChannelFuture
ChannelFuture future=the bootstrap. Bind (Integer. The parseInt (SMSTool. Local_tcp_port)). The sync ();
ChannelFuture future2=the bootstrap. Bind (Integer. The parseInt (SMSTool. Local_web_port)). The sync ();
System. Out.println (" server started!" );
LogHelper. Info (" server started!" );
GetConnectionTotal ();

//wait for the client closed
//Channel Channel=
Future. Channel (.) closeFuture (). The sync ();
Future2. Channel (.) closeFuture (). The sync ();
ResourceLeakDetector. SetLevel (ResourceLeakDetector. Level. The PARANOID);//memory leak detection development recommended the PARANOID online SIMPLE
//remember that closeFuture (), is used to waiting for the server shut down
//channel. CloseFuture (). The sync ();
} the catch (Exception e) {
e.printStackTrace();
} the finally {
BossGroup. ShutdownGracefully (.) syncUninterruptibly ();
WorkerGroup. ShutdownGracefully (.) syncUninterruptibly ();
}
}

The compiler code:
@ Override
Protected void decode (ChannelHandlerContext CTX, ByteBuf buffer, List Out) {
Try {
Int readable=buffer. ReadableBytes ();
If (readable & gt; Too much MAX_FRAME_SIZE) {//the buffer data
Buffer. SkipBytes (readable);//ignore all bytes can be read
//throw an exception to inform the super-long frame data
Throw new TooLongFrameException (" super-long frame data ");
}
//create a byte array, buffer readableBytes readable bytes
Byte [] b=new byte (readable);
//copy the content to a byte array b
Buffer. ReadBytes (b);
Out. The add (buffer);
} the catch (Exception e) {
//TODO: handle the exception
LogHelper. GetIntance (). The error (" decode=& gt;" + e. oString ());
}

}

Business processing categories:

@ Override
Public void channelRead (ChannelHandlerContext CTX, Object MSG) throws the Exception {
//TODO Auto - generated method stub
ByteBuf buf=(ByteBuf MSG);
Byte [] the req=new byte [buf readableBytes ()];
Buf. ReadBytes (the req);
Byte [] req2=new byte [the req. Length + 2];
System. Arraycopy (the req, 0, req2, 0, the req. Length);
Byte [] bb.=the Convert shortToByte (SMSTool. GT06_FOOTER);
Req2 [req2. Length - 2]=bb [0];
=bb req2 [req2 length - 1] [1].

Super. ChannelRead (CTX, req2);
}
@ Override
Public void channelInactive ChannelHandlerContext (CTX) {
Try {

CTX. Channel (). The close ();
//super. ChannelInactive (CTX);
//total. GetList (). Remove (CTX) channel () remoteAddress (). The toString ());
Total. GetConnection (). DecrementAndGet ();//record the number of connections
System. Out.println (" disconnect - & gt;" + CTX. Channel (.) remoteAddress (), toString ());//disconnected
} the catch (Exception e) {
//TODO: handle the exception
LogHelper. Error (e. oString ());
}

}
This is a major code server, please directly!!!!!!!!!!!!

CodePudding user response:

This issue has found the reason, and his stick
  • Related