异步调用服务器脚本

本文介绍了一个用于Java项目的Shell脚本调用工具类。该工具类支持同步和异步执行Shell命令,并能捕获命令执行的输出及错误信息。通过具体的示例代码展示了如何使用该工具类来执行服务的停止和启动脚本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

脚本调用工具类:

package com.framework.util;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class ShellUtil {
	private static final int SUCCESS = 0;	
    private static final String SUCCESS_MESSAGE = "successful";
    private static final String ERROR_MESSAGE = "fail";
    /**
     * 
    * @Title: executer 
    * @Description: TODO
    * @param command
    * @return
     */
    public static boolean executer(String command){
        try{
        	Process process=Runtime.getRuntime().exec(command);
            readProcessOutput(process);
            // 等待程序执行结束并输出状态
            int exitCode = process.waitFor();
            if (exitCode == SUCCESS) {
                System.out.println(SUCCESS_MESSAGE);
                return true;
            } else {
                System.err.println(ERROR_MESSAGE + exitCode);
            }
        }catch(Exception e){
        	e.printStackTrace();
        }
        return false;
    }
    
    /**
     * 输出打印信息
    * @Title: readProcessOutput 
    * @Description: TODO
    * @param process
     */
    private static void readProcessOutput(final Process process) {
	        // 将进程的正常输出在 System.out 中打印,进程的错误输出在 System.err 中打印
	        read(process.getInputStream(), System.out);
	        read(process.getErrorStream(), System.err);
	    }
    
    private static void  read(InputStream inputStream, PrintStream out) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                out.println(line);
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
 
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    public void runShell(String commend){
 			 ExecutorService cachedThreadPool = Executors.newCachedThreadPool();  
              cachedThreadPool.execute(new Runnable() {  
                  public void run() {  
                  	ShellUtil.executer(commend);//耗时任务
                  }  
              });  
              cachedThreadPool.shutdown();  
 }

}

异步执行:

ShellUtil su = new ShellUtil();
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					System.out.println("--->准备停止服务!");
					su.runShell("/home/apache-tomcat-8.5.31/data/stop_server.sh");
					System.out.println("--->服务将于5s后启动!");
					Thread.sleep(5000);//等待5s
					su.runShell("/home/apache-tomcat-8.5.31/data/start_server.sh");
				} catch (InterruptedException e) {
                    System.out.println("服务重启发生异常!");
					e.printStackTrace();
				}
			}
		}).start();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值