【问题标题】:mule read single file from classpath during flowmule 在流程中从类路径中读取单个文件
【发布时间】:2012-10-23 14:20:44
【问题描述】:

有没有一种简单的方法来配置 Flow 以一次从类路径中读取单个文件?我不需要轮询文件。我只需要读取一个已知文件并将其内容设置为消息有效负载。

【问题讨论】:

    标签: mule


    【解决方案1】:

    使用 set-payload 消息处理器和 MEL 表达式:

    <set-payload value="#[Thread.currentThread().getContextClassLoader().getResourceAsStream('my-file.abc')]" />
    

    【讨论】:

    • 实际上...这似乎将有效负载设置为 InputStream。有没有简单的方法让它成为一个字符串?
    • 在其两侧添加object-to-string-transformer
    • 我也能做到这一点...
    【解决方案2】:

    由于某种原因,我无法使 David Dossot 提出的解决方案发挥作用。我受到this answer 的启发,想出了另一个解决方案

    <spring:bean id="myResource" class="org.apache.commons.io.IOUtils" factory-method="toString">
        <spring:constructor-arg value="classpath:path/to/myResource.txt" type="java.io.InputStream"/>
    </spring:bean>
    

    然后你可以通过以下方式使用set-payload

    <set-payload value="#[app.registry.myResource]" doc:name="Set Payload"/>
    

    这将导致将文件内容设置为字符串的有效负载。

    此方法的优点是资源文件的内容只加载到 String 类型的 bean 中一次。因此,如果您的 set-payload 语句被频繁执行,这可能会提高性能。

    【讨论】:

    • 大卫·多索的回答也没有成功。此外,这个答案更清晰,更具可读性。这应该是公认的答案。
    【解决方案3】:
        <scripting:component doc:name="Get xls File">                                        
            <scripting:script engine="Groovy"><![CDATA[new File('C:/project/src/main/resources/account.xls').getText('UTF-8')]]></scripting:script>
        </scripting:component>
    

    【讨论】:

      【解决方案4】:
      <parse-template location="my-resurce.txt" doc:name="Load resource as a String"/>
      

      该文件可以完全不包含任何 MEL 表达式,在这种情况下,它实际上是按原样加载的,它成为有效负载。

      【讨论】:

        【解决方案5】:

        从 Mule 3.4 开始,使用解析模板转换器

        <parse-template location="my-resurce.txt" doc:name="Load resource as a String"/>
        

        这将使事情变得容易得多。

        您可能仍需要设置 MIME 类型,具体取决于您将如何使用模板。

        常见错误包括在转换器中使用完整路径,例如 c:\users\myusers\mule\myfile.txt 这样编译不好。

        例如,您还可以在解析模板和富文本中使用 Mule 表达式。

        http://www.mulesoft.org/documentation/display/current/Parse+Template+Reference

        【讨论】:

          【解决方案6】:

          我正在尝试抛出 Java 类转换器,以便您可以使用以下内容

          注意:路径是包含你想要的文件的直接包 读取存储在 mule 变量中的路径,然后读取文件 内容并将其值存储到 Payload 中

          .

          public class PayloadFileReader extends AbstractMessageTransformer {
          public Object transformMessage(MuleMessage message, String outputEncoding)
                  throws TransformerException {
          
              String result = "";
              try {
                  result = readFileTest(message.getInvocationProperty("path")
                          .toString());
              } catch (Exception e) {
                  e.printStackTrace();
              }
              message.setPayload(result);
              return message;
          }
          
          
          public String readFileTest(String path) throws FileNotFoundException,
                  IOException, Exception {
          
          
              ClassLoader classLoader = getClass().getClassLoader();
          
          "+classLoader.getResource("samples/v3/addVal-request.sample").getFile());
          
              File file = new File(classLoader.getResource(path).getFile());
              FileReader fileReader = new FileReader(file);
              BufferedReader bufferReader = null;
              StringBuilder stringBuffer = new StringBuilder();
              String line;
              try {
                  bufferReader = new BufferedReader(fileReader);
                  while ((line = bufferReader.readLine()) != null) {
                      stringBuffer.append(line);
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {
                  if (bufferReader != null) {
                      try {
                          bufferReader.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
              return stringBuffer.toString();
          }
          

          【讨论】:

            【解决方案7】:

            您可以创建一个“Spring Bean”来尝试“导入”文件(假设 Mule XML 配置文件)。示例代码如下:

            <spring:beans>
             <spring:import resource="classpath:sample-mule-integration.xml"/>
            </spring:beans>
            

            或者您可以尝试将文件放在属性占位符中,如下所示

            <context:property-placeholder location="file:E:\NewMuleWorkSpace\springbeanproperties\src\main\resources\property.properties"/>
            

            否则您甚至可以使用带有示例代码的 Groovy 组件来读取文件:

             File file = new File("C://Users//schiraboina//Desktop//123.txt")
            payload=file.getText()  
            

            【讨论】:

              【解决方案8】:

              最干净的解决方案似乎是使用专用的getResource() 方法,如 MUnit documentation.

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-07-18
                • 2015-10-26
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多