2015-09-24 5 views
0

Есть ли способ получить файл из входящего соединителя файла и сгенерировать несколько копий (скажем, 5) одного и того же файла в той же папке или в другом месте? Я попробовал его с компонентом Scatter-Gather, но не получился так, как я ожидал. Помоги пожалуйста?Как сгенерировать несколько копий одного и того же файла с помощью mule

Если предполагается использовать 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> 

ответ

1

Я думаю, со списком и Еогеасп может быть решение:

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

удачи!

1

Использование ниже 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] 
+0

Пробовал это, но все еще не смог установить определенное количество копий. +1 для выражений MEL. Пригоден для нескольких других операций. Престижность! – user3502734

Смежные вопросы