Skip to content

minsx framework common shell

Joker edited this page May 19, 2018 · 7 revisions
项目描述:主要用于JAVA与其他语言程序之间的进程调用与交互
主要功能
  •  支持跨平台Shell脚本调用
  •  支持异步和同步调用
  •  支持多行语句调用
  •  支持获取Shell的进程树信息(暂仅支持Windows)
  •  支持对复杂脚本的终止操作(暂仅支持Windows)
  •  支持报错通知(待完善)
  •  支持输出流交互
  •  支持任务依赖(待完善)
  •  支持任务计划(待完善)
简单脚本调用示例
import com.minsx.framework.common.shell.core.Shell;
import org.junit.Test;

public class ShellTest {

    @Test
    public void RunShell() {
        Shell.build("java -jar minsx-authorization-starter-1.0.0.jar")
                .inPath("E:/Temp/ServerRunner/MsAuthServer")
                .charset("UTF-8")
                .logged(true)
                .sync(true)
                .onOut((line, operator) -> {
                    if (line.contains("o.s.web.context.ContextLoader")) {
                        operator.stop();
                    }
                }).run();
    }

}
输出结果
      __  __ _                 _ _ _
 /\\ |  \/  (_)               \ \ \ \
( ( )| \  / |_ _ __  _____  __ \ \ \ \
 \\/ | |\/| | | '_ \/ __\ \/ /  ) ) ) )
     | |  | | | | | \__ \>  <  / / / /
     |_|  |_|_|_| |_|___/_/\_\/_/_/_/
=======================================
 :: Minsx Authorization :: (v1.0.0)
2018-04-04 15:57:08.453  INFO 12720 --- [           main] c.m.a.starter.ApplicationStarter         : Starting ApplicationStarter on JokerPc with PID 12720 (E:\Temp\ServerRunner\MsAuthServer\minsx-authorization-starter-1.0.0.jar started by Joker in E:\Temp\ServerRunner\MsAuthServer)
2018-04-04 15:57:08.463  INFO 12720 --- [           main] c.m.a.starter.ApplicationStarter         : No active profile set, falling back to default profiles: default
2018-04-04 15:57:09.163  INFO 12720 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1ff54e6: startup date [Wed Apr 04 15:57:09 CST 2018]; root of context hierarchy
2018-04-04 15:57:11.321  INFO 12720 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2018-04-04 15:57:12.151  INFO 12720 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2018-04-04 15:57:15.108  INFO 12720 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8693 (http)
2018-04-04 15:57:15.138  INFO 12720 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-04-04 15:57:15.148  INFO 12720 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
2018-04-04 15:57:15.402  INFO 12720 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-04-04 15:57:15.402  INFO 12720 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 6249 ms
复杂脚本调用示例
import com.minsx.framework.common.shell.core.Shell;
import org.junit.Test;

public class ShellTest {

    @Test
    public void RunTextShell() throws InterruptedException {
        StringBuffer text = new StringBuffer("@echo off");
        text.append("\n").append("echo please input your name:");
        text.append("\n").append("set /p name=");
        text.append("\n").append("echo Your name is %name%");

        StringBuffer sb = new StringBuffer("");

        Shell shell = Shell.buildText(text.toString())
                .charset("UTF-8")
                .logged(true)
                .sync(true)
                .preHandler(c -> {
                    System.out.println("begin");
                })
                .onOut((line, operator) -> {
                    sb.append(line);
                    if (line.contains("please input your name")) {
                        //operator.stop();
                        operator.send("Joker");
                    }
                }).onErr((line, operator) -> {
                    System.err.println(line);
                }).postHandler(code -> {
                    System.out.println("end");
                }).whetherSuccess(code -> {
                    boolean result = sb.toString().contains("Your name is Joker");
                    return code == 0 && result;
                }).onException(e -> {
                    System.out.println(e.getMessage());
                });

        shell.run();
        System.out.println(shell.isSuccess());
    }

}
输出结果
begin
please input your name:
Your name is Joker
end
true
以bat脚本文件的方式启动Spring Boot工程,查看当前Shell的进程树信息,并停止当前脚本
import com.alibaba.fastjson.JSON;
import com.minsx.framework.common.shell.core.Shell;
import org.junit.Test;

public class SimpleTest {

    @Test
    public void getProcessInfoTreeAndStop() throws InterruptedException {
        Shell shell = Shell.build("cmd /c Run.bat").inPath("E:\\Temp\\RunTest\\MsAuthServer").sync(false).logged(true);
        shell.run();
        Thread.sleep(25000);
        System.out.println(JSON.toJSONString(shell.getProcessInfoTree()));
        shell.stop();
    }

}
输出结果
{
	"child": [{
		"child": [],
		"commandLine": "\\??\\C:\\Windows\\system32\\conhost.exe 0x4",
		"creationDate": 1526708393617,
		"description": "conhost.exe",
		"executablePath": "C:\\Windows\\system32\\conhost.exe",
		"name": "conhost.exe",
		"parentProcessId": 14748,
		"processId": 6916
	}, {
		"child": [],
		"commandLine": "java  -jar minsx-authorization-starter-1.0.0.jar",
		"creationDate": 1526708431847,
		"description": "java.exe",
		"executablePath": "C:\\ProgramData\\Oracle\\Java\\javapath\\java.exe",
		"name": "java.exe",
		"parentProcessId": 14748,
		"processId": 8936
	}],
	"commandLine": "cmd /c Run.bat",
	"creationDate": 1526708383700,
	"description": "cmd.exe",
	"executablePath": "C:\\Windows\\SYSTEM32\\cmd.exe",
	"name": "cmd.exe",
	"parentProcessId": 10420,
	"processId": 14748
}