`
milk_36
  • 浏览: 118046 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

netty 使用注意事项

阅读更多

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

 

2010-8-11 12:20:28 org.jboss.netty.util.internal.SharedResourceMisuseDetector
警告: 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.
2010-8-11 12:20:28 org.jboss.netty.util.internal.SharedResourceMisuseDetector
警告: 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的时候就报警了!

private static final SharedResourceMisuseDetector misuseDetector =
        new SharedResourceMisuseDetector(MemoryAwareThreadPoolExecutor.class);
.....
// Misuse check
        misuseDetector.increase();

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

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

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

之前我是这样写的

public class ServerPipelineFactory implements ChannelPipelineFactory {
...
@Override
	public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = pipeline();
pipeline.addLast("executor", new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(16, 1048576, 1048576)));
pipeline.addLast("timeout", new ReadTimeoutHandler(new HashedWheelTimer(), 10));

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

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

 

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

 

public class ServerPipelineFactory implements ChannelPipelineFactory {
...
static OrderedMemoryAwareThreadPoolExecutor e = new OrderedMemoryAwareThreadPoolExecutor(16, 0, 0);
static HashedWheelTimer hashedWheelTimer = new HashedWheelTimer();
static ExecutionHandler executionHandler = new ExecutionHandler(e);
@Override
	public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = pipeline();
pipeline.addLast("executor", executionHandler );
pipeline.addLast("timeout", new ReadTimeoutHandler(hashedWheelTimer, 10));

 

 

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

 

 

 

0
0
分享到:
评论
1 楼 jiangzezhou1989 2013-03-27  
感觉这样写更好吧
private static class ServerPipelineFactory implements ChannelPipelineFactory {			
		
		private OrderedMemoryAwareThreadPoolExecutor executor;

		public ServerPipelineFactory(int poolSize) {			
			executor = new OrderedMemoryAwareThreadPoolExecutor(poolSize, 0, 0);
		}

		public ChannelPipeline getPipeline() throws Exception {
			ChannelPipeline pipeline = Channels.pipeline();
			count++;
			System.out.println("COUNT:" + count);
			pipeline.addLast("execution", new ExecutionHandler(executor));			
			return pipeline;
		}

		
	}

相关推荐

Global site tag (gtag.js) - Google Analytics