java实现spring boot项目启动时,重启Windows进程

一、方法总述

前5个实现了在spring boot在Windows启动服务时候,杀死相应的进程的;
第6个组成了启动相应的进程的方式。

二、SpringBoot项目启动时自动执行指定方法

在SpringBoot中,有两种接口方式实现启动执行,分别是ApplicationRunner和CommandLineRunner,除了可接受参数不同,其他的大同小异,这里选择随机选择一种,因为不需要接收参数,所以无所谓哪一种;

@Component
@Order(value = 1)
public class start implements ApplicationRunner {
	private static Logger logger = LoggerFactory.getLogger(StartService.class);

	@Override
	public void run(ApplicationArguments args) throws Exception {
		logger.info("spring boot项目启动成功");
	}
	
}

三、查看进程是否存在的方法

根据传入的course进程名,进行判断,如果存在的话,返回true,不存在返回false,供后面kill进程使用;

  /**
     * 查看进程是否存活
     *
     * @param course 进程的名称
     * @return
     * @throws IOException
     */
    public boolean isLive(String course) throws IOException {

        BufferedReader bufferedReader = null;

        try {
            Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + course + '"');
//            将获取到的进程打印出来
            bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream(), "UTF-8"));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
//            如果存在,则返回true
                if (line.contains(course)) {
                    return true;
                }
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
//            关闭资源
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();

                } catch (Exception ex) {

                }
            }
        }
    }

四、杀死Windows进程的方法

根据传入的进程的名称course,去杀死相应的进程;

  /**
     * 杀死进程
     * @param course
     * @return
     * @throws IOException
     */
    public static String killProc(String course) throws IOException {
//        判断进程是否为空
        if (StringUtils.isNotBlank(course)) {
            course = "taskkill /F /IM " + course;
        }
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("cmd /c " + course);
//            将获取到的进程打印出来
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
        String line = null;
        StringBuilder build = new StringBuilder();
        while ((line = br.readLine()) != null) {
            build.append(line);
        }
//        返回关闭的进程
        return build.toString();
    }

五、完整代码

public class start implements ApplicationRunner {

    /**
     * 关闭系统中存在的wps.exe进程
     */
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {

        String course = "wps.exe";

        try {
            //查看进程是否存活
            boolean isLive = isLive(course);
            if (isLive) {
                // 存在,则杀死该进程
                killProc(course);
            }
        } catch (Exception e) {
        //杀死进程失败
            e.printStackTrace();
        }

    }

    /**
     * 查看进程是否存活
     *
     * @param course 进程的名称
     * @return
     * @throws IOException
     */
    public boolean isLive(String course) throws IOException {

        BufferedReader bufferedReader = null;

        try {
            Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + course + '"');
//            将获取到的进程打印出来
            bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream(), "UTF-8"));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
//            如果存在,则返回true
                if (line.contains(course)) {
                    return true;
                }
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
//            关闭资源
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();

                } catch (Exception ex) {

                }
            }
        }
    }

    /**
     * 杀死进程
     * @param course
     * @return
     * @throws IOException
     */
    public static String killProc(String course) throws IOException {
//        判断进程是否为空
        if (StringUtils.isNotBlank(course)) {
            course = "taskkill /F /IM " + course;
        }
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("cmd /c " + course);
//            将获取到的进程打印出来
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
        String line = null;
        StringBuilder build = new StringBuilder();
        while ((line = br.readLine()) != null) {
            build.append(line);
        }
//        返回关闭的进程
        return build.toString();
    }
}

六、实现项目启动重启进程

如果想要实现在项目启动的时候,重启进程的话,只需要在关闭进程结束后,加上启动进程就可以了。

try {
            //查看进程是否存活
            boolean isLive = isLive(course);
            if (isLive) {
                // 存在,则杀死该进程
                killProc(course);
               //启动进程
                startProc(course);
            }else{
         //启动进程
         startProc(course);            
        } catch (Exception e) {
        log.error("重启/杀死提取程序失败。。。");  
        }

%%%%%最关键的:启动进程代码:%%%%%

    /**
     *  启动进程
     * @throws IOException
     */
    public static void startProc(String processName) {
        log.info("启动应用程序:" + processName);
        if (StringUtils.isNotBlank(processName)) {
            try {
                Desktop.getDesktop().open(new File(processName));
            } catch (Exception e) {
                e.printStackTrace();
                log.error("应用程序:" + processName + "不存在!");
            }
        }
    }
posted @ 2019-08-30 21:24  ah_lydms  阅读(400)  评论(0编辑  收藏  举报