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

📄 taskhost.java

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

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

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import java.util.*;

import waitline.Token;

public abstract class TaskHost extends HttpServlet {
	private String CLASSNAME_PROCESSHOST = Const.APPLICATION_PROCESSHOST;

	private int REMOVE_INTERVAL = Const.REMOVE_INTERVAL;

	private int CLEAN_INTERVAL = Const.CLEAN_INTERVAL;

	private String POS_LOGFILE = Const.POS_LOGFILE;

	private static final String PUT_TASK_TRUE = Const.PUT_TASK_TRUE;

	private static final String PUT_TASK_FALSE = Const.PUT_TASK_FALSE;

	private String APPLICATION_PROCESS_ASSISTANT = Const.APPLICATION_PROCESS_ASSISTANT;

	private int PROCESSHOST_THREAD_NUM = Const.PROCESSHOST_THREAD_NUM;

	private String APPLICATION_PROCESSHOST = Const.APPLICATION_PROCESSHOST;

	// the flag to verify user whether have put their task into the waiting
	// line.
	private String flag = Const.FLAG_PUT_TASK;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ProcessHost ph = (ProcessHost) this.getServletContext().getAttribute(
				APPLICATION_PROCESSHOST);
		HttpSession session = request.getSession(true);
		String havePutintoLine = (String) session.getAttribute(flag);

		// if the user's task have not put into waiting line then add task to
		// waiting line.
		if (havePutintoLine == null || !havePutintoLine.equals(PUT_TASK_TRUE)) {
			// the first time post the data to server
			Token token = new Token(request.getSession().getId(), null,
					new Date(System.currentTimeMillis()), new Date(System
							.currentTimeMillis()), new Date(System
							.currentTimeMillis()));

			// add task bean at here!!
			Object obj = createTask(request, response);
			token.setTaskObject(obj);

			// put task bean into waiting line.
			ph.inputTask(token);

			session.setAttribute(flag, PUT_TASK_TRUE);

			verifyTaskState(request, response, ph, session);

		} else {
			verifyTaskState(request, response, ph, session);
		}
	}

	private void verifyTaskState(HttpServletRequest request,
			HttpServletResponse response, ProcessHost ph, HttpSession session)
			throws ServletException, IOException {
		String sessionid = session.getId();
		int waiting_num = ph.isFinished(sessionid);

		if (waiting_num == Token.finished) {
			// when the task have been done with.
			session.setAttribute(flag, PUT_TASK_FALSE);
			processWhenFinish(request, response);
		} else {
			// when the task alse is in the waiting line.
			ph.refreshTask(session.getId());
			processWhenProcessing(request, response, waiting_num, ph
					.getTotalWaitingNumber());
		}
	}

	/**
	 * this fuction to be inherit by subclass of Waiter
	 * 
	 * @param request
	 * @param response
	 * 
	 */
	public abstract void processWhenFinish(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException;

	/**
	 * this fuction to be inherit by subclass of Waiter
	 * 
	 * @param request
	 * @param response
	 * 
	 */
	public abstract void processWhenProcessing(HttpServletRequest request,
			HttpServletResponse response, int waiting_num, int total_num)
			throws ServletException, IOException;

	/**
	 * this fuction to be inherit by subclass of Waiter
	 * 
	 * @param request
	 * @param response
	 * @return a instance of Task Bean
	 */
	public abstract Object createTask(HttpServletRequest request,
			HttpServletResponse response);

	/**
	 * this function will be invoked by ProcessHost. There is a pity at here
	 * because of the user's convenience at the original design,it was running
	 * in class ProcessHost and using the reflect tech to create a new instance
	 * of class ProcessHost.hehe.my name is liyanqing.
	 * 
	 * @param obj
	 *            is the Task Bean
	 */
	public abstract void processTask(Token token);

	public abstract void log(PrintWriter pw, Token wait_token);

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occure
	 */
	final public void init() throws ServletException {
		configParameters();
		ProcessHost ph = ProcessHost.getInstance(this, this.POS_LOGFILE,
				this.REMOVE_INTERVAL, this.CLEAN_INTERVAL);

		ph.createThreads(PROCESSHOST_THREAD_NUM);

		this.getServletContext().setAttribute(APPLICATION_PROCESSHOST, ph);

	}

	/**
	 * config the parameter of Servlet
	 * 
	 */
	private void configParameters() {
		String pos_processhost = this.getInitParameter("POSITION_PROCESSHOST");
		String pos_flag = this.getInitParameter("POSITION_FLAG_TASK");
		String threadnum_processhost = this
				.getInitParameter("THREAD_NUM_PROCESSHOST");
		String strLogfile = this.getInitParameter("POS_LOGFILE");
		String removeInterval = this.getInitParameter("REMOVE_INTERVAL");
		String cleanInterval = this.getInitParameter("CLEAN_INTERVAL");

		// config the Request's attrbute that contain class ProcessHost
		if (pos_processhost != null) {
			this.APPLICATION_PROCESSHOST = pos_processhost;
		}
		// config the Request's attrbute that verify whether have put task into
		// waitingline
		if (pos_flag != null) {
			this.flag = pos_flag;
		}
		// config the num of threads of Processhost
		if (threadnum_processhost != null) {
			int threadnum = this.PROCESSHOST_THREAD_NUM;
			try {
				threadnum = Integer.parseInt(threadnum_processhost);
			} catch (Exception e) {
				e.getStackTrace();
			}
			this.PROCESSHOST_THREAD_NUM = threadnum;
		}

		// config the log file's position ,the default path is
		// ${webapp}/TaskLog/default.txt
		if (strLogfile != null) {
			File logFile = new File(strLogfile);
			if (!logFile.exists()) {
				File path = logFile.getParentFile();
				try {
					if (!path.exists())
						path.mkdirs();
					logFile.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
					strLogfile = "";
				}

			}
			this.POS_LOGFILE = strLogfile;
		}
		if (strLogfile == null || strLogfile.equals("")) {
			strLogfile = this.getServletContext().getRealPath("/");
			strLogfile = strLogfile + this.POS_LOGFILE;
			File defFile = new File(strLogfile);
			File path = defFile.getParentFile();
			if (!path.exists())
				path.mkdirs();
			if (!defFile.exists())
				try {
					defFile.createNewFile();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			this.POS_LOGFILE = strLogfile;
		}

		// config the interval to remove task in finished line.
		if (removeInterval != null) {
			int remove_int = this.REMOVE_INTERVAL;
			try {
				remove_int = Integer.parseInt(removeInterval);
			} catch (Exception e) {
				e.getStackTrace();
			}
			if (remove_int > 0) {
				this.REMOVE_INTERVAL = remove_int;
			}

		}

		// config the interval to start cleanning finished Task
		if (cleanInterval != null) {
			int clean_int = this.CLEAN_INTERVAL;
			try {
				clean_int = Integer.parseInt(cleanInterval);
			} catch (Exception e) {
				e.getStackTrace();
			}
			if (clean_int > 0) {

				this.CLEAN_INTERVAL = clean_int;
			}
		}
	}

	final public void destroy() {
		super.destroy();
		ProcessHost ph = (ProcessHost) this.getServletContext().getAttribute(
				APPLICATION_PROCESSHOST);

		if (ph != null) {
			ph.setGoingRun(false);
		}

	}

}

⌨️ 快捷键说明

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