首页 / 更多教程 / Mina2.0框架源码剖析(二)
Mina2.0框架源码剖析(二)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Mina2.0框架源码剖析(二),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4812字,纯文字阅读大概需要7分钟。
内容图文
原文链接:http://www.cnblogs.com/pampam/p/5135133.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?(disposalFuture?==?null)?{
????????????????????????disposed?=?true;
????????????????????}
????????????????}
????????????}
????????}
????????idleStatusChecker.getNotifyingTask().cancel();
????????if?(disposalFuture?!=?null)
{//无中断地等待释放动作完成
????????????disposalFuture.awaitUninterruptibly();
????????}
????????if?(createdExecutor)?
{通过线程池去关闭线程
????????????ExecutorService?e?=?(ExecutorService)?executor;
????????????e.shutdown();
????????????while?(!e.isTerminated())?{
????????????????try?{
????????????????????e.awaitTermination(Integer.MAX_VALUE,?TimeUnit.SECONDS);
????????????????}?catch?(InterruptedException?e1)?{
????????????????????//?Ignore;?it?should?end?shortly.
????????????????}
????????????}
????????}
????????disposed?=?true;
????}
再来看会话初始化完成后的动作每个session都保持有自己的属性映射图,在会话结束初始化时,应该设置这个AttributeMap。
((AbstractIoSession)?session).setAttributeMap(session.getService()????????????????????.getSessionDataStructureFactory().getAttributeMap(session));
除此以为,还应该为会话配置写请求队列:
((AbstractIoSession)?session).setWriteRequestQueue(session????????????????????.getService().getSessionDataStructureFactory()
????????????????????.getWriteRequestQueue(session));
在初始化时会在会话的属性中加入一项SESSION_CREATED_FUTURE,这个属性会在连接真正建立后从会话中去除。
????????if?(future?!=?null?&&?future?instanceof?ConnectFuture)?{
???????????session.setAttribute(DefaultIoFilterChain.SESSION_CREATED_FUTURE,
????????????????????future);
?}
用户特定的初始化动作在finishSessionInitialization0这个方法中自行实现。
作者:phinecos(洞庭散人)
出处:http://phinecos.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但请保留此段声明,并在文章页面明显位置给出原文连接。
转载于:https://www.cnblogs.com/pampam/p/5135133.html
内容总结
以上是互联网集市为您收集整理的Mina2.0框架源码剖析(二)全部内容,希望文章能够帮你解决Mina2.0框架源码剖析(二)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。