文档详情

Mina2.0框架源码剖析(二).docx

发布:2017-12-10约3.05千字共4页下载文档
文本预览下载声明
/phinecos/archive/2008/12/03/1347052.html上一篇介绍了几个核心的接口,这一篇主要介绍实现这些接口的抽象基类。首先是实现IoService接口的AbstractIoService类。它包含了一个Executor来处理到来的事件。每个AbstractIoService都一个AtomicInteger类型的id号,确保每个id的唯一性。它内部的Executor可以选择是从外部传递进构造函数中,也可以在实例内部自行构造,若是后者,则它将是ThreadPoolExecutor类的一个实例,即是Executor线程池中的一员。代码如下:?if?(executor?==?null)??{?this.executor?=?Executors.newCachedThreadPool();?createdExecutor?=?true;?}??else??{?this.executor?=?executor;?createdExecutor?=?false;?} 其中有一个IdleStatusChecker成员,它用来对服务的空闲状态进行检查,在一个服务激活时会将服务纳入到检查名单中,而在服务失效时会将服务从名单中剔除。会单独开一个线程进行具体的空闲检查,这是通过下面这个线程类来负责的:private?class?NotifyingTaskImpl?implements?NotifyingTask?{?private?volatile?boolean?cancelled;//取消检查标志?private?volatile?Thread?thread;?public?void?run(){?thread?=?Thread.currentThread();?try?{?while?(!cancelled)?{?//每隔1秒检查一次空闲状态?long?currentTime?=?System.currentTimeMillis();?notifyServices(currentTime);?notifySessions(currentTime);?try?{?Thread.sleep(1000);?}?catch?(InterruptedException?e)?{?//?will?exit?the?loop?if?interrupted?from?interrupt()?}?}?}?Finally?{?thread?=?null;?}?}}具体的空闲检查代码如下,超过能容忍的最大空闲时间,就会fire出SessionIdle事件,上文也说过空闲有三种类型:读端空,写端空,双端空。notifyIdleSession1(s,?currentTime,?s.getConfig().getIdleTimeInMillis(IdleStatus.BOTH_IDLE),IdleStatus.BOTH_IDLE,Math.max(s.getLastIoTime(),s.getLastIdleTime(IdleStatus.BOTH_IDLE)));?private?static?void?notifyIdleSession1(?AbstractIoSession?session,?long?currentTime,?long?idleTime,?IdleStatus?status,?long?lastIoTime)?{?if?(idleTime??0??lastIoTime?!=?0??currentTime?-?lastIoTime?=?idleTime)?{?session.getFilterChain().fireSessionIdle(status);?}}?在释放资源的方法时,首先去获取释放锁disposalLock才行,然后具体的释放动作是通过dispose0完成的,接着取消掉空闲检查线程,此外,若线程是内部创建的线程池中的一员,则通过线程池去关闭线程。?public?final?void?dispose()?{?IoFuture?disposalFuture;?synchronized?(disposalLock)?{//获取释放锁?disposalFuture?=?this.disposalFuture;?if?(!disposing)?{?disposing?=?true;?try?{?this.disposalFuture?=?disposalFuture?=?dispose0();//具体释放动作?}?catch?(Exception?e)?{?ExceptionMonitor.getInstance().exceptionCaught(e);?}?finally?{?if?(disposalFutu
显示全部
相似文档