`
fujohnwang
  • 浏览: 153084 次
社区版块
存档分类
最新评论

Netty Framework Tips And Gotchas

    博客分类:
  • Tech
阅读更多

王福强(Darren.Wang)


1. Tips of Netty

 

  1. annotation doesn't effect anything, pipeline factory do!

  2. always provide your own PipelineFactory so that others can see your pipeline overview; add java doc(@see) in your ChannelHandler to point to the PipelineFactory definition class for further documentation;

  3. prevent re-invent the wheels that has been available;(Since Netty has provided lot of available ChannelHandler implementations we can use.)

  4. pay attention to event-driven attribute of Netty, simply put, the "messageReceived" method will be invoked multiple times, so take care of the state of your data carefully.

  5. use LoggingHandler to debug.

    LoggingHandler use JDK logging as default logging facility. If we want to change to use other ones, we need invoke InternalLoggingFactory.setDefaultFactory(..) before any netty classes are loaded. for example, if we want to use slf4j logging facility:

    InternalLoggingFactory.setDefaultFactory(new Slf4JLoggingFactory();
     
  6. You have to invoke channel.close() in another thread other than the IOWorker thread.

    public void exceptionCaught(final ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
                new Thread(){
                    public void run(){
                        ctx.getChannel().close().addListener(new ChannelFutureListener() {
                            public void operationComplete(ChannelFuture future) throws Exception {
                                future.awaitUninterruptibly();
                                getBoostrap().releaseExternalResources();
                                logger.debug("shutdown");
                            }
                        });
                    }
                }.start();
        }
     

     

     

  7. use IdleStateHandler and IdleStateAwareChannelHandler together to achieve some functionalities like connection status checking, long-push server mocks, etc.

    Note

    use a global Timer to share between all of you IdleStateHandlers instead of creating each for them. A Timer can suffice because of its implementation mechanism(TimerWheel).

     

  8. always set “connectTimeoutMillis” connection option to achieve timeout return. without this option, future.awaitUninterruptibly(timeoutValue) means less.

  9. More...

 

2. Gotchas of Netty

 

  1. 在使用ChannelBuffer的readBytes和getBytes的时候要注意index的意义不同.

    例 如: 当前ChannelBuffer中有10个byte, 你通过readBytes读取了4个, 然后, 想查看一下下一个byte的值, 那么,你可以通过readByte()方法, 然后resetReaderIndex(); 或者, 你可以通过getByte(4)来peek这个值, 这里要注意的就是, getByte传入的index是最初的ChannelBuffer开始位置进行计算, 而不是剩余的bytes的位置进行计算. 即不是getByte(0).

  2. @ChannelPipelineCoverage("one") Annotation doesn’t mean too much. It only works as a tip, nothing more. If you think you mark a ChannelHandler with “one” and Netty will use the ChannelHandler as a prototype, then you are wrong. It’s still your responsibility to ensure the scope semantics of singleton and prototype.

  3. 在ChannelHandler中, 通过exceptionCaught方法再次将异常抛出以期望更上层来处理是没有前途的。 因为这种情况下抛出的异常将只是由DefaultChannelPipeline记录一条warning的日志,仅此而已, 你无法进一步插足该异常的处理。

    另选方案可能是, 在exceptionCaught方法中将要抛出以交给其他对象处理的异常放入某个共享状态中, 比如某个queue, 然后, 对这些异常感兴趣的对象可以对该队列进行轮询以处理之。

  4. More...

 

分享到:
评论

相关推荐

    Netty in Action(Manning,2015)

    Netty in Action introduces the Netty framework and shows you how to incorporate it into your Java network applications. You'll learn to write highly scalable applications without the need to dive into...

    Netty In Action

    Netty is a Java-based networking framework designed to handle asynchronous network events smoothly so your applications are easy to write and maintain. The framework hides all the boilerplate and low...

    netty-all-4.1.22.Final.jar netty最新版jar包,直接导入

    Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming ...

    netty-all-4.1.10.Final.jar netty最新版jar包,直接导入

    Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming ...

    Netty in Action

    Netty is a Java-based networking framework designed to handle asynchronous network events smoothly so your applications are easy to write and maintain. The framework hides all the boilerplate and low...

    netty-all-4.1.29.Final-sources.jar 最新版netty源码

    Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programm ...

    Netty in Action.2014(MEAP v08)

    Netty in Action introduces the Netty framework and shows you how to incorporate it into your Java network applications. You'll learn to write highly-scalable applications without the need to dive into...

    netty-all-4.1.29.Final.jar最新版导入直接用

    Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming ...

    netty-all-4.1.97.Final-20230731.020124-2.jar netty最新jar包,可直接导入使用

    Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming

    Netty in Action (Netty 实战)

    Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming ...

    netty-netty-4.1.96.Final.tar.gz 官网最新版Netty Project

    Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programm ing

    Netty基础,用于学习Netty,参考黑马程序员的netty教程

    Netty基础,用于学习Netty,参考黑马程序员的netty教程

    springboot整合 netty做心跳检测

    springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合...

    netty包netty包

    netty-3.2.5.Final.jar netty包

    Netty (netty-netty-4.0.56.Final.tar.gz)

    Netty (netty-netty-4.0.56.Final.tar.gz)是一个 NIO 客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化和流线了网络编程,例如 TCP 和 UDP 套接字服务器。 “快速和简单”并...

    netty-all-4.1.32.Final-sources.jar 最新版netty源码全部包

    netty-buffer-4.1.32.Final-sources.jar netty-buffer-4.1.32.Final.jar netty-build-22-sources.jar netty-build-22.jar netty-codec-4.1.32.Final-sources.jar netty-codec-4.1.32.Final.jar netty-codec-...

    Netty入门教程文档

    Netty入门教程

    netty-netty-3.10.6.Final.tar.gz

    Netty (netty-netty-3.10.6.Final.tar.gz)是一个 NIO 客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化和流线了网络编程,例如 TCP 和 UDP 套接字服务器。 “快速和简单”并...

Global site tag (gtag.js) - Google Analytics