JAVA通訊系列三:Netty入門總結

1、Netty學習資料html

書籍《Netty In Action中文版》ide

對於Netty的十一個疑問
http://news.cnblogs.com/n/205413/post

深刻淺出Netty
http://wenku.baidu.com/view/7765bc2db4daa58da0114a4c.html性能

Netty瞭解與小試
http://www.cnblogs.com/xd502djj/archive/2012/06/25/2561318.html學習

Netty系列之Netty高性能之道【精彩】
http://www.infoq.com/cn/articles/netty-high-performance/ui

Netty系列之Netty 服務端建立【精彩】
http://www.infoq.com/cn/articles/netty-server-create編碼

Netty 5用戶指南
http://ifeve.com/netty5-user-guide/.net

基於Netty5.0入門案例六之NettyServer羣發消息
http://www.bubuko.com/infodetail-575271.htmlnetty

Netty5入門學習筆記002-TCP粘包/拆包問題的解決之道(上)
http://my.oschina.net/imhoodoo/blog/357290code

基於Netty與RabbitMQ的消息服務
http://www.cnblogs.com/luxiaoxun/archive/2015/01/28/4257105.html

基於Netty5.0入門案例五之NettyServer字符串編碼器
http://www.itstack.org/?post=9

2、代碼示例

Hello world
1.編寫處理器 DiscardServerHandler extends ChannelHandlerAdapter
2.編寫Main方法,啓動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 future) {
                assert f == future;
                ctx.close();
            }
        }); // (4)
    }

流數據的傳輸處理【分包,黏包】
1.ChannelHandler有2個生命週期的監聽方法:handlerAdded()和handlerRemoved()。你能夠完成任意初始化任務只要他不會被阻塞很長的時間。
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拆分紅2個處理器:TimeDecoder處理數據拆分的問題,TimeClientHandler原始版本的實現

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)
    }
}

用POJO代替ByteBuf

相關文章
相關標籤/搜索