TianGuangHui 8 yıl önce
ebeveyn
işleme
0223f7293a

+ 1 - 1
autoStart.bat

@@ -1,2 +1,2 @@
-CD D:\LeanwoProgram_2017\ProdogV7-ServerManagement\ServerManagement
+CD D:\LeanwoProgram_2017\ProdogV6\ServerManagement
 D:

+ 2 - 3
config/applicationContext.xml

@@ -44,11 +44,10 @@
 		</property>		
 		<property name="xmlConfigPath" >
 			<bean class="com.leanwo.management.model.XmlConfigPath">
-				<property name="xmlFileName" value="config/applicationContext_topnc.xml"></property>
+				<property name="xmlFileName" value="config/applicationContext.xml"></property>
 				<property name="nodeNames">
 					<list>
-						<value>dataSource1</value>
-						<value>dataSource2</value>
+						<value>dataSource</value>
 					</list>
 				</property>
 			</bean>

+ 82 - 62
src/main/java/com/leanwo/management/ServerManagmentJFrame.java

@@ -7,6 +7,7 @@ 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;
@@ -24,6 +25,8 @@ import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Timer;
+import java.util.TimerTask;
 
 import javax.imageio.ImageIO;
 import javax.swing.BorderFactory;
@@ -32,7 +35,7 @@ import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
-import javax.swing.Timer;
+import javax.swing.SwingUtilities;
 
 import org.apache.commons.exec.CommandLine;
 import org.apache.commons.exec.DefaultExecuteResultHandler;
@@ -61,7 +64,7 @@ import com.leanwo.management.model.ApplicationSettingCache;
  * @author YangZhiJie
  *
  */
-public class ServerManagmentJFrame extends JFrame implements ActionListener {
+public class ServerManagmentJFrame extends JFrame {
 	private static Log logger = LogFactory.getLog(ServerManagmentJFrame.class);
 	
 	// 得到显示器屏幕的宽高
@@ -73,12 +76,14 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
 	private int windowsHeight = 400;
 
 	private JPanel jPanel = null;
-	//private Timer timer;
+	private Timer timer;
 
 	private TrayIcon trayIcon;				// 托盘图标
 	private SystemTray systemTray;	// 系统托盘
 	
 	private Map<ApplicationSetting, JLabel> statusLabels = null;
+	private ApplicationSettingCache applicationSettingCache;
+	private boolean isRunning = false;
 	
 	public ServerManagmentJFrame(boolean isAutoRun) {
 		this.setLayout(new BorderLayout());
@@ -101,8 +106,8 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
 		
 		this.setVisible(true); // 显示,如果不设置就什么都看不到
 		
-		//timer = new Timer(1000,this);
-		//timer.start();
+		timer = new Timer();
+		timer.schedule(timerTimerTask, 2000L, 2000L);
 		
 		// 判断当前平台是否支持系统托盘
 		if(SystemTray.isSupported()) {
@@ -226,7 +231,7 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
         constrains.anchor = GridBagConstraints.NORTH;
         jPanel.add(label, constrains);
         
-		ApplicationSettingCache applicationSettingCache = (ApplicationSettingCache) SpringUtil.getSingleBean(ApplicationSettingCache.class);
+		applicationSettingCache = (ApplicationSettingCache) SpringUtil.getSingleBean(ApplicationSettingCache.class);
 		List<ApplicationSetting> settings = applicationSettingCache.getSettings();
 		if (settings != null && settings.size() > 0) {
 			for(int i = 0; i < settings.size(); i ++) {
@@ -380,67 +385,11 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
 		return watchDog;
 	}
 
-	@Override
-	public void actionPerformed(ActionEvent e) {
-		ApplicationSettingCache applicationSettingCache = (ApplicationSettingCache) SpringUtil.getSingleBean(ApplicationSettingCache.class);
-
-		List<ApplicationSetting> settings = applicationSettingCache.getSettings();
-		if (settings != null && settings.size() > 0) {
-			for(int i = 0; i < settings.size(); i ++) {
-				final ApplicationSetting setting = settings.get(i);
-				JLabel statusLabel = statusLabels.get(setting);
-				if(statusLabel != null) {
-					ExecuteWatchdog executeWatchdog = setting.getExecuteWatchdog();
-					if(executeWatchdog == null) {
-						statusLabel.setText("未运行");
-					}else{
-						if(executeWatchdog.isWatching()) {
-							statusLabel.setText("运行中");					
-						}else {
-							statusLabel.setText("已停止");		
-						}
-					}
-				}
-				
-				// 监控各个服务程序是否正在运行
-				/**
-				String url = setting.getMonitorUrl();
-				String token = setting.getToken();
-				if(url == null || "".equals(url) || token == null || "".equals(token)){
-					continue;
-				}
-				RestTemplate restTemplate = new RestTemplate();		
-				restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
-				HttpHeaders headers = new HttpHeaders();
-				headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
-				headers.add("account", "2");
-				headers.add("token", token);
-				HttpEntity entity = new HttpEntity(headers);
-				try{			
-					ResponseEntity<Long> response = restTemplate.exchange(url, HttpMethod.GET, entity, Long.class);
-					((SimpleClientHttpRequestFactory)restTemplate.getRequestFactory()).setReadTimeout(1000*30);
-					((SimpleClientHttpRequestFactory)restTemplate.getRequestFactory()).setConnectTimeout(1000*30);
-					Long result = response.getBody();
-					long mills = new Date().getTime() - result;
-					if(statusLabel != null){
-						statusLabel.setText("运行中(" + mills + "ms)");
-					}
-				}catch(Exception ex){
-					if(statusLabel != null){
-						statusLabel.setText("已停止");
-					}
-				}
-				*/
-			}
-		}
-	}
 	
 	/**
 	 * 程序自动运行
 	 */
 	private void autoStart() {
-		ApplicationSettingCache applicationSettingCache = (ApplicationSettingCache) SpringUtil.getSingleBean(ApplicationSettingCache.class);
-
 		List<ApplicationSetting> settings = applicationSettingCache.getSettings();
 		if (settings != null && settings.size() > 0) {
 			for(int i = 0; i < settings.size(); i ++) {
@@ -462,4 +411,75 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
 			}
 		}
 	}
+	
+	private TimerTask timerTimerTask = new TimerTask() {
+		@Override
+		public void run() {
+			if(isRunning) {
+				logger.debug("上个定时程序还未运行完成,本次运行跳过");
+				return;
+			}
+			isRunning = true;
+			List<ApplicationSetting> settings = applicationSettingCache.getSettings();
+			if (settings != null && settings.size() > 0) {
+				for(int i = 0; i < settings.size(); i ++) {
+					final ApplicationSetting setting = settings.get(i);
+					JLabel statusLabel = statusLabels.get(setting);
+					if(statusLabel != null) {
+						/*
+						ExecuteWatchdog executeWatchdog = setting.getExecuteWatchdog();
+						if(executeWatchdog == null) {
+							statusLabel.setText("未运行");
+						}else{
+							if(executeWatchdog.isWatching()) {
+								statusLabel.setText("运行中");					
+							}else {
+								statusLabel.setText("已停止");		
+							}
+						}*/
+						
+						// 监控各个服务程序是否正在运行
+						String status = null;
+						String url = setting.getMonitorUrl();
+						String token = setting.getToken();
+						if(url == null || "".equals(url) || token == null || "".equals(token)){
+							continue;
+						}
+						RestTemplate restTemplate = new RestTemplate();		
+						restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
+						HttpHeaders headers = new HttpHeaders();
+						headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
+						headers.add("account", "2");
+						headers.add("token", token);
+						HttpEntity entity = new HttpEntity(headers);
+						try{			
+							ResponseEntity<Long> response = restTemplate.exchange(url, HttpMethod.GET, entity, Long.class);
+							((SimpleClientHttpRequestFactory)restTemplate.getRequestFactory()).setReadTimeout(1000*30);
+							((SimpleClientHttpRequestFactory)restTemplate.getRequestFactory()).setConnectTimeout(1000*30);
+							Long result = response.getBody();
+							long mills = new Date().getTime() - result;
+							status = "运行中(" + mills + "ms)";
+						}catch(Exception ex){
+							status = "已停止";
+						}
+						updateStatus(statusLabel, status);
+					}
+				}
+			}
+			isRunning = false;
+		}
+	};
+	
+	/**
+	 * 在其他线程更新状态Label的文本
+	 * @param statusLabel
+	 * @param status
+	 */
+	private void updateStatus(JLabel statusLabel, String status) {
+		SwingUtilities.invokeLater(new Runnable() {
+		    public void run() {
+		    	statusLabel.setText(status);
+		    }
+		});
+	}
 }

+ 62 - 27
src/main/java/com/leanwo/management/ServerSettingJFrame.java

@@ -4,7 +4,6 @@ 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;
@@ -24,16 +23,19 @@ 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.JScrollPane;
 import javax.swing.JTextArea;
 import javax.swing.JTextField;
 import javax.swing.Timer;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.leanwo.management.exception.ConfigFileException;
 import org.leanwo.management.util.SpringUtil;
 import org.leanwo.management.util.XmlConfigPathService;
 
@@ -46,71 +48,99 @@ import com.leanwo.management.model.XmlConfigPath;
  *
  */
 public class ServerSettingJFrame  extends JFrame{
+	
+	private static Log logger = LogFactory.getLog(ServerSettingJFrame.class);
+
 	private ApplicationSetting applicationSetting;
 	private XmlConfigPath xmlConfigPath;
 	private XmlConfigPathService xmlConfigPathService;
-	private Container jPanel = null;
+	private JPanel mainPanel = null;
+	private JPanel innerPanel = null;
+	private GridBagConstraints constrains = null;
 	private JLabel label = null;
 	private JTextField portTextField;
 	private JTextField tokenTextField;
 	private Map<String, JTextArea> textAreas = new HashMap<String, JTextArea>();
 	private JButton saveButton;
 	
+	private int index = 0;
+	
 	public ServerSettingJFrame(ApplicationSetting applicationSetting) {
-
-		this.jPanel = this.getContentPane();  
-
 		this.setSize(800, 600);
 		this.setTitle("程序配置管理器");
+		
+		this.mainPanel = new JPanel(new BorderLayout());
+		this.add(mainPanel);
+				
+		
 		this.xmlConfigPathService = (XmlConfigPathService) SpringUtil.getSingleBean(XmlConfigPathService.class);
 		this.applicationSetting = applicationSetting;
-		this.xmlConfigPathService.load(applicationSetting);
+		
+		try {
+			this.xmlConfigPathService.load(applicationSetting);
+		}catch(ConfigFileException ex) {
+			logger.error("配置文件异常", ex);
+			JOptionPane.showMessageDialog(null, ex.getMessage(), "配置文件异常", JOptionPane.ERROR_MESSAGE);
+			this.dispose();
+		}
+		
 		this.xmlConfigPath = applicationSetting.getXmlConfigPath();
-		jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));  
+		this.innerPanel = new JPanel(new GridBagLayout());
+		
+		this.constrains = new GridBagConstraints();
+		this.constrains.insets = new Insets(5, 5, 5, 5);
+		this.constrains.fill = GridBagConstraints.BOTH;
+		this.constrains.gridx = 0;
+		this.constrains.gridy = index;
+		this.constrains.weightx = 100;        
+		this.constrains.anchor = GridBagConstraints.NORTH;
+		
+		JScrollPane scrollPanel = new JScrollPane(innerPanel);
+		mainPanel.add(scrollPanel, BorderLayout.CENTER);
 		
         if(this.xmlConfigPath != null) {
-    		label = new JLabel("端口号");
-    		label.setAlignmentY(LEFT_ALIGNMENT);
-
-            jPanel.add(label);
+    		label = new JLabel("端口号");    		
+            innerPanel.add(label, constrains);
             
             portTextField = new JTextField();
-            portTextField.setAlignmentY(LEFT_ALIGNMENT);
+            portTextField.setBorder(javax.swing.BorderFactory.createLineBorder(Color.BLACK));
             if(xmlConfigPath.getPortNo() != null) {
             	portTextField.setText(xmlConfigPath.getPortNo().toString());
             }
-            jPanel.add(portTextField);
+    		this.constrains.gridy = ++ index;
+            innerPanel.add(portTextField, constrains);
             
             
     		label = new JLabel("Token");
-    		label.setAlignmentY(LEFT_ALIGNMENT);
-            jPanel.add(label);
+    		this.constrains.gridy = ++ index;
+            innerPanel.add(label, constrains);
 
             tokenTextField = new JTextField();
-            tokenTextField.setAlignmentY(LEFT_ALIGNMENT);
-            tokenTextField.setText(xmlConfigPath.getToken());
-            jPanel.add(tokenTextField);
+            tokenTextField.setBorder(javax.swing.BorderFactory.createLineBorder(Color.BLACK));
+    		this.constrains.gridy = ++ index;
+            innerPanel.add(tokenTextField, constrains);
             
             
             
         	Map<String, String> contents = applicationSetting.getXmlConfigPath().getContents();
         	for(String key : contents.keySet()) {
         		label = new JLabel(key);
-        		label.setAlignmentY(LEFT_ALIGNMENT);
-                jPanel.add(label);
+        		this.constrains.gridy = ++ index;
+                innerPanel.add(label, constrains);
                 
         		JTextArea textArea = new JTextArea();
-        		textArea.setAlignmentY(LEFT_ALIGNMENT);
         		textArea.setText(contents.get(key));
-        		textArea.setSelectedTextColor(Color.RED);
-        		textArea.setLineWrap(true);        //激活自动换行功能 
-        		textArea.setWrapStyleWord(true);            // 激活断行不断字功能
-                jPanel.add(textArea);
+        		textArea.setRows(5);
+        		textArea.setBorder(javax.swing.BorderFactory.createLineBorder(Color.BLACK));
                 textAreas.put(key, textArea);
+        		this.constrains.gridy = ++ index;
+                innerPanel.add(textArea, constrains);
         	}
 
+    		index ++;
         	saveButton = new JButton("保存");
-            jPanel.add(saveButton);
+    		this.constrains.gridy = ++ index;
+            innerPanel.add(saveButton, constrains);
             
             saveButton.addActionListener(new ActionListener() {
 				@Override
@@ -136,5 +166,10 @@ public class ServerSettingJFrame  extends JFrame{
         }
 
 		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
+//		this.addWindowListener(new java.awt.event.WindowAdapter() {
+//			public void windowClosing(java.awt.event.WindowEvent e) {
+//				ServerSettingJFrame.this.setVisible(false);
+//			}
+//		});
 	}
 }

+ 12 - 0
src/main/java/org/leanwo/management/exception/ConfigFileException.java

@@ -0,0 +1,12 @@
+package org.leanwo.management.exception;
+
+/**
+ * 配置文件异常
+ * @author YangZhiJie
+ *
+ */
+public class ConfigFileException extends Exception {
+	public ConfigFileException(String msg) {
+		super(msg);
+	}
+}

+ 12 - 2
src/main/java/org/leanwo/management/util/XmlConfigPathService.java

@@ -24,6 +24,7 @@ import org.dom4j.Element;
 import org.dom4j.io.OutputFormat;
 import org.dom4j.io.SAXReader;
 import org.dom4j.io.XMLWriter;
+import org.leanwo.management.exception.ConfigFileException;
 
 import com.leanwo.management.model.ApplicationSetting;
 import com.leanwo.management.model.XmlConfigPath;
@@ -35,8 +36,9 @@ public class XmlConfigPathService {
 	 * 从xml配中获取node 的数据
 	 * ,contents等属性
 	 * @param setting
+	 * @throws ConfigFileException 
 	 */
-	public void load(ApplicationSetting setting) {
+	public void load(ApplicationSetting setting) throws ConfigFileException {
 		Map<String, String> contentMap = new HashMap<String, String>();
 		XmlConfigPath xmlConfigPath = setting.getXmlConfigPath();
 		String xmlFilePath = setting.getInstallPath() + "\\" + xmlConfigPath.getXmlFileName();
@@ -77,7 +79,7 @@ public class XmlConfigPathService {
 				e.printStackTrace();
 			} 
 		}else{
-			throw new RuntimeException("文件:"+xmlFilePath+",不存在");
+			throw new ConfigFileException("文件:"+xmlFilePath+",不存在");
 		}
 		//读取propertis信息
 		String propertiesPath = setting.getInstallPath()+"/config/config.properties";
@@ -115,6 +117,14 @@ public class XmlConfigPathService {
 		}
 	}
 	
+	private static String getContent(Element element) {
+		  StringBuilder builder = new StringBuilder();
+		  for (Iterator<Element> i = element.elementIterator(); i.hasNext();) {
+		    Element e = i.next();
+		    builder.append(e.asXML());
+		  }
+		  return builder.toString();
+		}
 	
 	/**
 	 * 获得X属性结果是X值的整个标签