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


一、Netty學習資料

書籍《Netty In Action中文版》

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

深入淺出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/

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

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

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

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

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

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

二、代碼示例

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM