|
@@ -1,19 +1,30 @@
|
|
|
package com.leanwo.management;
|
|
package com.leanwo.management;
|
|
|
|
|
|
|
|
|
|
+import java.awt.AWTEvent;
|
|
|
|
|
+import java.awt.AWTException;
|
|
|
import java.awt.BorderLayout;
|
|
import java.awt.BorderLayout;
|
|
|
import java.awt.GridBagConstraints;
|
|
import java.awt.GridBagConstraints;
|
|
|
import java.awt.GridBagLayout;
|
|
import java.awt.GridBagLayout;
|
|
|
|
|
+import java.awt.Image;
|
|
|
import java.awt.Insets;
|
|
import java.awt.Insets;
|
|
|
|
|
+import java.awt.SystemTray;
|
|
|
import java.awt.Toolkit;
|
|
import java.awt.Toolkit;
|
|
|
|
|
+import java.awt.TrayIcon;
|
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionEvent;
|
|
|
import java.awt.event.ActionListener;
|
|
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.ByteArrayOutputStream;
|
|
|
|
|
+import java.io.File;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
-import java.util.Date;
|
|
|
|
|
|
|
+import java.net.URL;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
import javax.swing.BorderFactory;
|
|
import javax.swing.BorderFactory;
|
|
|
import javax.swing.JButton;
|
|
import javax.swing.JButton;
|
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JFrame;
|
|
@@ -29,14 +40,6 @@ import org.apache.commons.exec.PumpStreamHandler;
|
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.Log;
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
import org.leanwo.management.util.SpringUtil;
|
|
import org.leanwo.management.util.SpringUtil;
|
|
|
-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.ApplicationSetting;
|
|
|
import com.leanwo.management.model.ApplicationSettingCache;
|
|
import com.leanwo.management.model.ApplicationSettingCache;
|
|
@@ -61,16 +64,23 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
private JPanel jPanel = null;
|
|
private JPanel jPanel = null;
|
|
|
private Timer timer;
|
|
private Timer timer;
|
|
|
|
|
|
|
|
|
|
+ private TrayIcon trayIcon; // 托盘图标
|
|
|
|
|
+ private SystemTray systemTray; // 系统托盘
|
|
|
|
|
+
|
|
|
private Map<ApplicationSetting, JLabel> statusLabels = null;
|
|
private Map<ApplicationSetting, JLabel> statusLabels = null;
|
|
|
|
|
|
|
|
- public ServerManagmentJFrame() {
|
|
|
|
|
|
|
+ public ServerManagmentJFrame(boolean isAutoRun) {
|
|
|
this.setLayout(new BorderLayout());
|
|
this.setLayout(new BorderLayout());
|
|
|
|
|
|
|
|
jPanel = new JPanel(new GridBagLayout());
|
|
jPanel = new JPanel(new GridBagLayout());
|
|
|
this.add(jPanel, BorderLayout.NORTH);
|
|
this.add(jPanel, BorderLayout.NORTH);
|
|
|
|
|
|
|
|
this.setTitle("Prodog程序管理器");
|
|
this.setTitle("Prodog程序管理器");
|
|
|
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设定按关闭时的操作,这里是关闭窗口,如果不设定,就什么也不会发生
|
|
|
|
|
|
|
+ // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设定按关闭时的操作,这里是关闭窗口,如果不设定,就什么也不会发生
|
|
|
|
|
+
|
|
|
|
|
+ //激活窗口事件
|
|
|
|
|
+ this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
|
|
|
|
|
+
|
|
|
// 设置窗体位置和大小
|
|
// 设置窗体位置和大小
|
|
|
this.setBounds((width - windowsWidth) / 2, (height - windowsHeight) / 2, windowsWidth, windowsHeight);
|
|
this.setBounds((width - windowsWidth) / 2, (height - windowsHeight) / 2, windowsWidth, windowsHeight);
|
|
|
|
|
|
|
@@ -81,8 +91,60 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
|
|
|
|
|
timer = new Timer(1000,this);
|
|
timer = new Timer(1000,this);
|
|
|
timer.start();
|
|
timer.start();
|
|
|
|
|
+
|
|
|
|
|
+ // 判断当前平台是否支持系统托盘
|
|
|
|
|
+ if(SystemTray.isSupported()) {
|
|
|
|
|
+ // 获得系统托盘的实例
|
|
|
|
|
+ systemTray = SystemTray.getSystemTray();
|
|
|
|
|
+ try {
|
|
|
|
|
+ URL path = ServerManagmentJFrame.class.getResource("prodog_16.png");
|
|
|
|
|
+ Image imgae = ImageIO.read(path);
|
|
|
|
|
+ trayIcon = new TrayIcon(imgae);
|
|
|
|
|
+ trayIcon.displayMessage("通知:", "程序最小化到系统托盘", TrayIcon.MessageType.INFO);
|
|
|
|
|
+ // 设置托盘的图标
|
|
|
|
|
+ systemTray.add(trayIcon);
|
|
|
|
|
+
|
|
|
|
|
+ trayIcon.addMouseListener(new MouseAdapter() {
|
|
|
|
|
+ public void mouseClicked(MouseEvent e) {
|
|
|
|
|
+ // 双击托盘窗口再现
|
|
|
|
|
+ if (e.getClickCount() == 2) {
|
|
|
|
|
+ boolean visible = isVisible();
|
|
|
|
|
+ // setExtendedState(Frame.NORMAL);
|
|
|
|
|
+ setVisible(!visible);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ this.addWindowListener(new WindowAdapter() {
|
|
|
|
|
+ public void windowIconified(WindowEvent e) {
|
|
|
|
|
+ // 窗口最小化时
|
|
|
|
|
+ setVisible(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ } catch (IOException e1) {
|
|
|
|
|
+ e1.printStackTrace();
|
|
|
|
|
+ } catch (AWTException e2) {
|
|
|
|
|
+ e2.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 程序自动运行
|
|
|
|
|
+ if(isAutoRun) {
|
|
|
|
|
+ autoStart();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ //重写这个方法
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void processWindowEvent(WindowEvent e) {
|
|
|
|
|
+ if (e.getID() == WindowEvent.WINDOW_CLOSING) {
|
|
|
|
|
+ setVisible(false);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ super.processWindowEvent(e); //该语句会执行窗口事件的默认动作(如:隐藏)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public void generateViewFromJson() {
|
|
public void generateViewFromJson() {
|
|
|
GridBagConstraints constrains = null;
|
|
GridBagConstraints constrains = null;
|
|
|
|
|
|
|
@@ -170,7 +232,8 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
@Override
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
try {
|
|
try {
|
|
|
- ExecuteWatchdog executeWatchdog = runProcess(setting.getStartBatFile());
|
|
|
|
|
|
|
+ String path = setting.getInstallPath() + File.separator + setting.getStartBatFile();
|
|
|
|
|
+ ExecuteWatchdog executeWatchdog = runProcess(path);
|
|
|
setting.setExecuteWatchdog(executeWatchdog);
|
|
setting.setExecuteWatchdog(executeWatchdog);
|
|
|
} catch (IOException e1) {
|
|
} catch (IOException e1) {
|
|
|
e1.printStackTrace();
|
|
e1.printStackTrace();
|
|
@@ -190,10 +253,11 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
@Override
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
try {
|
|
try {
|
|
|
- ExecuteWatchdog executeWatchdog = runProcess(setting.getStopBatFile());
|
|
|
|
|
|
|
+ String path = setting.getInstallPath() + File.separator + setting.getStopBatFile();
|
|
|
|
|
+ ExecuteWatchdog executeWatchdog = runProcess(path);
|
|
|
setting.setExecuteWatchdog(null);
|
|
setting.setExecuteWatchdog(null);
|
|
|
} catch (IOException e1) {
|
|
} catch (IOException e1) {
|
|
|
- e1.printStackTrace();
|
|
|
|
|
|
|
+ logger.error("程序启动失败", e1);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
@@ -208,6 +272,11 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
* @throws IOException
|
|
* @throws IOException
|
|
|
*/
|
|
*/
|
|
|
private ExecuteWatchdog runProcess(String command) throws IOException {
|
|
private ExecuteWatchdog runProcess(String command) throws IOException {
|
|
|
|
|
+ if(command == null || command.length() == 0) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ logger.info("即将运行命令:" + command);
|
|
|
|
|
+
|
|
|
final CommandLine cmdLine = CommandLine.parse(command);
|
|
final CommandLine cmdLine = CommandLine.parse(command);
|
|
|
|
|
|
|
|
final ExecuteWatchdog watchDog = new ExecuteWatchdog(Long.MAX_VALUE);
|
|
final ExecuteWatchdog watchDog = new ExecuteWatchdog(Long.MAX_VALUE);
|
|
@@ -223,7 +292,7 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
|
|
|
|
|
//等待5秒。
|
|
//等待5秒。
|
|
|
try {
|
|
try {
|
|
|
- resultHandler.waitFor(5000);
|
|
|
|
|
|
|
+ resultHandler.waitFor(2000);
|
|
|
} catch (InterruptedException e) {
|
|
} catch (InterruptedException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}
|
|
}
|
|
@@ -248,38 +317,42 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
if(executeWatchdog == null) {
|
|
if(executeWatchdog == null) {
|
|
|
statusLabel.setText("未运行");
|
|
statusLabel.setText("未运行");
|
|
|
}else{
|
|
}else{
|
|
|
- statusLabel.setText("运行中");
|
|
|
|
|
|
|
+ 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 ++) {
|
|
|
|
|
+ final ApplicationSetting setting = settings.get(i);
|
|
|
|
|
+ ExecuteWatchdog executeWatchdog;
|
|
|
|
|
+ try {
|
|
|
|
|
+ String path = setting.getInstallPath() + File.separator + setting.getStartBatFile();
|
|
|
|
|
+ executeWatchdog = runProcess(path);
|
|
|
|
|
+ if(executeWatchdog != null) {
|
|
|
|
|
+ boolean isWatch = executeWatchdog.isWatching();
|
|
|
|
|
+ if(! isWatch) {
|
|
|
|
|
+ logger.error("程序" + setting.getName() + "启动失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ setting.setExecuteWatchdog(executeWatchdog);
|
|
|
}
|
|
}
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ logger.error("程序" + setting.getName() + "启动失败", e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|