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

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-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-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实战.epub_netty实战epub_netty实战epub_netty_

    《Netty实战》这本书是针对Java网络编程框架Netty的一本深入实践教程,旨在帮助读者掌握Netty的核心特性和实际应用。Netty是一款高性能、异步事件驱动的网络应用程序框架,广泛应用于各种分布式系统、微服务架构以及...

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

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

    跟闪电侠学Netty:Netty即时聊天实战与底层原理-book-netty.zip

    《跟闪电侠学Netty:Netty即时聊天实战与底层原理》是一本深入浅出的Netty技术指南,旨在帮助读者掌握Netty框架,并利用它实现即时聊天应用,同时理解其底层工作原理。Netty是Java领域的一款高性能、异步事件驱动的...

    Netty实战 电子版.pdf_java_netty_服务器_

    《Netty实战》是针对Java开发者的一本技术指南,它深入介绍了如何利用Netty这个高性能、异步事件驱动的网络应用程序框架来构建高效且可扩展的网络应用。Netty不仅简化了网络编程的复杂性,还提供了丰富的特性和组件...

    Netty进阶之路-跟着案例学Netty

    《Netty进阶之路-跟着案例学Netty》是由知名技术专家李林峰撰写的一本专为Java开发者深入理解Netty框架而准备的书籍。这本书旨在通过实例教学,帮助读者全面掌握Netty的核心特性和实战技巧,提升网络编程的能力。 ...

    springboot整合netty的demo

    在SpringBoot项目中,我们可以通过添加对应的依赖来引入Netty,例如在`pom.xml`中加入`spring-boot-starter-webflux`,这个依赖集成了Reactor Netty,它是Spring Framework 5.x对响应式编程的支持,底层就是基于...

    netty-all-4.1.29.Final-javadoc.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 ...

Global site tag (gtag.js) - Google Analytics