Friday 15 January 2010

ant - How to fetch files from a directory and execute them parallely 5 at a time -


I am using ant to fetch all the files in a directory and only 5 files are parallel and the next 5 to 5 I execute again. The file previously executed must not be executed again.

  & lt; Target name = "parallel test" description = "checking parallel test" & gt; Param = "file" & gt; For & lt; & Lt; Path & gt; & Lt; Fileset dir = "C: / RCBuild3 / k / bin / prd1" & gt; & Lt; Include name = "* .xml" /> & Lt; / Fileset & gt; & Lt; / Path & gt; & Lt; Sequential & gt; & Lt; Antalk target = "paralexactine" & gt; & Lt; Param name = "productfile" value = "@ {file}" /> & Lt; / Antcall & gt; & Lt; / Sequential & gt; & Lt; For / & gt; & Lt; / Target & gt; & Lt; Target name = "paralexactine" & gt; & Lt; Exec dir = "C: / RCBuild3 / k / bin /" executable = "CMD" & gt; & Lt; Arg value = "/ c" /> & Lt; Arg value = "productupload.bat" /> & Lt; Arg value = "- fileName" /> & Lt; Arg value = "$ {productfile}" /> & Lt; / Executive & gt; & Lt; / Target & gt;   

Executes the above code sequentially.

Code first:

  & lt; Fileset dir = "C: / RCBuild3 / ofs / bin / prd1" id = "src.files" & gt; & Lt; Include name = "* .xml" /> & Lt; / Fileset & gt; & Lt; Pathconvert pathsep = "," property = "file.list" refid = "src.files" /> & Lt; List = "$ {file.list}" delimiter = "," param = "file" parallel = "true" threadCount = "5" & gt; & Lt; Sequential & gt; & Lt; Antalk target = "paralexactine" & gt; & Lt; Param name = "productfile" value = "@ {file}" /> & Lt; / Antcall & gt; & Lt; / Sequential & gt; & Lt; For / & gt;   

Explain:

First of all, you create a fileset for all those files that need to be processed.

Then, use pathconvert to convert the file to the property "file.list": filename1.xml, filename2.xml, filename3.xml .

"file.list" will be divided into list by using a comma for the Java code of work (which hides behind your ant file) And loop through the list . For each element in the list , the loop body ( sequential part) will run

parallel indicates to run the loop body with multiple threads for the task, and threadcount at the same time The maximum number of threads running on

Therefore, parallel = true and threadcount = 5 , which you describe, works just as well: 5 files at one time.

No comments:

Post a Comment