Jelajahi Sumber

1.优化程序。
2.解决程序卡死的问题。

YangZhiJie 7 tahun lalu
induk
melakukan
a60f8dc6d3

+ 1 - 0
.hgignore

@@ -4,3 +4,4 @@ target/*
 *.log
 stop.bat
 start.bat
+logs/server-management.log.1

+ 4 - 1
config/applicationContext.xml

@@ -26,7 +26,7 @@
 		<property name="settings">
 			<list>
 <!-- 				<ref bean="applicationSetting1" /> -->
-<!-- 				<ref bean="applicationSetting2" /> -->
+				<ref bean="applicationSetting2" />
 <!-- 				<ref bean="applicationSetting3" /> -->
 <!-- 				<ref bean="applicationSetting4" /> -->
 <!-- 				<ref bean="applicationSetting5" /> -->
@@ -69,6 +69,7 @@
 		<property name="name" value="文件服务器"></property>
 		<property name="installPath" value="D:\ProdogServers\FileServer"></property>
 		<property name="startCommand" value="javaw -jar D:\ProdogServers\FileServer\FileServer-0.0.1-SNAPSHOT.jar"></property>
+		<property name="startBatFile" value="start.bat"></property>
 		<property name="stopBatFile" value="stop.bat"></property>
 		<property name="monitorUrl" value="http://127.0.0.1:85/api/monitor"></property>
 		<property name="token" value="123456"></property>
@@ -78,6 +79,7 @@
 		<property name="name" value="反向代理服务器"></property>
 		<property name="installPath" value="D:\ProdogServers\nginx-1.13.8"></property>
 		<property name="startCommand" value="D:\ProdogServers\nginx-1.13.8\nginx.exe"></property>
+		<property name="startBatFile" value="start.bat"></property>
 		<property name="stopBatFile" value="stop.bat"></property>
 		<property name="monitorUrl" value="http://127.0.0.1:85/api/monitor"></property>
 		<property name="token" value="123456"></property>
@@ -146,6 +148,7 @@
 		<property name="name" value="排产服务器"></property>
 		<property name="installPath" value="D:\ProdogServers\ApsServer"></property>
 		<property name="startCommand" value="javaw -jar D:\ProdogServers\ApsServer\ProdogAssign-0.0.1.jar"></property>
+		<property name="startBatFile" value="start.bat"></property>
 		<property name="stopBatFile" value="stop.bat"></property>
 		<property name="monitorUrl" value="http://127.0.0.1:85/api/monitor"></property>
 		<property name="token" value="123456"></property>

+ 1 - 0
pom.xml

@@ -78,6 +78,7 @@
 			<artifactId>dom4j</artifactId>
 			<version>2.0.0-RC1</version>
 		</dependency>
+		
 	</dependencies>
 	
 	

+ 48 - 2
src/main/java/com/leanwo/management/App.java

@@ -1,5 +1,13 @@
 package com.leanwo.management;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import javax.swing.JOptionPane;
+import javax.swing.SwingUtilities;
+import javax.swing.UIManager;
+import javax.swing.UIManager.LookAndFeelInfo;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.springframework.context.ApplicationContext;
@@ -22,6 +30,8 @@ public class App {
 		ProgramAutoStartUtil.generateStartBat();
 		ProgramStopUtil.generateStopBat();
 		
+		ApplicationContext factory = new FileSystemXmlApplicationContext("config/applicationContext.xml");
+		
 		boolean isAutoRun = false;
 		if(args != null && args.length > 0) {
 			if(args[0].equals("autoRun")) {
@@ -36,7 +46,43 @@ public class App {
 			logger.info("程序管理器将会自动运行。");
 		}
 		
-		ApplicationContext factory = new FileSystemXmlApplicationContext("config/applicationContext.xml");
-		MainFrame serverManagmentJFrame = new MainFrame(isAutoRun);
+		final boolean isAutoRunFinal = isAutoRun;
+		
+		SwingUtilities.invokeLater(new Runnable() {
+            @Override
+            public void run() {
+//                installLookAndFeel();
+                try {
+                    final MainFrame mainFrame = new MainFrame(isAutoRunFinal);
+                    mainFrame.pack();
+                    mainFrame.setVisible(true);
+                } catch (final Exception ex) {
+                    ex.printStackTrace();
+
+                    final StringWriter sr = new StringWriter();
+                    ex.printStackTrace(new PrintWriter(sr));
+                    JOptionPane.showMessageDialog(null, sr.toString(), "Error", JOptionPane.ERROR_MESSAGE);
+                }
+            }
+        });
 	}
+	
+
+    private static void installLookAndFeel() {
+        try {
+            for (final LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
+                if ("Nimbus".equals(info.getName())) {
+                    UIManager.setLookAndFeel(info.getClassName());
+                    return;
+                }
+            }
+        } catch (final Exception ex) {
+            ex.printStackTrace();
+        }
+        try {
+            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
+        } catch (final Exception e) {
+            e.printStackTrace();
+        }
+    }
 }

+ 22 - 36
src/main/java/com/leanwo/management/MainFrame.java

@@ -14,7 +14,6 @@ import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
-import java.io.File;
 import java.io.IOException;
 import java.net.URL;
 import java.util.List;
@@ -24,15 +23,9 @@ import java.util.TimerTask;
 import javax.imageio.ImageIO;
 import javax.swing.JButton;
 import javax.swing.JFrame;
-import javax.swing.JMenu;
-import javax.swing.JMenuBar;
-import javax.swing.JMenu;
 import javax.swing.JOptionPane;
 import javax.swing.JToolBar;
-import javax.swing.event.MenuEvent;
-import javax.swing.event.MenuListener;
 
-import org.apache.commons.exec.ExecuteWatchdog;
 import org.apache.log4j.Logger;
 
 import com.leanwo.management.model.ApplicationSetting;
@@ -41,7 +34,6 @@ import com.leanwo.management.service.AutoUpdateService;
 import com.leanwo.management.service.ProcessService;
 import com.leanwo.management.service.ProgramRunResult;
 import com.leanwo.management.util.SpringUtil;
-import com.leanwo.management.widget.DefaultMenuListener;
 import com.leanwo.management.widget.SelectEventListener;
 import com.leanwo.management.widget.SelectObject;
 
@@ -93,6 +85,7 @@ public class MainFrame extends JFrame{
 		settings = applicationSettingCache.getSettings();
 		processService = (ProcessService) SpringUtil.getSingleBean(ProcessService.class);
 		autoUpdateService = (AutoUpdateService) SpringUtil.getSingleBean(AutoUpdateService.class);
+		Thread.currentThread().setName("界面主线程");
 	}
 	
 	private void initView() {
@@ -137,7 +130,6 @@ public class MainFrame extends JFrame{
 		serverLogFrame = new ServerLogFrame(settings);
         add(serverLogFrame, BorderLayout.CENTER);
         
-
 		this.setTitle("Prodog程序管理器");
 		// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设定按关闭时的操作,这里是关闭窗口,如果不设定,就什么也不会发生
 		
@@ -156,8 +148,6 @@ public class MainFrame extends JFrame{
         
 		// 设置窗体位置和大小
 		this.setBounds((width - windowsWidth) / 2, (height - windowsHeight) / 2, windowsWidth, windowsHeight);
-		
-        this.setVisible(true);
         
         if(isAutoStart) {
         	processService.autoStart(settings, serverLogFrame);
@@ -181,7 +171,7 @@ public class MainFrame extends JFrame{
 					
 					try {
 						ProgramRunResult programRunResult = processService.startProgram(setting);
-						if(programRunResult == null || programRunResult.getWatchDog() == null) {
+						if(programRunResult == null || programRunResult.getProcess() == null) {
 							JOptionPane.showMessageDialog(null, "未配置启动文件或启动命令", "配置异常", JOptionPane.ERROR_MESSAGE);
 						}else {
 							setting.setProgramRunResult(programRunResult);
@@ -205,11 +195,11 @@ public class MainFrame extends JFrame{
 					ApplicationSetting setting = settings.get(selectedRow);
 					try {
 						ProgramRunResult programRunResult = setting.getProgramRunResult();
-						if(programRunResult != null && programRunResult.getWatchDog() != null) {
-							programRunResult.getWatchDog().destroyProcess();
+						if(programRunResult != null && programRunResult.getProcess() != null) {
+							programRunResult.getProcess().destroyForcibly();
 						}
 						programRunResult = processService.stopProgram(setting);
-						if(programRunResult == null || programRunResult.getWatchDog() == null) {
+						if(programRunResult == null || programRunResult.getProcess() == null) {
 							JOptionPane.showMessageDialog(null, setting.getName() +"未配置停止文件", "程序关闭异常", JOptionPane.ERROR_MESSAGE);
 						}else {
 							setting.setProgramRunResult(null);
@@ -292,11 +282,11 @@ public class MainFrame extends JFrame{
 						final ApplicationSetting setting = settings.get(i);
 						try {
 							ProgramRunResult programRunResult = setting.getProgramRunResult();
-							if(programRunResult != null && programRunResult.getWatchDog() != null) {
-								programRunResult.getWatchDog().destroyProcess();
+							if(programRunResult != null && programRunResult.getProcess() != null) {
+								programRunResult.getProcess().destroyForcibly();
 							}
 							programRunResult = processService.stopProgram(setting);
-							if(programRunResult == null || programRunResult.getWatchDog() == null) {
+							if(programRunResult == null || programRunResult.getProcess() == null) {
 								logger.error(setting.getName() + "程序关闭异常,未配置停止文件。");
 							}else {
 								setting.setProgramRunResult(null);
@@ -336,33 +326,29 @@ public class MainFrame extends JFrame{
     protected void processWindowEvent(WindowEvent e) {  
         if (e.getID() == WindowEvent.WINDOW_CLOSING) {
         	//setVisible(false);
-        	boolean result = false;
         	// 关闭其他子线程
         	for(int i = 0; i < settings.size(); i ++) {
         		ApplicationSetting setting = settings.get(i);
         		ProgramRunResult programRunResult = setting.getProgramRunResult();
-    			if(programRunResult != null && programRunResult.getWatchDog() != null) {
-    				result = programRunResult.getWatchDog().killedProcess();
+    			if(programRunResult != null && programRunResult.getProcess() != null) {
+    				programRunResult.getProcess().destroyForcibly();
     			}
     			
-    			if(result == false) {
-    				try {
-						programRunResult = processService.stopProgram(setting);
-						if(programRunResult == null || programRunResult.getWatchDog() == null) {
-	    					logger.error("关闭程序失败" + setting.getName() +"未配置停止文件。");
-	    					JOptionPane.showMessageDialog(null, "未配置停止文件", "配置异常", JOptionPane.ERROR_MESSAGE);
-	    				}else {
-	    					setting.setProgramRunResult(null);
-	    				}
-					} catch (IOException e1) {
-    					logger.error("关闭程序失败" + setting.getName(), e1);
-					}
-    			}else {
+				try {
+					programRunResult = processService.stopProgram(setting);
+					if(programRunResult == null || programRunResult.getProcess() == null) {
+    					logger.error("关闭程序失败" + setting.getName() +"未配置停止文件。");
+    					JOptionPane.showMessageDialog(null, "未配置停止文件", "配置异常", JOptionPane.ERROR_MESSAGE);
+    				}else {
+    					setting.setProgramRunResult(null);
+    				}
+				} catch (IOException e1) {
+					logger.error("关闭程序失败" + setting.getName(), e1);
     				setting.setProgramRunResult(null);
-    			}	
+				}
         	}
         	
-        	Timer timer = new Timer();
+        	Timer timer = new Timer("定时器:延时关闭");
         	this.setEnabled(false);
         	timer.schedule(new TimerTask() {
 				@Override

+ 2 - 50
src/main/java/com/leanwo/management/ServerFrame.java

@@ -1,43 +1,12 @@
 package com.leanwo.management;
 
-import java.awt.AWTEvent;
-import java.awt.AWTException;
 import java.awt.BorderLayout;
-import java.awt.Color;
-import java.awt.GridBagConstraints;
-import java.awt.GridBagLayout;
-import java.awt.Image;
-import java.awt.Insets;
-import java.awt.Label;
-import java.awt.SystemTray;
-import java.awt.Toolkit;
-import java.awt.TrayIcon;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.awt.event.MouseAdapter;
-import java.awt.event.MouseEvent;
-import java.awt.event.WindowAdapter;
-import java.awt.event.WindowEvent;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.IOException;
-import java.net.URL;
-import java.util.Date;
-import java.util.EventListener;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
 import java.util.Timer;
 import java.util.TimerTask;
 
-import javax.imageio.ImageIO;
-import javax.swing.BorderFactory;
-import javax.swing.JButton;
-import javax.swing.JFrame;
-import javax.swing.JLabel;
-import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTable;
@@ -46,27 +15,11 @@ import javax.swing.SwingUtilities;
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;
 import javax.swing.table.DefaultTableModel;
-import javax.swing.table.TableModel;
 
-import org.apache.commons.exec.CommandLine;
-import org.apache.commons.exec.DefaultExecuteResultHandler;
-import org.apache.commons.exec.DefaultExecutor;
-import org.apache.commons.exec.ExecuteWatchdog;
-import org.apache.commons.exec.PumpStreamHandler;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
-import org.springframework.http.client.SimpleClientHttpRequestFactory;
-import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
-import org.springframework.web.client.RestTemplate;
 
 import com.leanwo.management.model.ApplicationSetting;
-import com.leanwo.management.model.ApplicationSettingCache;
-import com.leanwo.management.service.AutoUpdateService;
 import com.leanwo.management.service.ProcessService;
 import com.leanwo.management.util.SpringUtil;
 import com.leanwo.management.util.Variable;
@@ -135,7 +88,7 @@ public class ServerFrame extends JPanel {
 		table = new JTable(tableModel);
 		table.getColumn("序号").setPreferredWidth(50);
 		table.getColumn("程序").setPreferredWidth(150);
-		table.getColumn("状态").setPreferredWidth(96);
+		table.getColumn("状态").setPreferredWidth(90);
 
 		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 		table.setRowSelectionAllowed(false);
@@ -157,11 +110,10 @@ public class ServerFrame extends JPanel {
 		ColorCellRenderer rcr = new ColorCellRenderer();
 		table.setDefaultRenderer(Object.class, rcr);
 		
-		
 		JScrollPane scroll = new JScrollPane(table);
 		add(scroll);
 
-		timer = new Timer();
+		timer = new Timer("定时器:定时更新服务器状态");
 		timer.schedule(timerTask, 2000L, 2000L);
 	}
 

+ 4 - 11
src/main/java/com/leanwo/management/ServerLogFrame.java

@@ -1,7 +1,6 @@
 package com.leanwo.management;
 
 import java.awt.GridLayout;
-import java.io.PipedInputStream;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -10,16 +9,13 @@ import java.util.TimerTask;
 
 import javax.swing.JPanel;
 import javax.swing.JTabbedPane;
-import javax.swing.JTextArea;
 import javax.swing.SwingUtilities;
 
 import org.apache.log4j.Logger;
 
 import com.leanwo.management.console.LogConsole;
 import com.leanwo.management.model.ApplicationSetting;
-import com.leanwo.management.model.ApplicationSettingCache;
 import com.leanwo.management.service.ProgramRunResult;
-import com.leanwo.management.util.SpringUtil;
 
 /**
  * 服务器日志视图
@@ -63,10 +59,7 @@ public class ServerLogFrame extends JPanel{
 			JPanel panel = new JPanel();
 			tabbedpane.addTab(tabNames[i],  panel);
 			
-			PipedInputStream outPipedInputStream=new PipedInputStream(); 
-			PipedInputStream errorPipedInputStream=new PipedInputStream(); 
-			
-			LogConsole logConsole = new LogConsole(outPipedInputStream, errorPipedInputStream);
+			LogConsole logConsole = new LogConsole(tabNames[i]);
 			panel.setLayout(new GridLayout(1, 1));
 			panel.add(logConsole);
 			logConsoleMap.put(i, logConsole);
@@ -75,8 +68,8 @@ public class ServerLogFrame extends JPanel{
 		setLayout(new GridLayout(1, 1));
 		add(tabbedpane);
 		
-		timer = new Timer();
-		timer.schedule(timerTask, 10000L, 10000L);
+		timer = new Timer("定时器:保持控制台文字数量");
+		timer.schedule(timerTask, 1000L, 1000L);
 	}
 	
 	public void setSelectedIndex(int index) {
@@ -91,7 +84,7 @@ public class ServerLogFrame extends JPanel{
 	public void updateProgramRunResult(int index, ProgramRunResult programRunResult) {
 		if(logConsoleMap.containsKey(index)) {
 			LogConsole logConsole = logConsoleMap.get(index);
-			logConsole.setPipedInputStream(programRunResult.getOutPipedInputStream(), programRunResult.getErrorPipedInputStream());
+			logConsole.setProcess(programRunResult.getProcess());
 		}else {
 			logger.error("找不到序号" + index + "对应的视图。");
 		}

+ 0 - 20
src/main/java/com/leanwo/management/SettingEditFrame.java

@@ -1,39 +1,19 @@
 package com.leanwo.management;
 
-import java.awt.AWTEvent;
-import java.awt.AWTException;
-import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.Container;
-import java.awt.GridBagConstraints;
-import java.awt.GridBagLayout;
-import java.awt.Image;
-import java.awt.Insets;
-import java.awt.SystemTray;
-import java.awt.TrayIcon;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
-import java.awt.event.MouseAdapter;
-import java.awt.event.MouseEvent;
-import java.awt.event.WindowAdapter;
-import java.awt.event.WindowEvent;
-import java.io.IOException;
-import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
 
-
-import javax.imageio.ImageIO;
-import javax.swing.BorderFactory;
 import javax.swing.BoxLayout;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;
-import javax.swing.JPanel;
 import javax.swing.JTextArea;
 import javax.swing.JTextField;
-import javax.swing.Timer;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;

+ 64 - 89
src/main/java/com/leanwo/management/console/LogConsole.java

@@ -3,9 +3,7 @@ package com.leanwo.management.console;
 import java.awt.BorderLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PipedInputStream;
+import java.util.Date;
 
 import javax.swing.JButton;
 import javax.swing.JCheckBox;
@@ -16,53 +14,77 @@ import javax.swing.SwingUtilities;
 
 public class LogConsole extends JPanel{
 
+	private String name;
+	
 	private JTextArea textArea;
 	private JCheckBox autoScrollCheckBox;
 	private JButton clearButton;
 	
-	private PipedInputStream outPipedInputStream;
-	private PipedInputStream errorPipedInputStream;
-
-	private boolean quit;
-	private Thread outReaderThread;
-	private Thread errorReaderThread;
+	private Process process;
+	
+	private StreamOutputThread outReaderThread;
+	private StreamOutputThread errorReaderThread;
 	
 	/** 字符串最大的个数 */
-	private static final int MAX_CHARACTER_COUNT = 10000;
+	private static final int MAX_CHARACTER_COUNT = 100000;
 	
+	private StringBuilder cacheStringBuilder = null;
+	private long currentMills = 0L;
+	private long lastUpdateMills = 0L;
 	/** 自动滚动 */
 	private boolean autoScroll;
 	
-	public LogConsole(PipedInputStream outPipedInputStream, PipedInputStream errorPipedInputStream) {
-		this.outPipedInputStream = outPipedInputStream;
-		this.errorPipedInputStream = errorPipedInputStream;
+	public LogConsole(String name) {
+		this.name = name;		
 		initData();
 		initView();
 	}
 	
-	public void 	setPipedInputStream(PipedInputStream outPipedInputStream, PipedInputStream errorPipedInputStream) {
-		this.outPipedInputStream = outPipedInputStream;
-		this.errorPipedInputStream = errorPipedInputStream;
+	public void 	setProcess(Process process) {
+		if(outReaderThread != null) {
+			outReaderThread.stopThread();
+			outReaderThread = null;
+		}
+		
+		if(errorReaderThread != null) {
+			errorReaderThread.stopThread();
+			errorReaderThread = null;
+		}
+		
+		this.process = process;
+		initData();
 	}
 	
 	/**
 	 * 初始化数据
 	 */
 	private void initData() {
-		// signals the Threads that they should exit
-		quit=false; 
-		
 		// 自动滚动
 		autoScroll = true;
 		
-		// Starting two seperate threads to read from the PipedInputStreams				
-		outReaderThread=new Thread(outRunnable);
-		outReaderThread.setDaemon(true);	
-		outReaderThread.start();	
+		cacheStringBuilder = new StringBuilder();
 		
-		errorReaderThread=new Thread(errorRunnable);	
-		errorReaderThread.setDaemon(true);	
-		errorReaderThread.start();
+		if(process != null) {
+			outReaderThread=new StreamOutputThread(process.getInputStream(), new LogEvent() {
+				@Override
+				public void appendLog(String data) {
+					LogConsole.this.appendLog(data);
+				}
+			} );
+			outReaderThread.setDaemon(true);	
+			outReaderThread.setName(name = "正常消息输出线程。");
+			outReaderThread.start();	
+			
+			errorReaderThread=new StreamOutputThread(process.getErrorStream(), new LogEvent() {
+				@Override
+				public void appendLog(String data) {
+					LogConsole.this.appendLog(data);
+				}
+			});	
+			errorReaderThread.setDaemon(true);	
+			errorReaderThread.setName(name = "异常消息输出线程。");
+			errorReaderThread.start();
+		}
 	}
 	
 	/**
@@ -103,61 +125,6 @@ public class LogConsole extends JPanel{
 		});
 	}
 
-	private Runnable outRunnable = new Runnable() {
-		@Override
-		public synchronized void run() {
-			try {
-				while (!quit) {
-					try {
-						this.wait(10);
-					} catch (InterruptedException ie) {
-					}
-					if (outPipedInputStream != null && outPipedInputStream.available() != 0) {
-						String input = readLine(outPipedInputStream);
-						appendLog(input);						
-					}
-				}
-			} catch (Exception e) {
-				String text = "\r\nConsole reports an Internal error.\r\nThe error is:" + e.getMessage();
-				appendLog(text);
-			}
-		}
-	};
-	
-	
-	private Runnable errorRunnable = new Runnable() {
-		@Override
-		public synchronized void run() {
-			try {
-				while (!quit) {
-					try {
-						this.wait(10);
-					} catch (InterruptedException ie) {
-					}
-					if (errorPipedInputStream != null && errorPipedInputStream.available() != 0) {
-						String input = readLine(errorPipedInputStream);
-						appendLog(input);	
-					}
-				}
-			} catch (Exception e) {
-				String text = "\r\nConsole reports an Internal error.\r\nThe error is:" + e.getMessage();
-				appendLog(text);
-			}
-		}
-	};
-
-	private String readLine(InputStream inputStream) throws IOException {
-		String input = "";
-		do {
-			int available = inputStream.available();
-			if (available == 0)
-				break;
-			byte b[] = new byte[available];
-			inputStream.read(b);
-			input = input + new String(b, 0, b.length, "GBK");
-		} while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
-		return input;
-	}
 	
 	/**
 	 * 保留最多的字符串
@@ -168,7 +135,7 @@ public class LogConsole extends JPanel{
 			text = text.substring(text.length() - MAX_CHARACTER_COUNT *2/3);
 			textArea.setText(text);
 		}
-		textArea.setCaretPosition(textArea.getText().length());
+		appendLog(null);
 	}
 	
 	/**
@@ -176,13 +143,21 @@ public class LogConsole extends JPanel{
 	 * @param text
 	 */
 	private void appendLog(String text) {
-		 SwingUtilities.invokeLater(new Runnable() {
-             @Override
-             public void run() {
-        	 	textArea.append(text);
-        	 	scrollToEnd();	
-             }
-         });	
+		if(text != null) {
+			cacheStringBuilder.append(text).append("\r\n");
+		}
+		currentMills = (new Date()).getTime();
+		 if((currentMills - lastUpdateMills > 500)) {
+			 SwingUtilities.invokeLater(new Runnable() {
+	             @Override
+	             public void run() {
+	        	 	textArea.append(cacheStringBuilder.toString());
+	        	 	cacheStringBuilder.setLength(0);
+	        	 	lastUpdateMills = currentMills;
+	        	 	scrollToEnd();	
+	             }
+	         });	
+		 }		 
 	}
 	
 	/**

+ 10 - 0
src/main/java/com/leanwo/management/console/LogEvent.java

@@ -0,0 +1,10 @@
+package com.leanwo.management.console;
+
+/**
+ * 日志事件
+ * @author YangZhiJie1
+ *
+ */
+public interface LogEvent {
+	public void appendLog(String data);
+}

+ 58 - 0
src/main/java/com/leanwo/management/console/StreamOutputThread.java

@@ -0,0 +1,58 @@
+package com.leanwo.management.console;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.log4j.Logger;
+
+import com.leanwo.management.util.CloseableUtil;
+
+public class StreamOutputThread extends Thread {
+	private static Logger logger = Logger.getLogger(StreamOutputThread.class);
+	
+	private LogEvent logEvent;
+	
+	private InputStream inputStream;
+	private InputStreamReader inputStreamReader;
+	private BufferedReader bufferedReader;
+	
+	/** 停止标识符 */
+	private boolean stopFlag;
+	
+	public StreamOutputThread(InputStream inputStream, LogEvent logEvent) {
+		this.logEvent = logEvent;
+		this.inputStream = inputStream;
+		this.stopFlag = false;
+	}
+	
+	@Override
+	public synchronized void run() {
+		try {
+			this.inputStreamReader = new InputStreamReader(this.inputStream, "gbk");
+			this.bufferedReader = new BufferedReader(this.inputStreamReader);	
+		} catch (UnsupportedEncodingException e1) {
+			e1.printStackTrace();
+		}
+		
+		String output = null;  
+		try {
+			while (!stopFlag) {
+				output = bufferedReader.readLine();
+				if(output != null && logEvent != null) {
+					logEvent.appendLog(output);
+				}
+			}
+		} catch (Exception e) {
+			logger.error("流数据读取失败", e);
+		} finally {
+			CloseableUtil.close(inputStream, inputStreamReader, bufferedReader);
+		}
+	}
+	
+	public void stopThread() {
+		stopFlag = true;
+		CloseableUtil.close(inputStream, inputStreamReader, bufferedReader);
+	}
+}

+ 6 - 4
src/main/java/com/leanwo/management/model/ApplicationSetting.java

@@ -5,8 +5,6 @@ package com.leanwo.management.model;
 
 import java.util.List;
 
-import org.apache.commons.exec.ExecuteWatchdog;
-
 import com.leanwo.management.service.ProgramRunResult;
 import com.leanwo.management.util.CloseableUtil;
 
@@ -247,8 +245,12 @@ public class ApplicationSetting {
 	public void setProgramRunResult(ProgramRunResult programRunResult) {
 		if(programRunResult == null) {
 			if(this.programRunResult != null) {
-				CloseableUtil.close(this.programRunResult.getErrorPipedInputStream(), 
-						this.programRunResult.getOutPipedInputStream());
+				Process process = this.programRunResult.getProcess();
+				if(process != null) {
+					CloseableUtil.close(process.getInputStream(), process.getOutputStream(),
+							process.getErrorStream());
+					process.destroyForcibly();
+				}
 			}
 		}
 		this.programRunResult = programRunResult;

+ 0 - 1
src/main/java/com/leanwo/management/service/AutoUpdateService.java

@@ -23,7 +23,6 @@ import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 
 import org.apache.log4j.Logger;
-import org.springframework.stereotype.Service;
 
 import com.leanwo.management.model.ApplicationSetting;
 

+ 0 - 27
src/main/java/com/leanwo/management/service/ExecuteWatchdogEx.java

@@ -1,27 +0,0 @@
-package com.leanwo.management.service;
-
-import org.apache.commons.exec.ExecuteWatchdog;
-
-public class ExecuteWatchdogEx extends ExecuteWatchdog {
-
-	protected Process mProcess;
-	
-	public ExecuteWatchdogEx(long timeout) {
-		super(timeout);
-	}
-
-	@Override
-	public synchronized void start(Process processToMonitor) {
-		this.mProcess = processToMonitor;
-		super.start(processToMonitor);
-	}
-	
-	public synchronized boolean isAlive() {
-		if(mProcess == null) {
-			return false;
-		}else {
-			return mProcess.isAlive();
-		}
-	}
-
-}

+ 0 - 3
src/main/java/com/leanwo/management/service/HttpService.java

@@ -1,7 +1,5 @@
 package com.leanwo.management.service;
 
-import java.util.Date;
-
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpMethod;
@@ -9,7 +7,6 @@ import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.http.client.SimpleClientHttpRequestFactory;
 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
-import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
 import com.leanwo.management.model.ApplicationSetting;

+ 15 - 43
src/main/java/com/leanwo/management/service/ProcessService.java

@@ -1,25 +1,14 @@
 package com.leanwo.management.service;
 
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.io.PipedInputStream;
-import java.io.PipedOutputStream;
 import java.util.List;
 
-import org.apache.commons.exec.CommandLine;
-import org.apache.commons.exec.DefaultExecuteResultHandler;
-import org.apache.commons.exec.DefaultExecutor;
-import org.apache.commons.exec.ExecuteWatchdog;
-import org.apache.commons.exec.LogOutputStream;
-import org.apache.commons.exec.PumpStreamHandler;
 import org.apache.log4j.Logger;
-import org.springframework.stereotype.Service;
 
 import com.leanwo.management.ServerLogFrame;
 import com.leanwo.management.model.ApplicationSetting;
-import com.leanwo.management.util.SpringUtil;
 
 /**
  * 进程服务
@@ -40,13 +29,13 @@ public class ProcessService implements StatusService {
 		File workingDirectory = new File(setting.getInstallPath());
 		ProgramRunResult programRunResult = null;
 		
-		if(setting.getStartCommand() != null && setting.getStartCommand().length() > 0) {
-			programRunResult = runProgram(workingDirectory, setting.getStartCommand());
-		}else if(setting.getStartBatFile() != null && setting.getStartBatFile().length() > 0) {
+		//if(setting.getStartCommand() != null && setting.getStartCommand().length() > 0) {
+		//	programRunResult = runProgram(workingDirectory, setting.getStartCommand());
+		//}else if(setting.getStartBatFile() != null && setting.getStartBatFile().length() > 0) {
 			String fullPath = setting.getInstallPath() + File.separator + setting.getStartBatFile();
 			File batFile = new File(fullPath);
 			programRunResult = runProgram(workingDirectory, batFile);
-		}
+		//}
 		return programRunResult;
 	}
 	
@@ -107,30 +96,13 @@ public class ProcessService implements StatusService {
 		
 		logger.info("即将运行命令:" + command);
 		
-		final CommandLine cmdLine = CommandLine.parse(command);
-		
-		final ExecuteWatchdogEx watchDog = new ExecuteWatchdogEx(Long.MAX_VALUE);
-		
-		// 非阻塞方式执行进程
-		final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
-		DefaultExecutor executor = new DefaultExecutor();
-		executor.setWatchdog(watchDog);
-		
-		PipedOutputStream outPipedOutputStream = new PipedOutputStream();
-		PipedOutputStream errorPipedOutputStream = new PipedOutputStream();
-
-		PipedInputStream outPipedInputStream = new PipedInputStream(outPipedOutputStream);
-		PipedInputStream errorPipedInputStream = new PipedInputStream(errorPipedOutputStream);
-		
-		executor.setStreamHandler(new PumpStreamHandler(outPipedOutputStream, errorPipedOutputStream));
-		
-		executor.setWorkingDirectory(workingDirectory);
-		executor.execute(cmdLine, resultHandler);
-
+        ProcessBuilder processBuilder = new ProcessBuilder(command);
+        processBuilder.directory(workingDirectory);
+        
+        Process process = processBuilder.start();
+        
 		ProgramRunResult programRunResult = new ProgramRunResult();
-		programRunResult.setWatchDog(watchDog);
-		programRunResult.setOutPipedInputStream(outPipedInputStream);
-		programRunResult.setErrorPipedInputStream(errorPipedInputStream);
+		programRunResult.setProcess(process);
 		
 		return programRunResult;
 	}
@@ -146,8 +118,8 @@ public class ProcessService implements StatusService {
 				ProgramRunResult programRunResult;
 				try {
 					programRunResult = startProgram(setting);
-					if(programRunResult != null && programRunResult.getWatchDog() != null) {
-						boolean isAlive = programRunResult.getWatchDog().isAlive();
+					if(programRunResult != null && programRunResult.getProcess() != null) {
+						boolean isAlive = programRunResult.getProcess().isAlive();
 						if(! isAlive) {
 							logger.error("程序" + setting.getName() + "启动失败");
 						}
@@ -171,11 +143,11 @@ public class ProcessService implements StatusService {
 	public Boolean getStatus(ApplicationSetting setting) {
 		ProgramRunResult programRunResult = setting.getProgramRunResult();
 		if(programRunResult != null) {
-			ExecuteWatchdogEx executeWatchdog = programRunResult.getWatchDog();
-			if(executeWatchdog == null) {
+			Process process = programRunResult.getProcess();
+			if(process == null) {
 				return false;
 			}else{
-				if(executeWatchdog.isAlive()) {
+				if(process.isAlive()) {
 					return true;
 				}else {
 					return false;

+ 17 - 37
src/main/java/com/leanwo/management/service/ProgramRunResult.java

@@ -1,50 +1,30 @@
 package com.leanwo.management.service;
 
-import java.io.PipedInputStream;
-
-import org.apache.commons.exec.ExecuteWatchdog;
-
 /**
- * 程序运行结果
- * @author YangZhiJie1
+ * 程序运行结果.
  *
+ * @author YangZhiJie1
  */
 public class ProgramRunResult {
 	
-	private ExecuteWatchdogEx watchDog;
-	
-	/**
-	 * Out输入流
-	 */
-	private PipedInputStream outPipedInputStream;
-	
+	/**  子进程对象Process. */
+	private Process process;
+
 	/**
-	 * Error输入流
+	 * Gets the 子进程对象Process.
+	 *
+	 * @return the 子进程对象Process
 	 */
-	private PipedInputStream errorPipedInputStream;
-
-	public ExecuteWatchdogEx getWatchDog() {
-		return watchDog;
+	public Process getProcess() {
+		return process;
 	}
 
-	public void setWatchDog(ExecuteWatchdogEx watchDog) {
-		this.watchDog = watchDog;
-	}
-
-
-	public PipedInputStream getOutPipedInputStream() {
-		return outPipedInputStream;
-	}
-
-	public void setOutPipedInputStream(PipedInputStream outPipedInputStream) {
-		this.outPipedInputStream = outPipedInputStream;
-	}
-
-	public PipedInputStream getErrorPipedInputStream() {
-		return errorPipedInputStream;
-	}
-
-	public void setErrorPipedInputStream(PipedInputStream errorPipedInputStream) {
-		this.errorPipedInputStream = errorPipedInputStream;
+	/**
+	 * Sets the 子进程对象Process.
+	 *
+	 * @param process the new 子进程对象Process
+	 */
+	public void setProcess(Process process) {
+		this.process = process;
 	}
 }

+ 11 - 0
src/main/java/com/leanwo/management/util/ThreadUtil.java

@@ -0,0 +1,11 @@
+package com.leanwo.management.util;
+
+public class ThreadUtil {
+	public static void sleep(long millis) {
+		try {
+			Thread.sleep(millis);
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
+	}
+}

+ 0 - 3
src/main/java/com/leanwo/management/widget/TableButtonCell.java

@@ -3,13 +3,10 @@ package com.leanwo.management.widget;
 import java.awt.Component;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
-import java.util.EventObject;
 
 import javax.swing.AbstractCellEditor;
 import javax.swing.JButton;
-import javax.swing.JOptionPane;
 import javax.swing.JTable;
-import javax.swing.UIManager;
 import javax.swing.table.TableCellEditor;
 import javax.swing.table.TableCellRenderer;