Explorar o código

修复定时程序BUG。

YangZhiJie %!s(int64=8) %!d(string=hai) anos
pai
achega
3aa869cf3a
Modificáronse 1 ficheiros con 82 adicións e 62 borrados
  1. 82 62
      src/main/java/com/leanwo/management/ServerManagmentJFrame.java

+ 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);
+		    }
+		});
+	}
 }