HI,
attached is a tentative patch that allow a Task object to run asychronously. I have tested the patch on Solaris and win2k and everything works fine (without breaking backward compatibility).
To use the functionality, you just have to add to a task the spawn attribute, e.g.:
<exec dir="c:/src/jakarta-tomcat-5/build/bin/" executable="sh" spawn="true">
Since some task needs a couple of seconds to start/stop, I also added a spawnWaitUntil attribute where you can set the time required before moving to the next Task.
I did no wrote a <spawn> task because I think the functionality should be available to every Task. For now I did not add the functionality to Java, Javac, Jar.
Let me know what you think. At least I have something use when <gump>ing tomcat :-)
Thanks,
-- Jeanfrancois
Index: src/main/org/apache/tools/ant/Task.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/Task.java,v retrieving revision 1.36 diff -u -r1.36 Task.java --- src/main/org/apache/tools/ant/Task.java 22 Aug 2002 17:32:21 -0000 1.36 +++ src/main/org/apache/tools/ant/Task.java 21 Jan 2003 20:14:49 -0000 @@ -119,6 +119,19 @@ * its type. */ private boolean invalid; + + /** + * Spawn a new process without waiting for the process to comnplete + * This attribute implements asynchronous task behaviour. + */ + protected boolean spawnProcess = false; + + + /** + * Let a spawning task start properly before moving to another task. + */ + protected long spawnWaitUntil = 500; + /** Sole constructor. */ public Task() { @@ -194,6 +207,44 @@ */ public String getDescription() { return description; + } + + + /** + * Launch this execution without waiting for the process to complete. + * @param spawnProcess true if exec should launch the process and move to + * the next task + * false if exec should wait before moving to the next + * process (false by default) + */ + public void setSpawn(boolean spawnProcess){ + this.spawnProcess = spawnProcess; + } + + /** + * Return true if the process can be spawned. + * @return true if the process can be spawned. + */ + public boolean getSpawn(){ + return spawnProcess; + } + + /** + * Time required before moving to the next process. + * @param spawnWaitUntil time, in milisecond, before executing the next + * process. + */ + public void setSpawnWaitUntil(long spawnWaitUntil){ + this.spawnWaitUntil = spawnWaitUntil; + } + + /** + * Return the time before a process will be launched when spawn + * attribute is set. + * @return the time before a process will be launched + */ + public long getSpawnWaitUntil(){ + return spawnWaitUntil; } /** Index: src/main/org/apache/tools/ant/taskdefs/ExecTask.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ExecTask.java,v retrieving revision 1.42 diff -u -r1.42 ExecTask.java --- src/main/org/apache/tools/ant/taskdefs/ExecTask.java 25 Jul 2002 15:21:04 -0000 1.42 +++ src/main/org/apache/tools/ant/taskdefs/ExecTask.java 21 Jan 2003 20:14:49 -0000 @@ -97,7 +97,8 @@ private String resultProperty; private boolean failIfExecFails = true; private boolean append = false; - + + /** * Controls whether the VM (1.3 and above) is used to execute the * command @@ -306,6 +307,9 @@ exe.setAntRun(getProject()); exe.setWorkingDirectory(dir); exe.setVMLauncher(vmLauncher); + exe.setSpawnProcess(getSpawn()); + exe.setSpawnWaitUntil(getSpawnWaitUntil()); + String[] environment = env.getVariables(); if (environment != null) { for (int i = 0; i < environment.length; i++) { Index: src/main/org/apache/tools/ant/taskdefs/Execute.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Execute.java,v retrieving revision 1.48 diff -u -r1.48 Execute.java --- src/main/org/apache/tools/ant/taskdefs/Execute.java 27 Sep 2002 08:18:51 -0000 1.48 +++ src/main/org/apache/tools/ant/taskdefs/Execute.java 21 Jan 2003 20:14:50 -0000 @@ -73,6 +73,7 @@ * * @author [EMAIL PROTECTED] * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Tulley</a> + * @author [EMAIL PROTECTED] * * @since Ant 1.2 * @@ -91,6 +92,17 @@ private File workingDirectory = null; private Project project = null; private boolean newEnvironment = false; + + /** + * Spawn a new process without waiting for the process to comnplete + * This attribute implements asynchronous task behaviour. + */ + private boolean spawnProcess = false; + + /** + * Let a spawning task start properly before moving to another task. + */ + private long spawnWaitUntil = 500; /** Controls whether the VM is used to launch commands, where possible */ private boolean useVMLauncher = true; @@ -396,6 +408,26 @@ public void setVMLauncher(boolean useVMLauncher) { this.useVMLauncher = useVMLauncher; } + + /** + * Launch this execution without waiting for the process to complete. + * @param spawnProcess true if exec should launch the process and move to + * the next task + * false if exec should wait before moving to the next + * process (false by default) + */ + public void setSpawnProcess(boolean spawnProcess){ + this.spawnProcess = spawnProcess; + } + + /** + * Time required before moving to the next process. + * @param spawnWaitUntil time, in milisecond, before executing the next + * process. + */ + public void setSpawnWaitUntil(long spawnWaitUntil){ + this.spawnWaitUntil = spawnWaitUntil; + } /** * Creates a process that runs a command. @@ -431,11 +463,14 @@ final Process process = launch(project, getCommandline(), getEnvironment(), workingDirectory, useVMLauncher); - + try { streamHandler.setProcessInputStream(process.getOutputStream()); streamHandler.setProcessOutputStream(process.getInputStream()); streamHandler.setProcessErrorStream(process.getErrorStream()); + if ( spawnProcess ){ + streamHandler.setWaitUntilStoping(spawnWaitUntil); + } } catch (IOException e) { process.destroy(); throw e; @@ -449,8 +484,22 @@ if (watchdog != null) { watchdog.start(process); } - waitFor(process); - + + // Do not use the Process.waitFor since it doesn't work the same way + // depending on the patform it run on. + if ( spawnProcess ) { + try{ + // Give the asych task a chance to start before moving on. + synchronized(this){ + wait(spawnWaitUntil); + } + } catch (java.lang.Exception ex){ + ex.printStackTrace(); + } + } else { + waitFor(process); + } + // remove the process to the list of those to destroy if the VM exits // processDestroyer.remove(process); @@ -458,14 +507,16 @@ if (watchdog != null) { watchdog.stop(); } + streamHandler.stop(); if (watchdog != null) { watchdog.checkException(); } + return getExitValue(); } - - protected void waitFor(Process process) { + + protected void waitFor(final Process process) { try { process.waitFor(); setExitValue(process.exitValue()); Index: src/main/org/apache/tools/ant/taskdefs/ExecuteStreamHandler.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ExecuteStreamHandler.java,v retrieving revision 1.5 diff -u -r1.5 ExecuteStreamHandler.java --- src/main/org/apache/tools/ant/taskdefs/ExecuteStreamHandler.java 25 Jul 2002 15:21:04 -0000 1.5 +++ src/main/org/apache/tools/ant/taskdefs/ExecuteStreamHandler.java 21 Jan 2003 20:14:50 -0000 @@ -98,4 +98,10 @@ * Stop handling of the streams - will not be restarted. */ void stop(); + + + /** + * Wait this value before stoping thr process. + */ + void setWaitUntilStoping(long waitUntil); } Index: src/main/org/apache/tools/ant/taskdefs/JikesOutputParser.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/JikesOutputParser.java,v retrieving revision 1.13 diff -u -r1.13 JikesOutputParser.java --- src/main/org/apache/tools/ant/taskdefs/JikesOutputParser.java 25 Jul 2002 15:21:05 -0000 1.13 +++ src/main/org/apache/tools/ant/taskdefs/JikesOutputParser.java 21 Jan 2003 20:14:50 -0000 @@ -203,4 +203,13 @@ protected boolean getErrorFlag() { return errorFlag; } + + /** + * Wait this value before stoping thr process. + */ + public void setWaitUntilStoping(long waitUntil) { + // Not used + } + + } Index: src/main/org/apache/tools/ant/taskdefs/ProcessDestroyer.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ProcessDestroyer.java,v retrieving revision 1.4 diff -u -r1.4 ProcessDestroyer.java --- src/main/org/apache/tools/ant/taskdefs/ProcessDestroyer.java 15 Apr 2002 13:36:17 -0000 1.4 +++ src/main/org/apache/tools/ant/taskdefs/ProcessDestroyer.java 21 Jan 2003 20:14:50 -0000 @@ -126,4 +126,5 @@ } } } + } Index: src/main/org/apache/tools/ant/taskdefs/PumpStreamHandler.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/PumpStreamHandler.java,v retrieving revision 1.7 diff -u -r1.7 PumpStreamHandler.java --- src/main/org/apache/tools/ant/taskdefs/PumpStreamHandler.java 25 Jul 2002 15:21:05 -0000 1.7 +++ src/main/org/apache/tools/ant/taskdefs/PumpStreamHandler.java 21 Jan 2003 20:14:50 -0000 @@ -74,6 +74,8 @@ private OutputStream out, err; + private long waitUntil = 0; + public PumpStreamHandler(OutputStream out, OutputStream err) { this.out = out; this.err = err; @@ -99,6 +101,10 @@ public void setProcessInputStream(OutputStream os) { } + + public void setWaitUntilStoping(long waitUntil){ + this.waitUntil = waitUntil; + } public void start() { @@ -108,12 +114,9 @@ public void stop() { - try { - inputThread.join(); - } catch (InterruptedException e) {} - try { - errorThread.join(); - } catch (InterruptedException e) {} + join(inputThread); + join(errorThread); + try { err.flush(); } catch (IOException e) {} @@ -136,6 +139,16 @@ protected void createProcessErrorPump(InputStream is, OutputStream os) { errorThread = createPump(is, os); + } + + private void join(Thread thread){ + try { + if (waitUntil > 0) { + thread.join(waitUntil); + } else { + thread.join(); + } + } catch (InterruptedException e) {} } Index: src/main/org/apache/tools/ant/taskdefs/optional/ccm/CCMCreateTask.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ccm/CCMCreateTask.java,v retrieving revision 1.12 diff -u -r1.12 CCMCreateTask.java --- src/main/org/apache/tools/ant/taskdefs/optional/ccm/CCMCreateTask.java 25 Jul 2002 15:21:12 -0000 1.12 +++ src/main/org/apache/tools/ant/taskdefs/optional/ccm/CCMCreateTask.java 21 Jan 2003 20:14:50 -0000 @@ -362,6 +362,13 @@ } // end of try-catch } + + /** + * Wait this value before stoping thr process. + */ + public void setWaitUntilStoping(long waitUntil) { + // Not used + } } Index: src/main/org/apache/tools/ant/taskdefs/optional/ejb/BorlandDeploymentTool.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ejb/BorlandDeploymentTool.java,v retrieving revision 1.20 diff -u -r1.20 BorlandDeploymentTool.java --- src/main/org/apache/tools/ant/taskdefs/optional/ejb/BorlandDeploymentTool.java 8 Nov 2002 16:37:44 -0000 1.20 +++ src/main/org/apache/tools/ant/taskdefs/optional/ejb/BorlandDeploymentTool.java 21 Jan 2003 20:14:51 -0000 @@ -536,5 +536,12 @@ log("[java2iiop] " + s, Project.MSG_DEBUG); } // end of if () } + + /** + * Wait this value before stoping thr process. + */ + public void setWaitUntilStoping(long waitUntil) { + // Not used + } } Index: src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java,v retrieving revision 1.7 diff -u -r1.7 MAuditStreamHandler.java --- src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java 25 Jul 2002 15:21:17 -0000 1.7 +++ src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java 21 Jan 2003 20:14:51 -0000 @@ -255,4 +255,11 @@ violations.addElement(entry); } + + /** + * Wait this value before stoping thr process. + */ + public void setWaitUntilStoping(long waitUntil) { + // Not used + } } Index: src/main/org/apache/tools/ant/taskdefs/optional/metamata/MMetricsStreamHandler.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MMetricsStreamHandler.java,v retrieving revision 1.9 diff -u -r1.9 MMetricsStreamHandler.java --- src/main/org/apache/tools/ant/taskdefs/optional/metamata/MMetricsStreamHandler.java 25 Jul 2002 15:21:17 -0000 1.9 +++ src/main/org/apache/tools/ant/taskdefs/optional/metamata/MMetricsStreamHandler.java 21 Jan 2003 20:14:52 -0000 @@ -354,6 +354,13 @@ return indent; } } + + /** + * Wait this value before stoping thr process. + */ + public void setWaitUntilStoping(long waitUntil) { + // Not used + } } class MetricsElement { @@ -441,5 +448,8 @@ } return new MetricsElement(indent, name, metrics); } + + + }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>