Java Asynchronous process runner with inputstream reader using Futuretask -


I'm trying to run an asynchronous process and get its inputstream (if any)

This is my code:

  command line command call = new command call (commands); Executable Service Executor = Exporter. Eneaselinglingthread exeter (); Future & lt; Integer & gt; Task = executor submit (commandCall);  

and this process is run task

  public class command line enforceable & lt; Integer & gt; {Private byte [] output; Private Ent Rated = -1; Private string [] command = null; Process process = null; Public commandcall (string [] commands) exception {this.commands = commands; This.output = null; } Public Zero Blast () {try {if (process! = Null) process.destroy (); } Hold (exception e) {}} public byte [] getByteArray () {return output; } Public int getRetval () {return retval; } @ Override Public Integer Call () throws exceptions {try {process = new ProcessBuilder (commands) .start (); // Here I have to read process input and it must be stored on it. Output // It should be a non-lockable because the process can be retval = process.waitFor () without any output; } Finally {try {if (bos! = Null) bos.close (); } Hold (exception e) {}} return retval; }}  

I can not get the process output, please keep 2 very important things in mind:

  • The process should be resolved because I have timeout management
  • The process of installation can be optional and should not lock the thread for content: Any output can be without one process.
  • UPDATE

    I'm trying this version ... seems to work, but I do not know that it enought strong is.

      @Override public integer call () throws exception {InputStream = tap; Try {Process = new ProcessBuilder (Commands). Start (); = Process.getInputStream (); Int len; Int size = 1024; Bytereon OutputStream Boss = New Bytereunootstream (); Byte [] buf = new byte [shape]; While ((Lane = Reid (buff, 0, size)) = -1 Bose. Written (buff, 0, lane); Output = boss Batteryre (); Retval = process.waitFor (); } Finally {try {if (=!) Is.close (); } Hold (exception e) {}} return retval; }     

    Behaviors looking at this process' Output should be in your thread.

      @Override Public Integer Call () throws exceptions {Thread Output Observer = Faucet; Try (BiteArrayOnputStreamBaos = New Bytereanproststream ()) {this.process = New processbuster (Commands). Start (); OutputObserver = new outputObserver (process.getInputStream (), Baoos); OutputObserver.start (); This.retval = process.waitFor (); This.output = baos.toByteArray (); } Finally {if (Output Observer! = Faucet) OutputObser Endoprint (); } This refund } Private Static OutputAbService Thread {Private Final InputStream Input; Private final outputstream output; OutputOverver (inputstream input, OutputStream output) {this.input = input; This.output = output; } @ Override Public Wide Run (); while (! Thread.interrupted ()) {// Copy bytes input from input here (// handling any exceptions) // Use 3rd party Libby for this // // left here Also make sure to copy the bytes I}}  

    Some notes:

    • I have not tested any code, maybe I made some subtle errors, But the direction should be clear.
    • Typically I can not raise the thread , rather I can implement runnable . I just did not want to confuse things
    • I did not provide the code in an output stream to copy the bytes from the input stream. If you need help on this, do a little searching on the web. You will find many solutions.

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -