‘liusuyi’
2024-04-12 7de353fa99cfe82101c434b5d080f6a5a1945aac
ard-work/src/main/java/com/ruoyi/cmd/startup.java
@@ -1,171 +1,150 @@
package com.ruoyi.cmd;
import com.ruoyi.utils.process.CmdUtils;
import com.sun.jna.Platform;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Description: 外部程序启动
 * @ClassName: startup
 * @Author: 刘苏义
 * @Date: 2023年09月22日9:56:57
 **/
@Slf4j(topic = "cmd")
@Component
public class startup implements ApplicationContextInitializer {
    //minio
    String minioName = "minio.exe";
    @Value("${minio.accessKey}")
    String accessKey;
    @Value("${minio.secretKey}")
    String secretKey;
    @Value("${minio.path}")
    String path;
    @Value("${minio.enabled}")
    Boolean minioEnabled;
    //mediamtx
    String mediamtxName = "mediamtx.exe";
    @Value("${mediamtx.enabled}")
    Boolean mediamtxEnabled;
    //webrtc-streamer
    String webrtcName = "webrtc-streamer.exe";
    @Value("${webrtc.host}")
    String webrtcHost;
    @Value("${webrtc.enabled}")
    Boolean webrtcEnabled;
    //redis
    String redisName = "redis-server.exe";
    /**
     * 程序初始化启动redis
     * 刘苏义
     * 2023/9/22 10:38:41
     */
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        if (Platform.isWindows()) {
            String exePath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "redis" + File.separator + redisName;
            List<String> cmd = new ArrayList<>();
            cmd.add(exePath);
            if (CmdUtils.isProcessRunning(redisName)) {
                // 进程已经在运行,结束该进程
                CmdUtils.stopProcess(redisName);
            }
            // 启动后台进程
            CmdUtils.commandStart(redisName, cmd, null);
            // 启动cmd窗口
//                String[] command = {"cmd", "/c", "start", exePath, "-H127.0.0.1:8000", "-o"};
//                CmdUtils.commandStart(command);
        }
    }
    @PostConstruct
    public void init() {
        if (minioEnabled) {
            log.info("初始化启动minio");
            if (Platform.isWindows()) {
                String exePath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "minio" + File.separator + minioName;
                Map<String, String> env = new HashMap<>();
                env.put("MINIO_ROOT_USER", accessKey);
                env.put("MINIO_ROOT_PASSWORD", secretKey);
                List<String> cmd = new ArrayList<>();
                cmd.add(exePath);
                cmd.add("server");
                cmd.add(path);
                cmd.add("--console-address=0.0.0.0:9000");
                cmd.add("--address=0.0.0.0:9001");
                if (CmdUtils.isProcessRunning(minioName)) {
                    // 进程已经在运行,结束该进程
                    CmdUtils.stopProcess(minioName);
                }
                // 启动后台进程
                CmdUtils.commandStart(minioName, cmd, env);
                // 启动cmd窗口
                //String[] command = {"cmd", "/c", "start", exePath};
                //CmdUtils.commandStart(command);
            }
        }
        if (mediamtxEnabled) {
            log.info("初始化启动mediaMTX");
            if (Platform.isWindows()) {
                String exePath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "mediamtx" + File.separator + mediamtxName;
                String ymlPath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "mediamtx" + File.separator + "mediamtx.yml";
                List<String> cmd = new ArrayList<>();
                cmd.add(exePath);
                cmd.add(ymlPath);
                if (CmdUtils.isProcessRunning(mediamtxName)) {
                    // 进程已经在运行,结束该进程
                    CmdUtils.stopProcess(mediamtxName);
                }
                // 启动后台进程
                CmdUtils.commandStart(mediamtxName, cmd, null);
                // 启动cmd窗口
//            String[] command = {"cmd","/c","start",exePath,ymlPath};
//            CmdUtils.commandStart(command);
            }
        }
        if (webrtcEnabled) {
            log.info("初始化启动webrtc-streamer");
            if (Platform.isWindows()) {
                String exePath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "webrtc" + File.separator + webrtcName;
                List<String> cmd = new ArrayList<>();
                cmd.add(exePath);
                cmd.add("-H" + webrtcHost);
                cmd.add("-o");
                if (CmdUtils.isProcessRunning(webrtcName)) {
                    // 进程已经在运行,结束该进程
                    CmdUtils.stopProcess(webrtcName);
                }
                // 启动后台进程
                CmdUtils.commandStart(webrtcName, cmd, null);
                // 启动cmd窗口
//                String[] command = {"cmd", "/c", "start", exePath, "-H127.0.0.1:8000", "-o"};
//                CmdUtils.commandStart(command);
            }
        }
    }
    @PreDestroy
    public void destroy() {
        if (minioEnabled) {
            log.info("销毁minio");
            if (CmdUtils.isProcessRunning(minioName)) {
                // 进程已经在运行,结束该进程
                CmdUtils.stopProcess(minioName);
            }
        }
        if (mediamtxEnabled) {
            log.info("销毁mediaMtx");
            if (CmdUtils.isProcessRunning(mediamtxName)) {
                // 进程已经在运行,结束该进程
                CmdUtils.stopProcess(mediamtxName);
            }
        }
        if (webrtcEnabled) {
            log.info("销毁webrtc-streamer");
            if (CmdUtils.isProcessRunning(webrtcName)) {
                // 进程已经在运行,结束该进程
                CmdUtils.stopProcess(webrtcName);
            }
        }
        if (true) {
            log.info("销毁redis");
            if (CmdUtils.isProcessRunning(redisName)) {
                // 进程已经在运行,结束该进程
                CmdUtils.stopProcess(redisName);
            }
        }
    }
}
package com.ruoyi.cmd;
import com.ruoyi.utils.process.CmdUtils;
import com.sun.jna.Platform;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Description: 外部程序启动
 * @ClassName: startup
 * @Author: 刘苏义
 * @Date: 2023年09月22日9:56:57
 **/
@Slf4j(topic = "cmd")
@Component
public class startup implements ApplicationContextInitializer {
    //minio
    String minioName = "minio.exe";
    @Value("${minio.accessKey}")
    String accessKey;
    @Value("${minio.secretKey}")
    String secretKey;
    @Value("${minio.path}")
    String path;
    @Value("${minio.enabled}")
    Boolean minioEnabled;
    //mediamtx
    String mediamtxName = "mediamtx.exe";
    @Value("${mediamtx.enabled}")
    Boolean mediamtxEnabled;
    //redis
    String redisName = "redis-server.exe";
    /**
     * 程序初始化启动redis
     * 刘苏义
     * 2023/9/22 10:38:41
     */
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        if (Platform.isWindows()) {
            String workingDir = System.getProperty("user.dir") + File.separator + "server" + File.separator + "redis";
            String exePath = workingDir +File.separator+ redisName;
            List<String> cmd = new ArrayList<>();
            cmd.add(exePath);
            if (CmdUtils.isProcessRunning(redisName)) {
                // 进程已经在运行,结束该进程
                CmdUtils.stopProcess(redisName);
            }
            // 启动后台进程
            CmdUtils.commandStart(workingDir, redisName, cmd, null);
            // 启动cmd窗口
            //String[] command = {"cmd", "/c", "start", exePath, "-H127.0.0.1:8000", "-o"};
            //CmdUtils.commandStart(command);
        }
    }
    @PostConstruct
    @Order(1)
    public void init() {
        if (minioEnabled) {
            log.info("初始化启动minio");
            if (Platform.isWindows()) {
                String workingDir = System.getProperty("user.dir") + File.separator + "server" + File.separator + "minio";
                String exePath = workingDir + File.separator + minioName;
                Map<String, String> env = new HashMap<>();
                env.put("MINIO_ROOT_USER", accessKey);
                env.put("MINIO_ROOT_PASSWORD", secretKey);
                List<String> cmd = new ArrayList<>();
                cmd.add(exePath);
                cmd.add("server");
                cmd.add(path);
                cmd.add("--console-address=0.0.0.0:9000");
                cmd.add("--address=0.0.0.0:9001");
                if (CmdUtils.isProcessRunning(minioName)) {
                    // 进程已经在运行,结束该进程
                    CmdUtils.stopProcess(minioName);
                }
                // 启动后台进程
                CmdUtils.commandStart(workingDir, minioName, cmd, env);
                // 启动cmd窗口
                //String[] command = {"cmd", "/c", "start", exePath};
                //CmdUtils.commandStart(command);
            }
        }
        if (mediamtxEnabled) {
            log.info("初始化启动mediaMTX");
            if (Platform.isWindows()) {
                String workingDir = System.getProperty("user.dir") + File.separator + "server" + File.separator + "mediamtx";
                String exePath = workingDir + File.separator + mediamtxName;
                String ymlPath = workingDir + File.separator + "mediamtx.yml";
                List<String> cmd = new ArrayList<>();
                cmd.add(exePath);
               // cmd.add(ymlPath);
                if (CmdUtils.isProcessRunning(mediamtxName)) {
                    // 进程已经在运行,结束该进程
                    CmdUtils.stopProcess(mediamtxName);
                }
                // 启动后台进程
                CmdUtils.commandStart(workingDir, mediamtxName, cmd, null);
                // 启动cmd窗口
                //String[] command = {"cmd","/c","start",exePath,ymlPath};
                //CmdUtils.commandStart(command);
            }
        }
    }
    @PreDestroy
    public void destroy() {
        if (minioEnabled) {
            log.info("销毁minio");
            if (CmdUtils.isProcessRunning(minioName)) {
                // 进程已经在运行,结束该进程
                CmdUtils.stopProcess(minioName);
            }
        }
        if (mediamtxEnabled) {
            log.info("销毁mediaMtx");
            if (CmdUtils.isProcessRunning(mediamtxName)) {
                // 进程已经在运行,结束该进程
                CmdUtils.stopProcess(mediamtxName);
            }
        }
        if (true) {
            log.info("销毁redis");
            if (CmdUtils.isProcessRunning(redisName)) {
                // 进程已经在运行,结束该进程
                CmdUtils.stopProcess(redisName);
            }
        }
    }
}