Java怎么调用script
Java如何调用script
在Java 中如何调用各种脚本(shell,perl,python...)? 我们肯定会想到使用 Runtime.getRuntime().exec(cmd). 不过实际中根据需求会有更多更复杂的处理。 当有很多的脚本需要任务调度, 现在给粗略出一种解决办法 。
1. 开启一个服务端(单独的线程)专门来接受脚本
public class ScriptRunnerService { private static int port = 4444; public static void main(String[] args) { ScriptRunnerService srs = new ScriptRunnerService(); String hostIp = args[0]; String hostPort = args[1]; srs.go(hostIp, hostPort); } public void go(String hostIp, String hostPort) { try { ServerSocket listener = new ServerSocket(Integer.valueOf(hostPort), 0, InetAddress.getByName(hostIp)); Socket clientSocket; while (true) { clientSocket = listener.accept(); Thread t = new Thread(new ServiceRunner(clientSocket)); t.start(); } } catch (IOException ioe) { System.out.println("IOException on socket listen: " + ioe); } finally { } } class ServiceRunner implements Runnable { private Socket clientSocket; ServiceRunner(Socket clientSocket) { this.clientSocket = clientSocket; } @Override public void run() { int result = 1; String line; StringBuilder command = new StringBuilder(); BufferedReader br = null; BufferedWriter bw = null; try { System.out.print("command receiving..."); br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); if ((line = br.readLine()) != null) { command.append(line); } bw = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream())); System.out.println(command); Process p = Runtime.getRuntime().exec(command.toString()); result = p.waitFor(); p.destroy(); write(bw, command.toString() + "\r\n"); } catch (InterruptedException ie) { write(bw, ie.getMessage() + "\r\n"); } catch (IOException ioe) { write(bw, ioe.getMessage() + "\r\n"); } finally { write(bw, "..Result.." + result + "\r\n"); write(bw, "..Command.." + command.toString() + "\r\n"); write(bw, "Host...." + clientSocket.getInetAddress().getHostAddress() + "\r\n"); try { clientSocket.close(); } catch (IOException ioe) { ioe.printStackTrace(); } System.out.println("\t\tcommand finished with result: " + result); } } } private void write(BufferedWriter bw, String msg) { try { bw.write(msg); bw.flush(); } catch (IOException ioe) { ioe.printStackTrace(); } } }
public class AppInitializer{ public void execScriptService(){ String[] envParams = {"java -cp";, completeClasspath, ScriptRunnerService.class.getName(),“127.0.0.1”,“4445”}; ScriptRunnerService scriptRunnerProcess = Runtime.getRuntime().exec(envParams); }
2. 客户端专门接受服务端的返回消息
public class SocketTransport { protected Logger log = new Logger(SocketTransport.class.getName()); public String query(String host, int port, String queryString) throws IOException { String line; BufferedReader rd = null; BufferedWriter wr = null; Socket serverSocket = null; StringBuilder response = new StringBuilder(); try { serverSocket = new Socket(InetAddress.getByName(host), port); wr = new BufferedWriter(new OutputStreamWriter(serverSocket.getOutputStream(), "UTF8")); log.warn("sending request: %s to %s:%s ", queryString, host, port); wr.write(queryString + "\r\n"); wr.flush(); rd = new BufferedReader(new InputStreamReader(serverSocket.getInputStream())); while ((line = rd.readLine()) != null) { response.append(line); } } catch (UnknownHostException e) { log.error(e); throw new IOException(e); } finally { IOUtils.closeQuietly(wr); IOUtils.closeQuietly(rd); if (serverSocket != null) { serverSocket.close(); } } log.warn("finish request: ", queryString); return response.toString(); } }
3. 执行脚本
public class ScriptTest { public static void execute(String path , String script, String param) throws IOException, InterruptedException{ if(...){ SocketTransport st = new SocketTransport(); String msg = st.query("127.0.0.1", 4445, "java -version"); System.out.println(msg); else{ Process p = Runtime.getRuntime().exec(path + script + params); System.out.println("Executing for "+type+": " + path + script + params); p.waitFor(); p.destroy(); } } }