如何使用 mule 生成同一文件的多个副本
how to generate multiple copies of the same file using mule
有没有办法从入站文件连接器获取文件,并在同一个文件夹或不同位置生成同一个文件的多个副本(比如 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\MessageOut730717-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>
使用下面的 MEL 获取文件名,然后您可以附加额外的字符串(如果您知道您工作的文件的扩展名,那么您可以在最后添加它)
#[message.inboundProperties['originalFilename']]+'Testing'.txt
如果你不知道那么你需要先获得扩展
<set-variable value="#[org.apache.commons.io.FilenameUtils.getExtension(originalFilename)]" variableName="extension" doc:name="Set extension" />
然后在 fileName/pattern 字段
的出站文件中像下面这样写
#[message.inboundProperties['originalFilename'].substring(0,message.inboundProperties['originalFilename'].lastIndexOf('.'))]_Testing.#[flowVars.extension]
我认为使用列表和 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
运气好!
有没有办法从入站文件连接器获取文件,并在同一个文件夹或不同位置生成同一个文件的多个副本(比如 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\MessageOut730717-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>
使用下面的 MEL 获取文件名,然后您可以附加额外的字符串(如果您知道您工作的文件的扩展名,那么您可以在最后添加它)
#[message.inboundProperties['originalFilename']]+'Testing'.txt
如果你不知道那么你需要先获得扩展
<set-variable value="#[org.apache.commons.io.FilenameUtils.getExtension(originalFilename)]" variableName="extension" doc:name="Set extension" />
然后在 fileName/pattern 字段
的出站文件中像下面这样写#[message.inboundProperties['originalFilename'].substring(0,message.inboundProperties['originalFilename'].lastIndexOf('.'))]_Testing.#[flowVars.extension]
我认为使用列表和 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
运气好!