【问题标题】:how to generate multiple copies of the same file using mule如何使用 mule 生成同一文件的多个副本
【发布时间】:2015-09-25 23:32:12
【问题描述】:

有没有办法从入站文件连接器获取文件,并在同一文件夹或不同位置生成同一文件的多个副本(例如 5 个)?我已经尝试使用 Scatter-Gather 组件,但结果并没有达到我的预期。请帮忙?

如果使用 Scatter-Gather 应该可以工作,我该如何编写 MEL 表达式来更改文件名,同时保留原始扩展名?我目前的mule流程如下。

<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>

【问题讨论】:

    标签: mule mule-studio mule-el


    【解决方案1】:

    我认为使用 list 和 foreach 可能是一个解决方案:

    <?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>
    

    这个例子问你文件的数量:

    输出:

    这里是项目:

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

    好运!

    【讨论】:

      【解决方案2】:

      使用下面的 MEL 来获取文件名,然后你可以附加额外的字符串(如果你知道你正在工作的文件的扩展名,那么你可以在最后添加它)

      #[message.inboundProperties['originalFilename']]+'Testing'.txt
      

      如果你不知道,那么你需要先获取扩展名

      <set-variable value="#[org.apache.commons.io.FilenameUtils.getExtension(originalFilename)]" variableName="extension" doc:name="Set extension" />
      

      然后在文件名/模式字段的出站文件中写如下

      #[message.inboundProperties['originalFilename'].substring(0,message.inboundProperties['originalFilename'].lastIndexOf('.'))]_Testing.#[flowVars.extension]
      

      【讨论】:

      • 试过了,但仍然无法设置特定数量的副本。为 MEL 表达式 +1。在其他一些操作中派上用场。赞一个!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多