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

📄 taskproxy.java

📁 一个用于监控WEB服务器和数据库服务器的客户端程序。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				if (stopTask(ID)) {
					message = "<<"+taskName+">> 任务已经中止!";
					insertInstantInfo(true, message);
					JOptionPane.showMessageDialog(mainFrame, message);
					result = true;
				} else {
					model.setValueAt(new Boolean(true), rowNo, runColumn);
				}
			} else {
				model.setValueAt(new Boolean(true), rowNo, runColumn);
			}
		}
		return result;
	}
	
	public void runAllTask(){
		int rows = table.getRowCount();
		if (rows == 0) {
			JOptionPane.showMessageDialog(mainFrame, "任务列表中未定义任何任务!");
			return;
		}			
		int n = JOptionPane.showConfirmDialog(
                mainFrame, "即将启动任务列表中的所有未启动任务,是否继续?",
                "启动所有任务",
                JOptionPane.YES_NO_OPTION);
        if (n == JOptionPane.YES_OPTION) {
//        	 启动任务列表所有未启动的任务
			int cols = table.getColumnCount();
			int count = 0;
			String message;
			errorMsg = "";
			for(int i=0;i<rows;i++){
				Integer ID = (Integer)table.getValueAt(i, 0)-1;
				String taskName = (String)table.getValueAt(i, 1);
				boolean boo = ((Boolean)table.getValueAt(i, cols-1)).booleanValue();
				if (!boo) {
					if(runTask(ID, taskName)){
						message = "任务<<"+taskName+">>已经启动!";
						insertInstantInfo(true, message);
						errorMsg += taskName+" 任务已经启动!\r\n";
						table.setValueAt(new Boolean(true), i, cols-1);
						count++;
					}
					else{
						message = "任务<<"+taskName+">>无法启动!";
						insertInstantInfo(true, message);
						errorMsg += "无法启动任务:"+taskName+"\r\n";
						table.setValueAt(new Boolean(false), i, cols-1);
						/*if (errorMsg.indexOf("步骤") != -1) {
							editTask(i, 1);
						}
						else if (errorMsg.indexOf("调度") != -1
								|| errorMsg.indexOf("已到结束时间") != -1
								|| errorMsg.indexOf("已过执行时间") != -1) {
							editTask(i, 2);
						}*/
					}
				}
			}	
			JOptionPane.showMessageDialog(mainFrame, errorMsg);
			/*if(count < rows && count > 0) {
				JOptionPane.showMessageDialog(mainFrame, errorMsg+"本次操作启动了部分任务!");
			}
			else if(count == 0 || count == rows) {
				JOptionPane.showMessageDialog(mainFrame, errorMsg+"任务列表中的任务已经全部启动!");
			}*/
			
		}
	}
	
	public void stopAllTask(){
		int rows = table.getRowCount();
		if (rows == 0) {
			JOptionPane.showMessageDialog(mainFrame, "任务列表中未定义任何任务!");
			return;
		}
		int n = JOptionPane.showConfirmDialog(
                mainFrame, "即将中止任务列表中的所有正在运行的任务,是否继续?",
                "中止所有任务",
                JOptionPane.YES_NO_OPTION);
        if (n == JOptionPane.YES_OPTION) {
//        	 停止任务列表所有执行中的任务
			int cols = table.getColumnCount();
			int count=0;
			String message;
			for(int i=0;i<rows;i++){
				String taskName = (String)table.getValueAt(i, 1);
				boolean boo = ((Boolean)table.getValueAt(i, cols-1)).booleanValue();
				if (boo) {
					if (stopTask((Integer)table.getValueAt(i, 0)-1)) {
						message = "任务<<"+taskName+">>已经中止!";
						insertInstantInfo(true, message);
						model.setValueAt(new Boolean(false), i, cols-1);
					}
					count++;
				}
			}
			if (count > 0){
				JOptionPane.showMessageDialog(mainFrame, "任务列表中的任务已经全部中止!");
			}
			else {
				JOptionPane.showMessageDialog(mainFrame, "任务列表中没有正在执行中的任务!");
			}	
        }
	}
	
	public void insertInstantInfo(boolean isSuccess, String text) {
		JTextPane loginfo = mainFrame.getLogInfo();
		javax.swing.text.Document doc = loginfo.getDocument();
		try {
			AttributeSet atts = null;
			if (!isSuccess) {
				atts = mainFrame.getLogInfo().getStyle("Red");
			}
			doc.insertString(doc.getLength(), text+"\r\n", atts);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		loginfo.setDocument(doc);
		loginfo.setCaretPosition(doc.getLength());
	}
	
	public int getTasksSize(){
		return tasks.size();
	}
	
	private boolean runTask(Integer ID, String taskName) {
		boolean result = false;
		this.ID = ID;
		obj = taskMap.get(ID);
		ArrayList stepData = (ArrayList)((Object[])obj)[1];
		if (stepData.size() == 0) {
			errorMsg += "未定义步骤!\r\n";
			return false;
		}
		ArrayList attempData = (ArrayList)((Object[])obj)[2];
		if (attempData.size() == 0 && runType == 0) {
			errorMsg += "未定义调度!\r\n";
			return false;
		}
		if (timer == null) {
			timer = new Timer();
		}
		String[] notifyData = (String[])((Object[])obj)[3];
		managerMail = notifyData[2];
		ccMail = notifyData[3];
		task = new Task(mainFrame, stepData);
		task.setID(ID);
		task.setTaskName(taskName);
		task.setManagerMail(managerMail);
		task.setCcMail(ccMail);
		if (runType == 1 && (getSubTask(null) != null)) {
			return true;
		}
		Task[] subTasks = new Task[attempData.size()];
		ArrayList<Task[]> attempInnerTasks = new ArrayList<Task[]>();
		for(int i=0;i<attempData.size();i++){
			String[] oneAttemp = (String[]) attempData.get(i);
			setOneAttemp(oneAttemp);
			subTasks[i] = getSubTask(attempInnerTasks);
			if(subTasks[i] != null) {
				result = true;
			}
		}
		tasks.put(ID, subTasks);
		innerTasks.put(ID, attempInnerTasks);
		return result;
	}

	private Task getSubTask(ArrayList<Task[]> attempInnerTasks) {
		Task result = null;
		Date date = null;
		int execCode = 0;
		if(runType == 0) {
			execCode = Integer.parseInt(oneAttemp[2]); // 调度类型代码
		}
		else {
			execCode = 1;
		}
		task.setExecCode(execCode);
//		 立即执行任务
		if (execCode == 1) { 
			result = task.clone();
			timer.schedule(result, 0);
		}
//		 一次性运行任务
		else if (execCode == 2) { 
			date = getDate(oneAttemp[6], oneAttemp[7], oneAttemp[8]);
			if (date != null) {
				Date nowDate = new Date();
				if (date.after(nowDate)) {
					result = task.clone();
					timer.schedule(result, date);
				}
				else {
					errorMsg += "当前时间:"+Common.getDateString(nowDate)
							+" 已过执行时间:"+Common.getDateString(date)
							+",无法启动调度:"+oneAttemp[1]+"\r\n";
				}
			}
		}
//		 重复运行任务
		else if (execCode == 3) { 
			date = getDate(oneAttemp[23], oneAttemp[24], oneAttemp[25]);
			if (date !=null) {
				int endTypeCode = Integer.parseInt(oneAttemp[18]);
				if (endTypeCode == 1) {
					endDate = getDate("", oneAttemp[19], oneAttemp[20]);
				}
				else {
					int periodHour = Integer.parseInt(oneAttemp[21]);
					int periodMinute = Integer.parseInt(oneAttemp[22]);
					now.setTime(date);
					now.add(Calendar.HOUR_OF_DAY, periodHour);
					now.add(Calendar.MINUTE, periodMinute);
					endDate = now.getTime();
				}
				Date current = new Date();
				if (current.after(endDate)) {
					String currentStr = Common.getDateString(current);
					String endDateStr = Common.getDateString(endDate);
					errorMsg += "当前时间:"+currentStr+"已到结束时间:"+endDateStr+"\r\n";
					return null;
				}
				task.setEndDate(endDate);
				intervalCode = Integer.parseInt(oneAttemp[9]);
				int period = 0;
				if (intervalCode == 1) { // 每天
					period = Integer.parseInt(oneAttemp[10]);
				}
				else if (intervalCode == 2) { // 每周
					period = Integer.parseInt(oneAttemp[11]);
				}
				else if (intervalCode == 3) { // 每月
					
				}
				result = scheduleTask(date, period, attempInnerTasks);
			}
		}
		return result;
	}
	
	private Task scheduleTask(
			Date date, 
			final int period, 
			final ArrayList<Task[]> attempInnerTasks) {
		Task outerTask = null;
		int hourOrMinuteJI = Integer.parseInt(oneAttemp[16]);
		int hourOrMinute = Integer.parseInt(oneAttemp[17]);
		final int persistTime = hourOrMinute == 1 ?
					hourOrMinuteJI*60 : hourOrMinuteJI;
		if (persistTime == 0) {
			JOptionPane.showMessageDialog(mainFrame, "小时/分钟数必须大于零!");
			setRunable(false);
		} 
		else {
			now.setTime(date);
			final int theYear = now.get(Calendar.YEAR);
			final int theMonth = now.get(Calendar.MONTH);
			final int today = now.get(Calendar.DATE);
			final int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
			final Calendar c = Calendar.getInstance();
			task.setExecCode(3);
			task.setEndDate(endDate);
			if (intervalCode == 1) { // 每天
				if (period > 0) {
					final Task subTask = task.clone();
					outerTask = new Task(){
						public void run() {
							timer.schedule(subTask, 0, persistTime * 1000);
						}
					};
					timer.schedule(outerTask, date, period * 24 * 60 * 60 * 1000);
					attempInnerTasks.add(new Task[]{subTask});
				}
				else {
					JOptionPane.showMessageDialog(mainFrame, "天数必须大于零!");
					setRunable(false);
				}
			}
			else if (intervalCode == 2) { // 每周
				if (period > 0) {
					String[] days = {};
					if (oneAttemp[28] != null) {
						days = oneAttemp[28].split(",");
					}
					final String[] daysClone = days.clone();
					final int taskLen = days.length;
					outerTask = new Task() { 
						int count;
						public void run() {
							Task[] innerWeekTasksClone = new Task[taskLen];
							Date[] scheduleDatesClone = new Date[taskLen];
							for (int i = 0; i < taskLen; i++) {
								innerWeekTasksClone[i] = task.clone();
								int day = Integer.parseInt(daysClone[i]) + 1;
								int diffDay = dayOfWeek <= day ? (day - dayOfWeek)
										: (7 + day - dayOfWeek);
								c.set(Calendar.YEAR, theYear);
								c.set(Calendar.MONTH, theMonth);
								c.set(Calendar.DATE, today);
								c.add(Calendar.DATE, count * period * 7);
								c.add(Calendar.DAY_OF_WEEK, diffDay);
								scheduleDatesClone[i] = c.getTime();
								timer.schedule(
										innerWeekTasksClone[i], 
										scheduleDatesClone[i], 
										persistTime * 60 * 1000
								); 
							} 
							attempInnerTasks.add(innerWeekTasksClone);
							count++;
						} 
					};
					timer.schedule(
							outerTask, date, period * 7 * 24 * 60 * 60 * 1000);
				}
				else {
					JOptionPane.showMessageDialog(mainFrame, "周数必须大于零!");
					setRunable(false);
				}
			}
			else if (intervalCode == 3) { // 每月
				String[] months = {};
				if (oneAttemp[29] != null) {
					months = oneAttemp[29].split(",");
				}
				
				final String[] monthsClone = months.clone();
				final int taskLen = months.length;
				outerTask = new Task() { 
					int count;
					public void run() {
						Task[] innerMonthTasksClone = new Task[taskLen];
						Date[] scheduleDatesClone = new Date[taskLen];
						int hour = now.get(Calendar.HOUR_OF_DAY);
						int minute = now.get(Calendar.MINUTE);
						int second = now.get(Calendar.SECOND);
						for (int i = 0; i < taskLen; i++) {
							innerMonthTasksClone[i] = task.clone();
							int month = Integer.parseInt(monthsClone[i]);
							int diffMonth = theMonth <= month ? (month - theMonth)
									: (12 + month - theMonth);
							c.set(Calendar.YEAR, theYear);
							c.set(Calendar.MONTH, month);
							c.set(Calendar.DATE, today);
							c.set(Calendar.HOUR_OF_DAY, hour);
							c.set(Calendar.MINUTE, minute);

⌨️ 快捷键说明

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