From ant-user-return-10992-apmail-jakarta-ant-user-archive=jakarta.apache.org@jakarta.apache.org Fri Aug 03 17:10:37 2001 Return-Path: Delivered-To: apmail-jakarta-ant-user-archive@jakarta.apache.org Received: (qmail 9589 invoked by uid 500); 3 Aug 2001 17:10:37 -0000 Mailing-List: contact ant-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Reply-To: ant-user@jakarta.apache.org Delivered-To: mailing list ant-user@jakarta.apache.org Delivered-To: moderator for ant-user@jakarta.apache.org Received: (qmail 72931 invoked from network); 3 Aug 2001 15:32:10 -0000 Message-ID: <3B6AC5CB.8090601@sedonacorp.com> Date: Fri, 03 Aug 2001 11:39:55 -0400 From: Matthew Inger User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.2) Gecko/20010701 X-Accept-Language: en-us MIME-Version: 1.0 To: ant-user@jakarta.apache.org Subject: Re: running daemons References: <20010803124152.57485.qmail@web9904.mail.yahoo.com> <3B6AC203.7000405@channelpoint.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Filter-Version: 1.3 (netra1) X-Spam-Rating: h31.sny.collab.net 1.6.2 0/1000/N Paul S. Kilroy wrote: > Lauren Commons wrote: > >> I've looked for the answer to this, but couldn't find >> it. If I missed it in the docs, please let me know... >> I am building and testing (!) an asynchronous >> messaging system (based on JMS). For automated >> testing, I'm looking for a way to start the server in >> it's own process then run a test script and retrieve >> the output for comparison to expected results. >> The problem is how do I have Ant kick off the server >> then move on to the next task (calling a junit test)? >> >> Thanks >> Mr. Lauren Commons >> > I wrote a task to do this. It also (since I needed this also...) lets > you specify standard input to the java process. Its called JavaInput... > > > It entends the Java task to add parameters "spawn" (boolean) this > spawns off the Java code in a seperate thread (daemon), "sleep" (long) > milliseconds to wait before returning (so the daemon has time to > start), and nested "" tags for specifying standard > input. > > Paul > > > >------------------------------------------------------------------------ > >package com.channelpoint.build.ant.publiclib; > >import org.apache.tools.ant.*; >import org.apache.tools.ant.taskdefs.*; > >import java.io.*; > >/** > * This is an enhanced Java task. It can run java programs in another thread, > * pause while they startup, and give them input. > * > * Arguments include: > *
  • > *
      spawn (true or false to create a separate thread for this execution) > *
        sleep (milliseconds to pause before returning) > *
          input (nested ex. ) > * > */ >public class JavaInputTask extends Java { > private Input _input = null; > private boolean _spawn = false; > private long _sleep = 0; > > public void setSpawn( boolean spawn ) { > _spawn = spawn; > } > > public void setSleep( long sleep ) { > _sleep = sleep; > } > > public Input createInput() { > if( _input == null ) { > _input = new Input(); > } > return _input; > } > > public void doit() throws BuildException { > super.execute(); > } > public void execute() throws BuildException { > InputStream old = null; > if( _input != null ) { > old = System.in; > System.setIn( new ByteArrayInputStream( _input.getBytes() ) ); > } > > if( _spawn ) { > Spawn s = new Spawn( this ); > s.start(); > try { > Thread.currentThread().sleep( _sleep ); > } catch ( InterruptedException e ) { > throw new BuildException( e ); > } > } else { > doit(); > } > > if( old != null ) { > System.setIn( old ); > } > } > > public class Spawn extends Thread { > private JavaInputTask _ji; > > public Spawn( JavaInputTask ji ) { > _ji = ji; > } > > public void run() { > _ji.doit(); > } > } > > public class Input { > private StringBuffer _buf = new StringBuffer(); > > public void setLine( String line ) { > _buf.append( line + "\n" ); > } > > public byte[] getBytes() { > return _buf.toString().getBytes(); > } > } >} > > JavaInputTask.java > > Content-Type: > > text/plain > Content-Encoding: > > 7bit > > isn't that a little unsafe? resetting System.in? What if: 1) The thread isn't finished with the input before you set System.in back to it's original value (say for instance if it processes one line of input at a time, and then does some io over a slow network, though this is a bad design in general, but could happen)? 2) You have multiple threads running which use standard input (though unlikely, it could happen)? The first one is really the one to worry about. I would think it's safer to just pass input like that in a file, or as command line arguments. Just my $.02 -- Matt Inger (matt.inger@sedonacorp.com) Sedona Corporation 455 S. Gulph Road, Suite 300 King of Prussia, PA 19406 (484) 679-2213 "Self-respect - the secure feeling that no one, as yet, is suspicious." -H.L. Mencken