netty 使用注意事项

最近在使用netty的时候突然碰到这样的一个警告:

 

Java代码   收藏代码
  1. 2010-8-11 12:20:28 org.jboss.netty.util.internal.SharedResourceMisuseDetector  
  2. 警告: You are creating too many MemoryAwareThreadPoolExecutor instances.  MemoryAwareThreadPoolExecutor is a shared resource that must be reused across the application, so that only a few instances are created.  
  3. 2010-8-11 12:20:28 org.jboss.netty.util.internal.SharedResourceMisuseDetector  
  4. 警告: You are creating too many HashedWheelTimer instances.  HashedWheelTimer is a shared resource that must be reused across the application, so that only a few instances are created.  

说的是我在使用

MemoryAwareThreadPoolExecutor和HashedWheelTimer

的时候创造了太多的实例.后来一看源码才发现问题所在!这两个貌似都是线程池的对象,在各自的构造方法里面,每实例一个对象就会使各自SharedResourceMisuseDetector(滥用共享资源探测器)加一.当超过256的时候就报警了!

Java代码   收藏代码
  1. private static final SharedResourceMisuseDetector misuseDetector =  
  2.         new SharedResourceMisuseDetector(MemoryAwareThreadPoolExecutor.class);  
  3. .....  
  4. // Misuse check  
  5.         misuseDetector.increase();  

 后来再查看HashedWheelTimer的源代码中还发现了这样的提示:

Java代码   收藏代码
  1. <h3>Do not create many instances.</h3>  
  2.  *  
  3.  * {@link HashedWheelTimer} creates a new thread whenever it is instantiated and  
  4.  * started.  Therefore, you should make sure to create only one instance and  
  5.  * share it across your application.  One of the common mistakes, that makes  
  6.  * your application unresponsive, is to create a new instance in  
  7.  * {@link ChannelPipelineFactory}, which results in the creation of a new thread  
  8.  * for every connection.  

大致就说不要创建太多的实例

之前我是这样写的

Java代码   收藏代码
  1. public class ServerPipelineFactory implements ChannelPipelineFactory {  
  2. ...  
  3. @Override  
  4.     public ChannelPipeline getPipeline() throws Exception {  
  5. ChannelPipeline pipeline = pipeline();  
  6. pipeline.addLast("executor"new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(1610485761048576)));  
  7. pipeline.addLast("timeout"new ReadTimeoutHandler(new HashedWheelTimer(), 10));  

 这样以来每个channel获取PipelineFactory的时候都会重新实例MemoryAwareThreadPoolExecutor和HashedWheelTimer,

当连接一多的时候就报警了!

 

根据这个提示我修改了ServerPipelineFactory,把他们做出单例的引用

 

Java代码   收藏代码
  1. public class ServerPipelineFactory implements ChannelPipelineFactory {  
  2. ...  
  3. static OrderedMemoryAwareThreadPoolExecutor e = new OrderedMemoryAwareThreadPoolExecutor(1600);  
  4. static HashedWheelTimer hashedWheelTimer = new HashedWheelTimer();  
  5. static ExecutionHandler executionHandler = new ExecutionHandler(e);  
  6. @Override  
  7.     public ChannelPipeline getPipeline() throws Exception {  
  8. ChannelPipeline pipeline = pipeline();  
  9. pipeline.addLast("executor", executionHandler );  
  10. pipeline.addLast("timeout"new ReadTimeoutHandler(hashedWheelTimer, 10));  

 

 

这样就不会再有SharedResourceMisuseDetector的警告了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
93个netty高并发全面的教学视频下载,每个视频在400-700M,一到两个小时时长的视频,无机器码和解压密码,下载下来的就是MP4格式视频。点击即可观看学习。下载txt文档,里面有永久分享的连接。包括01_学习的要义;02_Netty宏观理解;03_Netty课程大纲深度解读;04_项目环境搭建与Gradle配置;05_Netty执行流程分析与重要组件介绍;06_Netty回调与Channel执行流程分析;07_Netty的Socket编程详解;08_Netty多客户端连接与通信,09_Netty读写检测机制与长连接要素,10_Netty对WebSocket的支援;11_Netty实现服务器端与客户端的长连接通信;12_Google Protobuf详解;13_定义Protobuf文件及消息详解;14_Protobuf完整实例详解;15_Protobuf集成Netty与多协议消息传递;16_Protobuf多协议消息支援与工程最佳实践;17_Protobuf使用最佳实践与Apache Thrift介绍;18_Apache Thrift应用详解与实例剖析;19_Apache Thrift原理与架构解析;20_通过Apache Thrift实现Java与Python的RPC调用;21_gRPC深入详解 ;22_gRPC实践 ;23_Gradle Wrapper在Gradle项目构建中的最佳实践;24_gRPC整合Gradle与代码生成············82_Netty引用计数原子更新揭秘与AtomicIntegerFieldUpdater深度剖析;83_AtomicIntegerFieldUpdater实例演练与volatile关键字分析;84_Netty引用计数注意事项与内存泄露检测方式;85_Netty编解码器剖析与入站出站处理器详解;86_Netty自定义编解码器与TCP粘包拆包问题;87_Netty编解码器执行流程深入分析;88_ReplayingDecoder源码分析与特性解读;89_Netty常见且重要编解码器详解;90_TCP粘包与拆包实例演示及分析;91_Netty自定义协议与TCP粘包拆包问题解决之道;92_精通并发与Netty课程总结与展望

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值