|
|
@@ -3,13 +3,17 @@ package com.leanwo.management.service;
|
|
|
import java.io.File;
|
|
|
import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import com.leanwo.management.ServerLogFrame;
|
|
|
import com.leanwo.management.model.ApplicationSetting;
|
|
|
+import com.leanwo.management.util.ThreadUtil;
|
|
|
|
|
|
/**
|
|
|
* 进程服务
|
|
|
@@ -20,6 +24,7 @@ public class ProcessService implements StatusService {
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ProcessService.class);
|
|
|
|
|
|
+ private Thread startAllThread = null;
|
|
|
/**
|
|
|
* 启动新的程序
|
|
|
* @param setting
|
|
|
@@ -110,27 +115,113 @@ public class ProcessService implements StatusService {
|
|
|
* 程序自动运行
|
|
|
*/
|
|
|
public void autoStart(List<ApplicationSetting> settings, ServerLogFrame serverLogFrame) {
|
|
|
+ if(startAllThread != null) {
|
|
|
+ startAllThread.interrupt();
|
|
|
+ startAllThread = null;
|
|
|
+ }
|
|
|
+ // 1. 启动一个新的线程
|
|
|
+ Map<Integer, List<ApplicationSetting>> applicationSettingMap = new HashMap<Integer, List<ApplicationSetting>>();
|
|
|
+ Runnable startAllRunnable = new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ // 2. 对settings根据启动优先级进行分组
|
|
|
+ if (settings != null && settings.size() > 0) {
|
|
|
+ for(int index = 0; index < 10; index ++) {
|
|
|
+ List<ApplicationSetting> applicationSettings = new ArrayList<ApplicationSetting>();
|
|
|
+ for(int i = 0; i < settings.size(); i ++) {
|
|
|
+ final ApplicationSetting setting = settings.get(i);
|
|
|
+ if(setting.getStartSequence() != null && setting.getStartSequence().equals(index)) {
|
|
|
+ applicationSettings.add(setting);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(index == 9 && setting.getStartSequence() == null) {
|
|
|
+ applicationSettings.add(setting);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ applicationSettingMap.put(index, applicationSettings);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 一个一个的启动
|
|
|
+ for(int index = 0; index < 10; index ++) {
|
|
|
+ List<ApplicationSetting> applicationSettings = applicationSettingMap.get(index);
|
|
|
+ if(applicationSettings != null && applicationSettings.size() > 0) {
|
|
|
+ for(int i = 0; i < applicationSettings.size(); i ++) {
|
|
|
+ if(startAllThread == null || (startAllThread != null && startAllThread.isInterrupted())) {
|
|
|
+ // 线程被中断,返回
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ final ApplicationSetting setting = applicationSettings.get(i);
|
|
|
+ ProgramRunResult programRunResult;
|
|
|
+ try {
|
|
|
+ programRunResult = startProgram(setting);
|
|
|
+ if(programRunResult != null && programRunResult.getProcess() != null) {
|
|
|
+ boolean isAlive = programRunResult.getProcess().isAlive();
|
|
|
+ if(! isAlive) {
|
|
|
+ logger.error("程序" + setting.getName() + "启动失败");
|
|
|
+ }
|
|
|
+ setting.setProgramRunResult(programRunResult);
|
|
|
+ serverLogFrame.updateProgramRunResult(settings.indexOf(setting), programRunResult);
|
|
|
+
|
|
|
+ if(index < 9) {
|
|
|
+ // 顺序启动
|
|
|
+ while(setting.isStartSuccess() == false) {
|
|
|
+ ThreadUtil.sleep(1000L);
|
|
|
+ if(startAllThread == null || (startAllThread != null && startAllThread.isInterrupted())) {
|
|
|
+ // 线程被中断,返回
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("程序" + setting.getName() + "启动失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ startAllThread = new Thread(startAllRunnable);
|
|
|
+ startAllThread.start();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 停止所有的服务器
|
|
|
+ * @param settings
|
|
|
+ */
|
|
|
+ public void stopAll(List<ApplicationSetting> settings) {
|
|
|
+ if(startAllThread != null) {
|
|
|
+ startAllThread.interrupt();
|
|
|
+ startAllThread = null;
|
|
|
+ }
|
|
|
+
|
|
|
if (settings != null && settings.size() > 0) {
|
|
|
for(int i = 0; i < settings.size(); i ++) {
|
|
|
final ApplicationSetting setting = settings.get(i);
|
|
|
- ProgramRunResult programRunResult;
|
|
|
try {
|
|
|
- programRunResult = startProgram(setting);
|
|
|
+ ProgramRunResult programRunResult = setting.getProgramRunResult();
|
|
|
if(programRunResult != null && programRunResult.getProcess() != null) {
|
|
|
- boolean isAlive = programRunResult.getProcess().isAlive();
|
|
|
- if(! isAlive) {
|
|
|
- logger.error("程序" + setting.getName() + "启动失败");
|
|
|
- }
|
|
|
- setting.setProgramRunResult(programRunResult);
|
|
|
- serverLogFrame.updateProgramRunResult(i, programRunResult);
|
|
|
+ programRunResult.getProcess().destroyForcibly();
|
|
|
}
|
|
|
- } catch (IOException e) {
|
|
|
- logger.error("程序" + setting.getName() + "启动失败", e);
|
|
|
+ programRunResult = stopProgram(setting);
|
|
|
+ if(programRunResult == null || programRunResult.getProcess() == null) {
|
|
|
+ logger.error(setting.getName() + "程序关闭异常,未配置停止文件。");
|
|
|
+ }else {
|
|
|
+ setting.setProgramRunResult(null);
|
|
|
+ }
|
|
|
+ } catch (IOException e1) {
|
|
|
+ logger.error(setting.getName() +"程序关闭失败", e1);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 获取服务器的状态
|