ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-08-04 13:05:09  阅读:264  来源: 互联网

标签:session disposalFuture Mina2.0 currentTime 剖析 源码 线程 executor 空闲


原文链接: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

标签:session,disposalFuture,Mina2.0,currentTime,剖析,源码,线程,executor,空闲
来源: https://blog.csdn.net/weixin_30352191/article/details/98459259

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有