Netty send and receive data handler processors are primarily inherited Simplechannelinboundhandler and channelinboundhandleradapter
The general use of Netty to send and receive data will inherit Simplechannelinboundhandler and Channelinboundhandleradapter These two abstract classes, then what is the difference between the two?
In fact, with these two abstract classes are fastidious, in the client 's business handler inherit is Simplechannelinboundhandler, and on the server side inherited is Channelinboundhandleradapter.
The main difference is that Simplechannelinboundhandler automatically release the Bytebuffer resource (called Bytebuffer.release ()) that is consumed by the data after it receives the data. And why the server is not available, because we want the server to send the data requested by the client back, and the server side may not have finished writing the data before the Channelread method returns, so it cannot be automatically release.
Handler processor built-in methods
Channelactive
Triggered when the channel is activated, when the client connect succeeds, the server receives the event, allowing the client's channel to be recorded for later reuse
Channelread
This must be used, when the data received from the other side of the trigger, the parameter MSG is the information sent, can be the basic type, or can be serialized complex object.
Channelreadcomplete
Channelread Trigger after execution
Exceptioncaught
Errors are triggered and some error handling is done
Specific examples of inheritance Channelinboundhandleradapter
/*** Netty Server's Listening processor * *@authorFLM October 27, 2017*/ Public classIohandlerextendsChannelinboundhandleradapter {Private StaticLogger log = Logger.getlogger (Iohandler.class); //Netty Attributekey relative to Web session "important" Public Static Finalattributekey<devicesession> KEY = attributekey.valueof ("IO"); PrivateProducer Producer; PublicIohandler (Producer Producer) { This. producer=producer; }/*** Read Data*/@Override Public voidChannelread (Channelhandlercontext ctx, Object msg)throwsException {devicesession session= Ctx.channel (). attr (KEY). get ();//detects if the client is registered by itselfBytebuf Buffer=(BYTEBUF) msg; if(Buffer = =NULL|| Session = =NULL) {closeconnection (CTX);//Close Connection} msgentity msgentity=Newmsgentity (buffer);//Decode Buffer Package msgentityLog.info ("# Accept Client data:" +msgentity.tostring ()); if(Msgtype.unknow = =Msgentity.getmsgtype ()) {Log.info ("# Client send data type undefined ...:" +msgentity.tostring ()); return; } if(!session.isactivity ()) {session.setactivity (true); Session.setimei (Msgentity.getimei ()); Sessionmanager.getsingleton (). Addclient (session); } producer.putdata (msgentity); } /*** Client Registration*/@Override Public voidChannelregistered (Channelhandlercontext ctx)throwsException {Super. channelregistered (CTX); Log.info (String.Format ("# Client Registered ... :%s ... ", Ctx.channel ())); Devicesession Session=Newdevicesession (Ctx.channel ()); //bind client to socketCtx.channel (). attr (KEY). Set (session); } /*** Client loses connection*/@Override Public voidChannelinactive (Channelhandlercontext ctx)throwsException {Super. channelinactive (CTX); Log.info (String.Format ("# Client out ...:%s", Ctx.channel ())); Devicesession Session= Ctx.channel (). attr (KEY). Getandset (NULL); //Remove session and delete the clientSessionmanager.getsingleton (). Removeclient (Session,true); if(Session.getdeviceid ()! =NULL) { //Producer.ondata (New Request (New Rootmessage (messagetype.logout, NULL, NULL), session)); } } /*** Heartbeat mechanism user Event trigger*/@Override Public voidUsereventtriggered (Channelhandlercontext ctx, Object evt)throwsException {if(evtinstanceofidlestateevent) {idlestateevent e=(idlestateevent) evt; //detects if there is no contact with the server during this time if(e.state () = =idlestate.all_idle) { //Heartbeat DetectionCheckidle (CTX); } } Super. usereventtriggered (CTX, evt); } /*** ERROR Handling Events*/@Override Public voidexceptioncaught (Channelhandlercontext ctx, throwable cause)throwsException {log.error ("# Client Connection Netty error ..."); Cause.printstacktrace (); //Close ConnectionCloseConnection (CTX); }
netty--advanced send and receive data handler processors