Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whats the best way to stream the output? #38

Closed
jasw opened this issue Dec 15, 2015 · 4 comments
Closed

Whats the best way to stream the output? #38

jasw opened this issue Dec 15, 2015 · 4 comments

Comments

@jasw
Copy link

jasw commented Dec 15, 2015

Hi there,

The shell.exec interface can take a outputStream to get the returning response,

    int exec(String command, InputStream stdin,
        OutputStream stdout, OutputStream stderr) throws IOException;

I have used a few different OutputStream implementations trying to be able to read the output as the output comes in. I could easily pipe the output into input stream, but I found I could not read from input until the output is finished.

As an example, thinking if we are executing a long running or never ending command such as tail, of top, is there a way to be able to continuously feed the return?

I am thinking from a reactive programming point of view, if Jcabi can provide an API to do call backs for every line of response available.

If there are alternatives, please let me know.

Many thanks,
Jason

@jasw
Copy link
Author

jasw commented Dec 15, 2015

It turns out once I use the piped in/out stream with another thread, I could get 'live' streaming response back.

Just post my example here in case some one else needs it

       PipedOutputStream out = new PipedOutputStream();
        PipedInputStream in = new PipedInputStream(out);
        Thread thread = new Thread(() -> {
            try {
                shell.exec(command, new NullInputStream(0L), out, new NullOutputStream());
            } catch (IOException e) {
                log.error("command has failed",e);
            }

        });
        thread.start();
        Reader reader = new InputStreamReader(in);
        //rxjava-string makes this easier. 
        StringObservable.byLine(StringObservable.from(reader)).forEach(s -> log.info(s));

You can close this issue now.
Thanks,
Jason

@dmarkov
Copy link

dmarkov commented Dec 18, 2015

@yegor256 take a look at this issue please and dispatch it somehow, see par.21

@yegor256
Copy link
Member

@jasw thanks for submitting it and your example. please, close the issue (you're the author, it's better if you do it). thanks!

@jasw jasw closed this as completed Apr 17, 2017
@0crat
Copy link

0crat commented May 2, 2017

Oops! Job gh:jcabi/jcabi-ssh#38 was not in scope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants