Search code examples
mulemule-studiomule-el

how to generate multiple copies of the same file using mule


Is there a way to get a file from an inbound File connector, and generate multiple copies (say, 5) of the same file within that same folder or a different location? I have tried it with Scatter-Gather component, but it did not turn out the way I expected. Help, please?

If using Scatter-Gather is supposed to work, how do I write a MEL expression to alter the filename, keeping the original extension? My current mule flow is as follows.

<file:connector name="File_Send" readFromDirectory="C:\Users\AnypointStudio\workspace\MessageOut" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/>
<file:connector name="File_Receive" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/>
<spring:beans>
    <spring:bean id="readFile" class="java.lang.String">
        <spring:constructor-arg>
            <spring:bean class="org.springframework.util.FileCopyUtils" factory-method="copyToByteArray">
                <spring:constructor-arg type="java.io.InputStream" value="${C:\Users\AnypointStudio\workspace\MessageOut\24730717-99a3-4353-bfcc-d19d3ba7f50a.xml}"/>
            </spring:bean>
        </spring:constructor-arg>
    </spring:bean>
</spring:beans>
<flow name="loop_testFlow">
    <file:inbound-endpoint path="C:\Users\AnypointStudio\workspace" connector-ref="File_Send" responseTimeout="10000" doc:name="File"/>
    <object-to-byte-array-transformer doc:name="Object to Byte Array"/>
    <set-variable variableName="file" value="#[app.registry.readFile]" doc:name="Variable"/>
    <scatter-gather doc:name="Scatter-Gather">
        <file:outbound-endpoint path="C:\Users\AnypointStudio\workspace\MessageIn" connector-ref="File_Receive" responseTimeout="10000" doc:name="File"/>
        <file:outbound-endpoint path="C:\Users\AnypointStudio\workspace\MessageIn" connector-ref="File_Receive" responseTimeout="10000" doc:name="File"/>
    </scatter-gather>
</flow>

Solution

  • I think with a list and foreach could be a solution:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
        xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:spring="http://www.springframework.org/schema/beans" xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio"
        xsi:schemaLocation="http://www.mulesoft.org/schema/mule/stdio http://www.mulesoft.org/schema/mule/stdio/current/mule-stdio.xsd
    http://www.mulesoft.org/schema/mule/stdio http://www.mulesoft.org/schema/mule/stdio/3.6/mule-stdio.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
    http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.6/mule.xsd
    http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
    http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd" version="EE-3.6.1">
    
        <stdio:connector name="stdioConnector"
            messageDelayTime="1234" outputMessage="abc" promptMessage="Enter number of files:"
            doc:name="STDIO" />
    
        <flow name="flow">
            <stdio:inbound-endpoint system="IN" connector-ref="stdioConnector" doc:name="STDIO"/>
            <logger message="generating #[payload] files" level="INFO" doc:name="Logger"/>
            <scripting:component doc:name="Groovy">
                <scripting:script engine="Groovy"><![CDATA[
    
                    list = new ArrayList();
    
                    int fin = payload.toInteger()
    
                    (1..fin).each {
                        list.add("file_00${it}.txt")
                    }
    
                    return list
    
                ]]></scripting:script>
            </scripting:component>
            <foreach collection="#[payload]" doc:name="For Each">
                <logger message="#[payload]" level="INFO" doc:name="Logger"/>
                <object-to-string-transformer doc:name="Object to String"/>
                <file:outbound-endpoint path="D:\opt" outputPattern="#[payload]" responseTimeout="10000" doc:name="File outbound"/>
            </foreach>
    
        </flow>
    </mule>
    

    This example ask you the number of files:

    enter image description here

    Output:

    enter image description here

    Here the project:

    https://github.com/jrichardsz/mule-esb-usefull-templates/tree/master/several-output-file

    Luck!