Recently in the study Netty related knowledge, think "Netty authoritative guide" This book is still very good, suitable for me this kind of beginner. In addition to many examples of Netty itself, it is very interesting to learn. Simply record,
The general server code is as follows:
public void Run () throws Exception {Eventloopgroup bossgroup = new Nioeventloopgroup (); Eventloopgroup Workergroup = new Nioeventloopgroup (); try {serverbootstrap b = new Serverbootstrap (); B.group (Bossgroup, Workergroup). Channel (Nioserversocketchannel.class). Childhandler (New Channelin Itializer<socketchannel> () {@Override public void Initchannel (Socketchannel ch) throw S Exception {ch.pipeline (). AddLast (New Objectencoder (), New Objectdecoder (classresolvers.cachedisabled (NULL)), new Objectechoserverhandler (), New Myserverhandler ()); } }); Bind and start to accept incoming connections. B.bind (port). Sync (). Channel (). Closefuture (). sync (); } finally {Bossgroup.shutdowngracefulLy (); Workergroup.shutdowngracefully (); } }
Read the "Netty authoritative guide" This book, the following code should be relatively easy to understand, as its name, a call Bossgroup (boss), a call Workergroup (worker), Bossgroup used to accept the request thread Group, Workergroup is used to process the IO operation thread Group, Bossgroup is called after receiving the request Workergroup to handle,
Eventloopgroup Bossgroup = new Nioeventloopgroup ();
Eventloopgroup Workergroup = new Nioeventloopgroup ();
Serverbootstrap is a startup tool class, and the Childhandler () method is used to add operations classes that process the channel and initialize them. Channelpipeline is used for intra-chain management of channel processing classes.
B.bind (port). Sync (). Channel (). Closefuture (). sync (); This sentence is broken down into two sentences to understand the point:
B.bind (port). sync (); Thread synchronization blocking waits for the server to bind to the specified port.
... channel (). Closefuture (). Sync () After successfully binding to the port, add a pipe-closed listener to the channel and block synchronously until the channel is closed, and the thread will execute down and end the process.
Netty Study Record One