1 18 package org.apache.activemq.systest.impl; 19 20 import org.apache.activemq.systest.AgentStopper; 21 import org.apache.activemq.systest.AgentSupport; 22 23 import java.io.BufferedReader ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 28 33 public class SeparateProcessAgent extends AgentSupport { 34 35 private String [] commands; 36 private Process process; 37 private long sleepTime = 10000; 38 39 public SeparateProcessAgent() { 40 } 41 42 public String [] getCommands() { 43 if (commands == null) { 44 commands = createCommand(); 45 } 46 return commands; 47 } 48 49 public void setCommands(String [] command) { 50 this.commands = command; 51 } 52 53 public Process getProcess() { 54 return process; 55 } 56 57 public void start() throws Exception { 58 process = createProcess(); 59 Thread thread = new Thread (new Runnable () { 60 public void run() { 61 readInputStream(process.getInputStream()); 62 } 63 }); 64 thread.start(); 65 66 Thread thread2 = new Thread (new Runnable () { 67 public void run() { 68 waitForProcessExit(); 69 } 70 }); 71 thread2.start(); 72 73 75 System.out.println("Waiting a little while to give the broker process to start"); 76 Thread.sleep(sleepTime); 77 } 78 79 public void stop(AgentStopper stopper) { 80 if (process != null) { 81 try { 82 process.destroy(); 83 } 84 catch (Exception e) { 85 stopper.onException(this, e); 86 } 87 finally { 88 process = null; 89 } 90 } 91 } 92 93 protected Process createProcess() throws Exception { 94 return Runtime.getRuntime().exec(commands); 95 } 96 97 protected String [] createCommand() { 98 throw new IllegalArgumentException ("You must configure the 'commands' property"); 99 } 100 101 protected void readInputStream(InputStream inputStream) { 102 try { 103 BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream)); 104 while (true) { 105 String line = reader.readLine(); 106 if (line == null) { 107 break; 108 } 109 System.out.println(line); 110 } 111 } 112 catch (IOException e) { 113 } 116 } 117 118 protected void waitForProcessExit() { 119 Process p = process; 120 try { 121 p.waitFor(); 122 } 123 catch (InterruptedException e) { 124 System.out.println("Interrupted while waiting for process to complete: " + e); 125 e.printStackTrace(); 126 } 127 int value = p.exitValue(); 128 System.out.println("Process completed with exit value: " + value); 129 130 } 131 } 132 | Popular Tags |