瀏覽代碼

1.修改了日志控件,使其线程安全。
2.添加全部停止的功能。

YangZhiJie 7 年之前
父節點
當前提交
8162896cc4

+ 55 - 8
config/applicationContext.xml

@@ -25,14 +25,18 @@
 	<bean id="applicationSettingCache" class="com.leanwo.management.model.ApplicationSettingCache">
 		<property name="settings">
 			<list>
-				<ref bean="applicationSetting1" />
-				<ref bean="applicationSetting2" />
-				<ref bean="applicationSetting3" />
-				<ref bean="applicationSetting4" />
-				<ref bean="applicationSetting5" />
-				<ref bean="applicationSetting6" />
-				<ref bean="applicationSetting7" />
-				<ref bean="applicationSetting8" />
+<!-- 				<ref bean="applicationSetting1" /> -->
+<!-- 				<ref bean="applicationSetting2" /> -->
+<!-- 				<ref bean="applicationSetting3" /> -->
+<!-- 				<ref bean="applicationSetting4" /> -->
+<!-- 				<ref bean="applicationSetting5" /> -->
+<!-- 				<ref bean="applicationSetting6" /> -->
+<!-- 				<ref bean="applicationSetting7" /> -->
+<!-- 				<ref bean="applicationSetting8" /> -->
+				<ref bean="applicationSetting13" />
+				<ref bean="applicationSetting14" />
+				<ref bean="applicationSetting15" />
+				<ref bean="applicationSetting16" />
 			</list>
 		</property>
 	</bean>
@@ -162,4 +166,47 @@
 		<property name="startBatFile" value="start.bat"></property>
 		<property name="stopBatFile" value="stop.bat"></property>
 	</bean>
+	
+	
+	<bean id="applicationSetting11" class="com.leanwo.management.model.ApplicationSetting">
+		<property name="name" value="源代码服务器"></property>
+		<property name="installPath" value="D:\ProdogServers\TortoiseHg"></property>
+		<property name="startCommand" value="start.bat"></property>
+		<property name="startBatFile" value="stop.bat"></property>
+		<property name="stopBatFile" value="stop.bat"></property>
+	</bean>
+	
+	
+	<bean id="applicationSetting12" class="com.leanwo.management.model.ApplicationSetting">
+		<property name="name" value="natapp服务器"></property>
+		<property name="installPath" value="D:\ProdogServers\natapp_windows_8080"></property>
+		<property name="startBatFile" value="natapp.exe"></property>
+		<property name="stopBatFile" value="stop.bat"></property>
+	</bean>
+	
+	
+	<bean id="applicationSetting13" class="com.leanwo.management.model.ApplicationSetting">
+		<property name="name" value="测试服务器"></property>
+		<property name="installPath" value="D:\LeanwoProgram_2018\OtherServer\ServerManagement\src\test\java"></property>
+		<property name="startBatFile" value="test.bat"></property>
+		<property name="stopBatFile" value="stop.bat"></property>
+	</bean>
+	<bean id="applicationSetting14" class="com.leanwo.management.model.ApplicationSetting">
+		<property name="name" value="测试服务器"></property>
+		<property name="installPath" value="D:\LeanwoProgram_2018\OtherServer\ServerManagement\src\test\java"></property>
+		<property name="startBatFile" value="test.bat"></property>
+		<property name="stopBatFile" value="stop.bat"></property>
+	</bean>
+	<bean id="applicationSetting15" class="com.leanwo.management.model.ApplicationSetting">
+		<property name="name" value="测试服务器"></property>
+		<property name="installPath" value="D:\LeanwoProgram_2018\OtherServer\ServerManagement\src\test\java"></property>
+		<property name="startBatFile" value="test.bat"></property>
+		<property name="stopBatFile" value="stop.bat"></property>
+	</bean>
+	<bean id="applicationSetting16" class="com.leanwo.management.model.ApplicationSetting">
+		<property name="name" value="测试服务器"></property>
+		<property name="installPath" value="D:\LeanwoProgram_2018\OtherServer\ServerManagement\src\test\java"></property>
+		<property name="startBatFile" value="test.bat"></property>
+		<property name="stopBatFile" value="stop.bat"></property>
+	</bean>
 </beans>

+ 0 - 6
pom.xml

@@ -57,12 +57,6 @@
 			<version>4.3.12.RELEASE</version>
 		</dependency>
 		
-		<dependency>
-		    <groupId>org.apache.commons</groupId>
-		    <artifactId>commons-exec</artifactId>
-		    <version>1.3</version>
-		</dependency>
-		
 		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
 		<dependency>
 			<groupId>com.fasterxml.jackson.core</groupId>

+ 34 - 4
src/main/java/com/leanwo/management/MainFrame.java

@@ -53,6 +53,7 @@ public class MainFrame extends JFrame{
 	private JButton upgradeMenu = null;
 	private JButton editConfigMenu = null;
 	private JButton startAllMenu = null;
+	private JButton stopAllMenu = null;
 	
 	private ServerFrame serverFrame = null;
 	private ServerLogFrame serverLogFrame =  null;
@@ -118,6 +119,9 @@ public class MainFrame extends JFrame{
         startAllMenu = new JButton("全部启动");
         toolbar.add(startAllMenu);
         
+        stopAllMenu = new JButton("全部关闭");
+        toolbar.add(stopAllMenu);
+        
 		serverFrame = new ServerFrame(settings);
 		serverFrame.setPreferredSize(new Dimension(300, 0));
         add(serverFrame, BorderLayout.WEST);
@@ -204,16 +208,15 @@ public class MainFrame extends JFrame{
 						if(programRunResult != null && programRunResult.getWatchDog() != null) {
 							programRunResult.getWatchDog().destroyProcess();
 						}
-						
 						programRunResult = processService.stopProgram(setting);
 						if(programRunResult == null || programRunResult.getWatchDog() == null) {
-							JOptionPane.showMessageDialog(null, "未配置停止文件", "配置异常", JOptionPane.ERROR_MESSAGE);
+							JOptionPane.showMessageDialog(null, setting.getName() +"未配置停止文件", "程序关闭异常", JOptionPane.ERROR_MESSAGE);
 						}else {
 							setting.setProgramRunResult(null);
 						}				
 					} catch (IOException e1) {
-						JOptionPane.showMessageDialog(null, e1.getMessage(), "程序关闭失败", JOptionPane.ERROR_MESSAGE);
-						logger.error("程序关闭失败", e1);
+						JOptionPane.showMessageDialog(null, e1.getMessage(), setting.getName() +"程序关闭失败", JOptionPane.ERROR_MESSAGE);
+						logger.error(setting.getName() +"程序关闭失败", e1);
 					}
 				}else {
 					JOptionPane.showMessageDialog(null, "请先选择服务器", "请先选择服务器", JOptionPane.ERROR_MESSAGE);
@@ -257,6 +260,7 @@ public class MainFrame extends JFrame{
 			}
 		});
 		
+		// 全部启动
 		startAllMenu.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
@@ -278,6 +282,32 @@ public class MainFrame extends JFrame{
 				}
 			}
 		});
+		
+		// 全部关闭
+		stopAllMenu.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				if (settings != null && settings.size() > 0) {
+					for(int i = 0; i < settings.size(); i ++) {
+						final ApplicationSetting setting = settings.get(i);
+						try {
+							ProgramRunResult programRunResult = setting.getProgramRunResult();
+							if(programRunResult != null && programRunResult.getWatchDog() != null) {
+								programRunResult.getWatchDog().destroyProcess();
+							}
+							programRunResult = processService.stopProgram(setting);
+							if(programRunResult == null || programRunResult.getWatchDog() == null) {
+								logger.error(setting.getName() + "程序关闭异常,未配置停止文件。");
+							}else {
+								setting.setProgramRunResult(null);
+							}				
+						} catch (IOException e1) {
+							logger.error(setting.getName() +"程序关闭失败", e1);
+						}
+					}
+				}
+			}
+		});
 	}
 	
 	/**

+ 5 - 1
src/main/java/com/leanwo/management/ServerFrame.java

@@ -231,8 +231,11 @@ public class ServerFrame extends JPanel {
 
 					setting.setOnline(online);
 					String status = online ? Variable.RUNNING : Variable.NOT_RUNNING;
-					table.setValueAt(status, i, 2);
+					
+					final int tempI = i;
 
+					table.setValueAt(status, tempI, 2);
+					
 					if (selectedRow == i) {
 						if(oldStatus != online) {
 							fireSelectEvent(selectedRow);
@@ -241,6 +244,7 @@ public class ServerFrame extends JPanel {
 				}
 			}
 			isRunning = false;
+			
 			updateTable();
 		}
 	};

+ 9 - 3
src/main/java/com/leanwo/management/ServerLogFrame.java

@@ -11,6 +11,7 @@ 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;
 
@@ -100,9 +101,14 @@ public class ServerLogFrame extends JPanel{
 	private TimerTask timerTask = new TimerTask() {
 		@Override
 		public void run() {
-			for(LogConsole logConsole : logConsoleMap.values()) {
-				logConsole.reserveMaxCharacter();
-			}
+			 SwingUtilities.invokeLater(new Runnable() {
+	             @Override
+	             public void run() {
+					for(LogConsole logConsole : logConsoleMap.values()) {
+						logConsole.reserveMaxCharacter();
+					}
+	             }
+	         });	
 		}
 	};
 }

+ 62 - 11
src/main/java/com/leanwo/management/console/LogConsole.java

@@ -8,13 +8,16 @@ import java.io.InputStream;
 import java.io.PipedInputStream;
 
 import javax.swing.JButton;
+import javax.swing.JCheckBox;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTextArea;
+import javax.swing.SwingUtilities;
 
 public class LogConsole extends JPanel{
 
 	private JTextArea textArea;
+	private JCheckBox autoScrollCheckBox;
 	private JButton clearButton;
 	
 	private PipedInputStream outPipedInputStream;
@@ -25,7 +28,10 @@ public class LogConsole extends JPanel{
 	private Thread errorReaderThread;
 	
 	/** 字符串最大的个数 */
-	private static final int MAX_CHARACTER_COUNT = 100000;
+	private static final int MAX_CHARACTER_COUNT = 10000;
+	
+	/** 自动滚动 */
+	private boolean autoScroll;
 	
 	public LogConsole(PipedInputStream outPipedInputStream, PipedInputStream errorPipedInputStream) {
 		this.outPipedInputStream = outPipedInputStream;
@@ -46,6 +52,9 @@ public class LogConsole extends JPanel{
 		// 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);	
@@ -62,16 +71,34 @@ public class LogConsole extends JPanel{
 	private void initView() {
 		textArea=new JTextArea();
 		textArea.setEditable(false);
+		textArea.setText("");
+		
+		JPanel bottomPanel = new JPanel();
+		bottomPanel.setLayout(new BorderLayout());
+		
+		autoScrollCheckBox = new JCheckBox("自动滚动");
+		autoScrollCheckBox.setSelected(autoScroll);
+		bottomPanel.add(autoScrollCheckBox, BorderLayout.WEST);
+		
 		clearButton=new JButton("清空");
+		bottomPanel.add(clearButton);
 		
 		setLayout(new BorderLayout());
 		add(new JScrollPane(textArea),BorderLayout.CENTER);
-		add(clearButton,BorderLayout.SOUTH);
+		add(bottomPanel,BorderLayout.SOUTH);
 		
 		clearButton.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				textArea.setText(null);
+				textArea.setText("");
+			}
+		});
+		
+		autoScrollCheckBox.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				autoScroll = autoScrollCheckBox.isSelected();
+				scrollToEnd();
 			}
 		});
 	}
@@ -82,17 +109,17 @@ public class LogConsole extends JPanel{
 			try {
 				while (!quit) {
 					try {
-						this.wait(100);
+						this.wait(10);
 					} catch (InterruptedException ie) {
 					}
 					if (outPipedInputStream != null && outPipedInputStream.available() != 0) {
 						String input = readLine(outPipedInputStream);
-						textArea.append(input);
+						appendLog(input);						
 					}
 				}
 			} catch (Exception e) {
-				textArea.append("\nConsole reports an Internal error.");
-				textArea.append("The error is: " + e);
+				String text = "\r\nConsole reports an Internal error.\r\nThe error is:" + e.getMessage();
+				appendLog(text);
 			}
 		}
 	};
@@ -104,17 +131,17 @@ public class LogConsole extends JPanel{
 			try {
 				while (!quit) {
 					try {
-						this.wait(100);
+						this.wait(10);
 					} catch (InterruptedException ie) {
 					}
 					if (errorPipedInputStream != null && errorPipedInputStream.available() != 0) {
 						String input = readLine(errorPipedInputStream);
-						textArea.append(input);
+						appendLog(input);	
 					}
 				}
 			} catch (Exception e) {
-				textArea.append("\nConsole reports an Internal error.");
-				textArea.append("The error is: " + e);
+				String text = "\r\nConsole reports an Internal error.\r\nThe error is:" + e.getMessage();
+				appendLog(text);
 			}
 		}
 	};
@@ -141,5 +168,29 @@ public class LogConsole extends JPanel{
 			text = text.substring(text.length() - MAX_CHARACTER_COUNT *2/3);
 			textArea.setText(text);
 		}
+		textArea.setCaretPosition(textArea.getText().length());
+	}
+	
+	/**
+	 * 添加日志
+	 * @param text
+	 */
+	private void appendLog(String text) {
+		 SwingUtilities.invokeLater(new Runnable() {
+             @Override
+             public void run() {
+        	 	textArea.append(text);
+        	 	scrollToEnd();	
+             }
+         });	
+	}
+	
+	/**
+	 * 滚动到最底端
+	 */
+	private void scrollToEnd() {
+		if(autoScroll) {
+			textArea.setCaretPosition(textArea.getText().length());
+		}
 	}
 }

+ 4 - 0
src/test/java/test.bat

@@ -0,0 +1,4 @@
+@echo off
+:S
+@echo %date%%time%
+goto S