commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Strachan" <james_strac...@yahoo.co.uk>
Subject Re: [Jelly] ThreadTag
Date Wed, 26 Jun 2002 09:16:56 GMT
Thanks Vinay!

I made a minor change to your patch before committing it. I think 2 seperate
threads writing to the same XMLOutput (SAX ContentHandler) is kinda scary.
Never mind threading issues, it seems far too likely that the generated SAX
events would generate non-well-formed XML, since tags would usually
intermingle with each other. e.g.

<!-- invalid XML... -->
<thread1>
    <thread2>
    </thread1>
</thread2>

So I've made it mandatory that an XMLOutput instance is passed into the
<thread> tag. Also there's an additional helper property file="" which
creates an XMLOutput from a file (pretty much as you had it before). So it
should be easy for folks to fire off async blocks, pipe the output to a file
or buffer etc. It also means that I don't need to patch the
XMLOutput.close() methods which could leave streams open if a thread
terminates unexpectantly.

One other change I'm tempted not to make as well is the join() logic in
JellyContext. I think thats up to the 'engine' or container that invokes the
Jelly script. For example, Ant does stuff like this anyways. Let me know if
you disagree. Leaving this change out just makes the code a bit cleaner.

I added the build target 'demo.thread' to run your sample.

Going forward, a reusable ThreadPool object that allows Runnable objects to
be dispatched onto it for asynchronous execution, would be really nice to
have in the commons somewhere ;-)

Thanks again for the patch.

James
----- Original Message -----
From: "Vinay Chandran" <sahilvinay@yahoo.com>
To: "Jakarta Commons Developers List" <commons-dev@jakarta.apache.org>
Sent: Wednesday, June 26, 2002 6:47 AM
Subject: [Jelly] ThreadTag


> Hi,
> Attached here is Thread Tag implementation +
> patches for existing classes along with a test
> sample illustrating the same.
>
> Regards,
> Vinay Chandran
>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! - Official partner of 2002 FIFA World Cup
> http://fifaworldcup.yahoo.com


----------------------------------------------------------------------------
----


> Index: JellyContext.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/je
lly/JellyContext.java,v
> retrieving revision 1.13
> diff -r1.13 JellyContext.java
> 64d63
> < import java.io.File;
> 67a67
> > import java.util.ArrayList;
> 120a121,122
> >     /**Place Holder for spawned Threads */
> >     private ArrayList listOfThreads=new ArrayList();
> 618a621,646
> >     }
> >
> >     /**
> >      *  startThread starts a thread
> >      */
> >     public void startThread(String name, Thread work)
> >     {
> >         if(name==null)
> >             name="ThreadTag-Thread["+listOfThreads.size()+"]";
> >         work.setName(name);
> >         listOfThreads.add(work);
> >         //Start the Thread
> >         work.start();
> >     }
> >
> >     /**
> >      * Close method
> >      *   . This method  waits for all the spawned Threads to finish
their work
> >      */
> >     public void close() throws InterruptedException
> >     {
> >         int numberOfThreads=listOfThreads.size();
> >         for(int i=0;i<numberOfThreads;i++)
> >         {
> >             ((Thread)listOfThreads.get(i)).join();
> >         }
>
> Index: tags/core/CoreTagLibrary.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/je
lly/tags/core/CoreTagLibrary.java,v
> retrieving revision 1.11
> diff -r1.11 CoreTagLibrary.java
> 96a97
> >         registerTag("thread", ThreadTag.class);
>
> Index: task/JellyTask.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/je
lly/task/JellyTask.java,v
> retrieving revision 1.5
> diff -r1.5 JellyTask.java
> 25d24
> <
> 28,29d26
> <
> < import org.apache.tools.ant.Task;
> 31,35c28
> < import org.apache.tools.ant.Project;
> < import org.apache.tools.ant.DirectoryScanner;
> < import org.apache.tools.ant.types.FileSet;
> < import org.apache.tools.ant.types.FilterSet;
> < import org.apache.tools.ant.util.FileUtils;
> ---
> > import org.apache.tools.ant.Task;
> 77d69
> <
> 82c74,79
> <             getXMLOutput().close();
> ---
> >
> >             //Cleanup
> >             getXMLOutput().flush();//Flush the existing output
> >             context.close();       //clean-up resources (eg: Waits for
spawned threads  to stop)
> >             getXMLOutput().close(); //Close the stream Finally
> >
>
> Index: build.xml
> ===================================================================
> RCS file: /home/cvspublic/jakarta-commons-sandbox/jelly/build.xml,v
> retrieving revision 1.56
> diff -r1.56 build.xml
> 287a288,293
> >       <target name="thread.hw" depends="compile"
> >       description="Runs the Hello World demo">
> >
> > <jelly file="src/test/org/apache/commons/jelly/testThread.jelly"/>
> >
> >    </target>
>


----------------------------------------------------------------------------
----


> <?xml version="1.0"?>
>
> <j:jelly xmlns:j="jelly:core" xmlns:x="jelly:xml">
> <j:thread name="thread1" output="test1">
>   <x:parse var="doc">
>     <web-app>
>       <servlet>
>         <servlet-name>snoop</servlet-name>
>         <servlet-class>SnoopServlet</servlet-class>
>       </servlet>
>       <servlet>
>         <servlet-name>file</servlet-name>
>         <servlet-class>ViewFile</servlet-class>
>         <init-param>
>           <param-name>initial</param-name>
>           <param-value>1000</param-value>
>           <description>
>             The initial value for the counter  <!-- optional -->
>           </description>
>         </init-param>
>       </servlet>
>       <servlet-mapping>
>         <servlet-name>snoop</servlet-name>
>         <url-pattern>/foo/snoop</url-pattern>
>       </servlet-mapping>
>     </web-app>
>   </x:parse>
>
>   <x:set var="count" select="count($doc//servlet)"/>
>   <x:forEach var="s" select="$doc//servlet">
>     name: <x:expr select="$s/servlet-name"/>
>     class: <x:expr select="$s/servlet-class"/>
>   </x:forEach>
>
>   There are <x:expr select="$count"/> servlet(s)
> </j:thread>
>  <!-- a really silly way to do this but -->
>   <!-- this should test the choose/when/otherwise tags -->
>   <forEach var="iter" items="System.getProperties()">
>     <choose>
>       <when test='iter.key.equals( "java.home" )'>
>         Java Home = <expr value="iter.value"/>
>       </when>
>       <when test='iter.key.equals( "java.version" )'>
>         Java Version = <expr value="iter.value"/>
>       </when>
>       <otherwise>
>         <expr value="iter.key"/> = <expr value="iter.value"/>
>       </otherwise>
>     </choose>
>   </forEach>
> </j:jelly>
>
>
>
>
>


----------------------------------------------------------------------------
----


> --
> To unsubscribe, e-mail:
<mailto:commons-dev-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
<mailto:commons-dev-help@jakarta.apache.org>


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message