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

📄 processhost.java

📁 请求排队系统1.1,一个简单的将请求队列后依次执行
💻 JAVA
字号:
package waitline;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.*;

/**
 * @author 李延庆
 * @version 1.1
 * @Date 2007-04-03
 */

public class ProcessHost implements Runnable {

	// the assistant to clean the finished and outdated task
	private final ProcessAssistant assistant;

	// the task host' instance ,its function Log,processTask will be invoked
	private final TaskHost taskhost;

	// the list of the waiting line
	private List waitline = Collections.synchronizedList(new LinkedList());

	// the list of the finished line
	private List finishline = Collections.synchronizedList(new LinkedList());

	// the switch to kill the thread of ProcessHost
	private boolean GoingRun = true;

	// define the logfile's path
	private String strlogfile = "";

	// Log Print Writer
	private PrintWriter logPrinter = null;

	// the time to wait for cleanning(MillSeconds)
	private int removeInterval = 15000;

	/**
	 * @author lyq
	 * 
	 */
	private ProcessHost(TaskHost taskhost, String logfile, int removeInterval,
			int cleanInterval) {

		this.taskhost = taskhost;
		this.strlogfile = logfile;
		this.removeInterval = removeInterval;
		this.createLogPrinter(logfile);
		this.assistant = ProcessAssistant.getInstance(this);
		this.assistant.setInterval(cleanInterval);

		new Thread(this.assistant).start();

	}

	/**
	 * 
	 * @param taskhost
	 * @param logfile
	 * @param removeInterval
	 *            this is a interval to remove the task from finish line when
	 *            clean the task
	 * @param cleanInterval
	 *            this is a interval to start to clean task ,the default value
	 *            is 10 Sec
	 * @return
	 */
	public static ProcessHost getInstance(TaskHost taskhost, String logfile,
			int removeInterval, int cleanInterval) {

		if (removeInterval < 0) {
			removeInterval = Const.DEFAULT_REMOVE_INTERVAL;
		}

		if (cleanInterval < 0) {
			cleanInterval = Const.DEFAULT_CLEAN_INTERVAL;
		}

		ProcessHost host = new ProcessHost(taskhost, logfile, removeInterval,
				cleanInterval);

		return host;
	}

	private void createLogPrinter(String logfilename) {
		File file = new File(logfilename);
		FileOutputStream fos;
		PrintWriter pw = null;

		try {
			fos = new FileOutputStream(file);
			pw = new PrintWriter(fos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		this.logPrinter = pw;
	}

	/*
	 * 
	 * param sessionid is the client's session id . return -1 if the task is
	 * finished . return n(n>=0) if the task is waiting for processing return
	 * Token.finished . if task have not queryed in the finished line.
	 */

	public int isFinished(String sessonid) {

		for (int i = 0; i < this.waitline.size(); i++) {
			Token token = (Token) this.waitline.get(i);
			if (sessonid.equals(token.getSessionid())) {
				return i;
			}
		}

		for (int i = 0; i < this.finishline.size(); i++) {
			Token token = (Token) this.finishline.get(i);
			if (sessonid.equals(token.getSessionid())) {
				return Token.finished;
			}
		}

		return Token.finished;
	}

	/**
	 * @author lyq
	 * 
	 */
	/*
	 * 
	 * input the task into ProcessHost's waiting line param token is the
	 * instance of token
	 * 
	 */
	public boolean inputTask(Token token) {

		this.waitline.add(token);
		return true;
	}

	/*
	 * the fuction to process task
	 * 
	 * @return true if the task is finished successful. return false if error
	 * occur or no task is waiting
	 */
	private boolean processTask() {
		// get the waiting task from waitline
		Token wait_token = this.getWaitingTask();

		if (wait_token == null)
			return false;

		// process the task at here!
		this.taskhost.processTask(wait_token);

		wait_token.setFinishedtime(new Date(System.currentTimeMillis()));
		wait_token.setState(Token.finished);
		this.finishline.add(wait_token);
		this.waitline.remove(wait_token);

		log(wait_token);
		return true;
	}

	// log
	private void log(Token wait_token) {
		this.taskhost.log(this.logPrinter, wait_token);
	}

	/*
	 * when the task is finished .put the task into finishline and remove it
	 * from waiting line
	 */

	/**
	 * @author lyq
	 * 
	 */

	/*
	 * get the waiting task from waitline.return the instance of Token
	 */
	private Token getWaitingTask() {
		Token token = null;
		for (int i = 0; i < waitline.size(); i++) {
			token = (Token) waitline.get(i);
			if (token != null && token.getState() == Token.waiting) {
				token.setState(Token.processing);
				return token;
			}
		}

		return null;
	}

	/*
	 * the content of the thread
	 */
	public void run() {
		while (GoingRun) {

			try {
				Thread.sleep(1500);
				this.processTask();

			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		if (!GoingRun) {
			this.assistant.setGoingRun(false);
		}
	}

	/*
	 * the fuction to create n thread to process task @param threadnum is the
	 * number of the threads of ProcessTask
	 */
	public void createThreads(int threadnum) {

		for (int i = 0; i < threadnum; i++) {
			Thread t = new Thread(this);
			t.start();
		}
	}

	/*
	 * query the state of threads of ProcessHost
	 */
	public boolean isGoingRun() {
		return GoingRun;
	}

	/*
	 * set the key of thread f ProcessHost
	 */
	public void setGoingRun(boolean goingRun) {
		GoingRun = goingRun;
	}

	/*
	 * clean the task that is finished and not requested in the finishline
	 */
	protected void cleanTask() {
		Token token = null;
		long now = System.currentTimeMillis();

		for (int i = 0; i < this.finishline.size(); i++) {
			token = (Token) this.finishline.get(i);
			if (token == null) {
				this.finishline.remove(token);
				continue;
			}
			long interval = now - token.getLastmodify().getTime();
			if (interval > this.removeInterval) {
				this.finishline.remove(token);
			}
		}

	}

	/*
	 * when the task is requested again,the task'last_modify_time would be
	 * modify
	 * 
	 */
	public boolean refreshTask(String sessionid) {
		Token token = null;

		for (int i = 0; i < this.waitline.size(); i++) {
			token = (Token) this.waitline.get(i);
			if (sessionid.equals(token.getSessionid())) {
				token.setLastmodify(new Date(System.currentTimeMillis()));
				return true;
			}

		}

		return false;
	}

	/*
	 * @return the total number of task what is waiting.
	 */
	public int getTotalWaitingNumber() {
		int totalnum;

		totalnum = waitline.size();

		return totalnum;
	}
}

⌨️ 快捷键说明

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