当路由语句“from(file://dir/largefile.txt)”拾取文件时,apache 文件组件将整个文件加载到内存中。问题是文件大小为 300MB,而 kubernetes pod 的内存有限,因此在加载大文件时会导致内存不足错误。
加载文件后,将使用分割来处理文件中的行。 .split(body().tokenize(" ")).streaming()
问题是如何让文件组件不首先将整个文件加载到内存中。
如果有任何其他方法可以与 Apache Camel 配合使用来实现这一点也可以。
在上一节中已解释过
您是否尝试使用流缓存,它使用临时文件,并且不会加载整个文件来处理它?
类似的东西:
@Override
public void configure() throws Exception {
// Enable StreamCache
getContext().setStreamCaching(true);
getContext().getStreamCachingStrategy().setSpoolThreshold(1024L * 1024L); // 1MB threshold
from("file://dir?fileName=largefile.txt&noop=true")
.split(body().tokenize("\n")).streaming()
.process(exchange -> {
String line = exchange.getIn().getBody(String.class);
// Process each line
System.out.println(line);
})
.end();
}