⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 poolmanager.java

📁 轻量级Http代理服务器
💻 JAVA
字号:
/*
 * jRevProxy, an opensource Java reverse proxy server
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * $Id: PoolManager.java,v 2.4 2003/03/27 15:53:55 fnoe Exp $ 
 */

package cx.noe.jrevproxy;

import java.util.Stack;
import java.util.EmptyStackException;
import cx.noe.jrevproxy.logging.ILog;
import cx.noe.jrevproxy.IHandlerInfo;

/**
 * PoolManager
 * 
 * The Poolmanager manages a set of threads that handle incoming and outgoing
 * HTTP requests.
 *
 * @author <a href="mailto:frederik@noe.cx"> Frederik Noe </a>
 * @version <tt>$Revision: 2.4 $</tt>
 */

public class PoolManager implements ILog {
    private int poolsize = 0;
    private static final int DEFAULTPOOLSIZE = 8;
    private Stack poolstack = new Stack();
    private ILog logger = null;
    
    public PoolManager(int poolsize,ILog logger) throws IllegalArgumentException {
        if(poolsize <= 0)
            throw new IllegalArgumentException("poolsize must > 0");
        
        this.poolsize = poolsize;
        this.logger = logger;
    }
    
    public PoolManager(int poolsize) throws IllegalArgumentException {
        this(poolsize, null);
    }
    
    public PoolManager(ILog logger) {
        this(DEFAULTPOOLSIZE,logger);
    }
    
    public PoolManager() {
        this(DEFAULTPOOLSIZE,null);
    }
    
    public void init() {
        log(METHOD,"--init--");
        createPool();
    }
    
    /**
     * Destroys all the handler objects in the pool
     **/
    public synchronized void destroy() {
        log(METHOD,"--destroy--");
        Handler h = null;
        int stoppedHandlers = 0;
        
        // loop till all handlers have been added to the stack
        while(stoppedHandlers < poolsize) {
            try {
                h = (Handler)poolstack.pop();
                log(DEBUG,"stopping handler " + h.id);
                h.isRunning = false;
                stoppedHandlers++;
            }
            catch(EmptyStackException e) {}
        }
        log(INFO,"all handlers have been stopped successfully");
    }
    
    public int getPoolsize() {
        log(METHOD,"--getPoolsize--");
        return poolsize;
    }
    
    /**
     * Receives the job to be done; selects a handlerobject out of the pool
     * that will handle the job
     *
     * @param thejob
     */
    public synchronized void run(RevProRunnable thejob) throws Exception {
        log(METHOD,"--run--");
        // find the next available handler
        Handler h = null;
        
        try {
            h = (Handler)poolstack.pop();
        }
        catch(EmptyStackException e) {
            log(INFO, "no handler available to do the job");
            throw new Exception("no handler available to do the job");
        }
        //handover the job
        h.handle(thejob);
        log(INFO,"job allocated successfully");
    }
    /**
     * Create a number of handlers equal to the poolsize
     * Add them to the pool of free handlers
     */
    private void createPool() {
        log(METHOD,"--createPool--");
        for(int i=0;i<poolsize;i++) {
            Handler w = new Handler(i);
            w.start();
            poolstack.push(w);
            log(DEBUG,"handler " + i + " created and pushed back");
        }
        log(INFO,"threadpool creation finished");
    }
    
    /**
     * Returns handler objects that have handled a job to the pool of free handlers
     * @param handler
     */
    private synchronized void addToPool(Handler handler) {
        log(METHOD,"--addToPool--");
        poolstack.push(handler);
        log(INFO,"handler " + handler.id + " pushed back");
    }
    
    public void log(Throwable e) {
        if(logger != null)
            logger.log(e);
    }
    
    public void log(int level, String str) {
        if(logger != null)
            logger.log(level,"[PoolManager] " + str);
    }
    
    public ILog getLogger() {
        log(METHOD,"--getLogger--");
        return logger;
    }
    
    // Inner class
    /**
     * Handlerthread that actually handles the requests
     */
    class Handler extends Thread implements ILog, IHandlerInfo {
        private int id;
        private boolean isRunning = false;
        private RevProRunnable thejob = null;
        private ILog logHandler = null;
        
        public Handler(int id) {
            this.id = id;
            isRunning = true;
            logHandler = logger;
        }
        
        public void run() {
            log(METHOD,"--run--");
            while(isRunning) {
                synchronized (this) {
                    while (isRunning && thejob == null) {
                        log(DEBUG,"waiting for a job");
                        try {
                            this.wait();
                        } catch (InterruptedException e) {}
                    }
                    log(DEBUG,"will handle a job");
                    
                    if(thejob != null) {
                        thejob.run((IHandlerInfo)this);
                        thejob = null;
                        if(isRunning == false) {
                            log(DEBUG,"going to stop the loop");
                            return;
                        }
                        addToPool(this);
                    }
                    else
                        log(DEBUG,"no job available");
                }
            }
            log(DEBUG,"do not need to loop anymore");
        }
        
        private void handle(RevProRunnable job) {
            log(METHOD,"--handle--");
            synchronized(this) {
                thejob = job;
                this.notify();
                log(DEBUG,"job added and notified");
            }
        }
        
        /////////////////// ILog //////////////////////////////////
        public void log(Throwable e) {
            if(logHandler != null)
                logHandler.log(e);
        }
        
        public void log(int level, String str) {
            if(logHandler != null)
                logHandler.log(level,"[Thread " + id + "] " +str);
            
        }
        /////////////////// IHandlerInfo //////////////////////////////////
        public int getID() {
            return id;
        }
        
        public ILog getLogger() {
            return this;
        }
    }
    
    // Inner class
    /**
     * PoolMonitor will check from time to time the size of the pool and 
     * increase or decrease it appropriately. 
     * The pool can decrease only till the default level. 
     */
    class PoolMonitor extends Thread{
        
        public void run(){
        
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -