/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */
package org.jfox.ejb;

import java.util.ArrayList;
import java.util.List;

/**
 * 检测超时的 StalefulSessionBean
 *
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

class StatefulSessionLifecycle {
    // 每10分钟检查一次
    private long sleepTime = 10000L;
    private List pools = new ArrayList();

    public StatefulSessionLifecycle() {
        this(StatefulSessionObjectPool.MAX_LIFE_TIME / 6);
    }

    public StatefulSessionLifecycle(long sleepTime) {
        this.sleepTime = sleepTime;
        ManagerThread mthread = new ManagerThread("StatefulSessionBean LifecycleManager Thread");
        mthread.setDaemon(true);
        mthread.start();
    }

    public void manageIt(StatefulSessionObjectPool pool) {
        pools.add(pool);
    }

    /**
     * when undeploy, unmanage the pool
     */
    public void unmanageIt(StatefulSessionObjectPool pool) {
        pools.remove(pool);
    }

    class ManagerThread extends Thread {
        public ManagerThread(String name) {
            super(name);
//      this.setPriority(Thread.MIN_PRIORITY);
        }

        public void run() {
            while(true) {
                try {
                    sleep(sleepTime);
                }
                catch(InterruptedException e) {
                    e.printStackTrace();
                }
                for(int i = 0; i < pools.size(); i++) {
                    ((StatefulSessionObjectPool) pools.get(i)).lifecycleCheck();
                }
            }
        }
    }

    public static void main(String[] args) {

    }
}

