I. Netty study Materials
Book "Netty in Action Chinese version"
11 Questions for Netty
http://news.cnblogs.com/n/205413/
In layman Netty
Http://wenku.baidu.com/view/7765bc2db4daa58da0114a4c.html
Netty Understanding and small test
Http://www.cnblogs.com/xd502djj/archive/2012/06/25/2561318.html
Netty series of Netty High-performance Road "Wonderful"
http://www.infoq.com/cn/articles/netty-high-performance/
Netty series of Netty server Create "wonderful"
Http://www.infoq.com/cn/articles/netty-server-create
Netty 5 User Guide
http://ifeve.com/netty5-user-guide/
Based on the Netty5.0 starter case six nettyserver mass message
Http://www.bubuko.com/infodetail-575271.html
Netty5 Getting Started learning notes 002-TCP solution to the problem of sticky bag/unpacking (UP)
http://my.oschina.net/imhoodoo/blog/357290
Message service based on Netty and RABBITMQ
Http://www.cnblogs.com/luxiaoxun/archive/2015/01/28/4257105.html
Nettyserver string encoder based on Netty5.0 starter case Five
Http://www.itstack.org/?post=9
Second, code example
Hello World
1. Write processor Discardserverhandler extends Channelhandleradapter
2. Write the main method to start the Discardserverhandler
Echo
@Override
Public void Channelread (Channelhandlercontext ctx, Object msg) {
bytebuf in = (bytebuf) msg;
try {
While (In.isreadable ()) {//(1)
System.out.print ((char) in.readbyte ());
System.out.flush ();
}
} finally {
referencecountutil.release (msg);//(2)
}
}
Time
@Override
public void Channelactive (final channelhandlercontext ctx) {//(1)
Final Bytebuf time = Ctx.alloc (). buffer (4); (2)
Time.writeint ((int) (System.currenttimemillis ()/1000L + 2208988800L));
final Channelfuture f = ctx.writeandflush (time);//(3)
F.addlistener (New Channelfuturelistener () {
@Override
Public void Operationcomplete (channelfuture) {
assert F = = future;
ctx.close ();
}
}); (4)
}
Stream data transfer processing "subcontracting, sticky package"
1.ChannelHandler has 2 life cycle monitoring methods: handleradded () and handlerremoved (). You can complete any initialization task as long as he won't be blocked for a long time.
public class Timeclienthandler extends Channelhandleradapter {
Private Bytebuf buf;
@Override
public void handleradded ( Channelhandlercontext ctx) {
buf = Ctx.alloc (). buffer (4); (1)
}
@Override
public void handlerremoved ( Channelhandlercontext ctx) {
buf.release (); (1)
buf = null;
}
@Override
public void Channelread ( Channelhandlercontext ctx, Object msg) {
bytebuf m = (bytebuf) msg;
Buf.writebytes (m); (2)
m.release ();
if ( Buf.readablebytes () >= 4) {//(3)
Long Currenttimemillis = (Buf.readint ()- 2208988800L) * 1000L;
System.out.println (New Date (Currenttimemillis));
ctx.close ();
}
}
@Override
public void Exceptioncaught ( Channelhandlercontext CTX, Throwable cause) {
Cause.printstacktrace ();
Ctx.close ();
}
}
2.timeclienthandler split into 2 processors: Timedecoder handles data splitting, timeclienthandler the implementation of the original version
public class Timedecoder extends Bytetomessagedecoder {//(1)
@Override
protected void decode (Channelhandlercontext ctx, bytebuf in, list<object> out) {//(2)
if (in.readablebytes ( ) < 4) {
return; (3)
}
Out.add (In.readbytes (4)); (4)
}
}
Replace Bytebuf with Pojo
Reference:
Http://www.cnblogs.com/itfly8/p/5844929.html
Java Communication Series III: Netty Primer Summary