Correct way to execute external process from a verticle

1,466 views
Skip to first unread message

David Laing

unread,
Sep 28, 2012, 5:46:57 PM9/28/12
to ve...@googlegroups.com
I'm experimenting with a vert.x application that exposes an API to trigger server admin tasks (eg, run a bash script) (https://github.com/mrdavidlaing/pressupbox-api/tree/vert.x-spike).

I hope that greater minds than mine can help me with 2 questions:

1) I currently have:  (a) a HTTP API verticle -> eventbus -> (b) a "run sync command" worker verticle.

Is this the right way to structure things?

2) How do I execute an external command from a Javascript verticle?

My expectation was that I could use Rhino's runCommand() but this fails with:

Exception in JavaScript verticle:
ReferenceError: "runCommand" is not defined.
    at file:/Users/mrdavidlaing/Projects/mrdavidlaing/pressupbox-api/mods/pressupbox.gitpull-v0.1.x/gitpull-main.js:54 (anonymous)
    at core/event_bus.js:56 (anonymous)

(Link takes you to full source code)

Do I have to import something to get runCommand() to work?

Thanks in advance.

:D

Jochen Bedersdorfer

unread,
Sep 28, 2012, 7:14:59 PM9/28/12
to ve...@googlegroups.com
You should be able to use java.lang.ProcessBuilder for example.

var process = new java.lang.ProcessBuilder("myCommand", "myArg1", "myArg2").start();

T.J. Crowder

unread,
Sep 29, 2012, 4:35:41 AM9/29/12
to ve...@googlegroups.com
On Friday, 28 September 2012 22:46:57 UTC+1, David Laing wrote:
My expectation was that I could use Rhino's runCommand() but this fails with:

Rhino and the Rhino shell are different things. Rhino is the ability to use JavaScript within the JVM. The Rhino shell runs JavaScript code in Rhino and adds several things to the global namespace (runCommand, importPackage, print).

As Jochen said, you can use ProcessBuilder[1], or if your needs are simple the old stuff on Runtime[2] still works.

-- T.J.

David Laing

unread,
Sep 29, 2012, 5:46:09 AM9/29/12
to ve...@googlegroups.com
Aha!  Thank you both, that pinpoints where I went wrong.

I'll experiment further with process builder and runtime, and report findings back here to add to te google juice for vert.x
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/vertx/-/8zVu-_l2uLYJ.
To post to this group, send an email to ve...@googlegroups.com.
To unsubscribe from this group, send email to vertx+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/vertx?hl=en-GB.


--
David Laing
Open source @ City Index - github.com/cityindex
http://davidlaing.com
Twitter: @davidlaing

David Laing

unread,
Sep 29, 2012, 1:43:14 PM9/29/12
to ve...@googlegroups.com
All,

I've had success using the following "wrapper function" around Apache Common's Exec functionality (imported by putting the commons-exec-1.1.jar in my module's lib/ folder

/*
 * Execute cmdLine synchronously from workingDirectory.
 * Will kill cmdLine of runs for more than timeoutInMs milliseconds
 * Returns { exitValue: <cmdLine exit value>, output: <what was written to stdOut + stdErr by cmdLine> }
 *
 * Requires: lib/commons-exec-1.1.jar (http://commons.apache.org/exec/)
 */

function runCommand(command, workingDirectory, timeoutInMs) {
   
   
var executor = new org.apache.commons.exec.DefaultExecutor();
   
var cmdLine = org.apache.commons.exec.CommandLine.parse(command);
   
    executor
.setWorkingDirectory(new java.io.File(workingDirectory));
   
   
var watchdog = new org.apache.commons.exec.ExecuteWatchdog(timeoutInMs);
    executor
.setWatchdog(watchdog);
   
   
var outputStream = new java.io.ByteArrayOutputStream();
   
var streamHandler = new  org.apache.commons.exec.PumpStreamHandler(outputStream);
    executor
.setStreamHandler(streamHandler);
   
   
var exitValue = executor.execute(cmdLine);
   
   
return { "exitValue": exitValue, "output": outputStream.toString() };
}

Thanks for the pointers!

:D


On Friday, 28 September 2012 22:46:57 UTC+1, David Laing wrote:
Reply all
Reply to author
Forward
0 new messages