如何使用mule

时间:2015-09-24 05:43:12

标签: mule mule-studio mule-el

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

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

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

2 个答案:

答案 0 :(得分:1)

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

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

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

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

然后在fileName / pattern field的文件出站中写如下所示

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

答案 1 :(得分:1)

我认为列表和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>

此示例询问您文件的数量:

enter image description here

输出:

enter image description here

这个项目:

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

运气!