使用Apache Camel,如何在已经很大的文件中添加一些行?

问题描述 投票:0回答:1

我实际上有一个包含3个步骤的过程:

  1. 将csv格式化为另一种格式(使用split&aggregator)
  2. 手动添加一些行作为页眉和页脚
  3. 压缩并发送到ftp

测试时所有这些都正常工作,但是后来,我看到当处理大型CSV时,第2步失败。

有关该步骤2的详细信息

步骤2和3的路线

      <route id="tozip">
        <from uri="file:{{path}}/out?readLock=changed"/>
        <setBody>
            <simple>resource:file:{{path}}/conf/HeadTxtFoot.txt</simple>
        </setBody>
         <log message="Compressing message..." loggingLevel="INFO" />
         <marshal ref="gzip" />
         <log message="Sending ${file:name}.gz..." loggingLevel="INFO" />
         <to uri="file:{{path}}/tosend/?fileName=${file:name}.gz" />
      </route>

HeadTxtFoot.txt

<tag>
    <informationDate>${headers.date}</informationDate>
    <files>
    ${body}
    </files>
<tag>

我最近在此路由中输入了350Mb的文件,并且出现此错误

Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[tozip             ] [tozip             ] [file:///u01?readLock=changed                 ] [       459]
[tozip             ] [setBody2          ] [setBody[simple{resource:file:{{path}}/conf/HeadTxtFoot.txt}]                  ] [       459]

Stacktrace
--------------------------------------------------------------------------------------------------------------------------------------- 
org.apache.camel.TypeConversionException: Error during type conversion from type: java.lang.String to the required type: java.lang.String with value [Body is file based: GenericFile[/u01/file.xml]] due java.lang.OutOfMemoryError: Java heap space
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.createTypeConversionException(BaseTypeConverterRegistry.java:667)
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.convertTo(BaseTypeConverterRegistry.java:158)
...
Caused by: org.apache.camel.RuntimeCamelException: java.lang.OutOfMemoryError: Java heap space
    at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1831)
    at org.apache.camel.util.ObjectHelper.invokeMethod(ObjectHelper.java:1410)
    at org.apache.camel.impl.converter.StaticMethodTypeConverter.convertTo(StaticMethodTypeConverter.java:59)
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.doConvertTo(BaseTypeConverterRegistry.java:326)
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.convertTo(BaseTypeConverterRegistry.java:141)
    ... 22 more
Caused by: java.lang.OutOfMemoryError: Java heap space

没有setBody / Simple的相同路线可以正常工作

现在,我的问题是:在文件中添加行以避免错误的最佳方法是什么?

java apache-camel large-files
1个回答
0
投票
您的问题是,您正在将整个文件读入Java内存,因此它耗尽了内存,给您Java堆空间问题。

要解决:请勿将整个文件读入内存。而是使用“流”流式传输文件内容。为此,您可以在路线上设置streaming参数

© www.soinside.com 2019 - 2024. All rights reserved.