Search code examples
antant-contrib

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


I'm Using to ant to fetch all files in a directory and execute only 5 files parallely and next 5 five again. Already executed file should not be executed again.

<target name="ParallelTest" description="Checking the parallelTest">
        <for param="file" >
            <path> 
                <fileset dir="C:/RCBuild3/ofs/bin/prd1">
                        <include name="*.xml"/> 
                </fileset>
            </path>
            <sequential>
                <antcall target="parallelexecutoin">
                    <param name="productfile" value="@{file}"/>
                </antcall>
            </sequential>
        </for> 

    </target>
    <target name="parallelexecutoin">
        <exec dir="C:/RCBuild3/ofs/bin/" executable="cmd">
            <arg value="/c"/>
            <arg value="productupload.bat"/>
            <arg value="-fileName"/>
            <arg value="${productfile}"/>
        </exec>
    </target>    

The Above code executes sequentially .


Solution

  • Code first:

    <fileset dir="C:/RCBuild3/ofs/bin/prd1" id="src.files">
        <include name="*.xml"/> 
    </fileset>
    
    <pathconvert pathsep="," property="file.list" refid="src.files"/>
    
    <for list="${file.list}" delimiter="," param="file" parallel="true" threadCount="5">
        <sequential>
            <antcall target="parallelexecutoin">
                <param name="productfile" value="@{file}"/>
            </antcall>
        </sequential>
    </for>
    

    Explain:

    First of all, you prepare a fileset for all the files that need to be processed.

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

    for task's java code (which hides behind your Ant file) will split "file.list" using comma into a List, and loop through the List. For each element in the List, the loop body (sequential part) will run.

    parallel tells for task to run the loop body with multiple threads, and threadcount is the maximium number of threads that could be running at the same time.

    So with parallel = true and threadcount = 5, is acts exactly as what you described: 5 files a time.