Spring 无法使用http4在camel中处理大型文件
在一个场景中,需要使用rest Web服务,该服务提供一个巨大的文件作为流输出,反之亦然,需要处理流并直接写入文件而不是内存。 服务:Spring 无法使用http4在camel中处理大型文件,spring,spring-boot,apache-camel,spring-restcontroller,camel-http,Spring,Spring Boot,Apache Camel,Spring Restcontroller,Camel Http,在一个场景中,需要使用rest Web服务,该服务提供一个巨大的文件作为流输出,反之亦然,需要处理流并直接写入文件而不是内存。 服务: @RequestMapping(value = "downloadFile", method = RequestMethod.GET) public StreamingResponseBody getSteamingFile(HttpServletResponse response) throws IOException { response.setCo
@RequestMapping(value = "downloadFile", method = RequestMethod.GET)
public StreamingResponseBody getSteamingFile(HttpServletResponse response) throws IOException {
response.setContentType("application/octet-stream");
InputStream inputStream = new FileInputStream(new File("data\\test_big.txt"));
return outputStream -> {
int nRead;
byte[] data = new byte[1024];
System.out.println("Writing some bytes..");
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
outputStream.write(data, 0, nRead);
}
System.out.println("Completed #####");
outputStream.flush();
outputStream.close();
response.flushBuffer();
};
}
消费者路线:
.to("http4://localhost:8080/downloadFile")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
InputStream is = exchange.getIn().getBody(InputStream.class);
File ret = File.createTempFile("loadTest", "tmp");
FileOutputStream fos = new FileOutputStream(ret);
StreamUtils.copy(is, fos);
System.out.println("File Name "+ ret.getName());
is.close();
fos.flush();
fos.close();
}
});
256 JVM在处理300 MB时内存不足,这表明我的路由没有执行到文件的流。您必须启用流缓存读取 .to(“http4://localhost:8080/downloadFile?disableStreamCache=true”) 禁用StreamCache:
确定是否缓存来自Servlet的原始输入流(Camel将该流读取到内存中/溢出到文件、流缓存)缓存中。默认情况下,Camel将缓存Servlet输入流以支持多次读取,以确保Camel可以从流中检索所有数据。但是,您可以将此选项设置为true,例如,当您需要访问原始流时,例如将其直接流式传输到文件或其他持久性存储这仅在将大量有效负载流式传输到磁盘时才有帮助。这将导致相当大的性能损失。原则上,应该可以将所有内容作为流进行处理,从而避免将负载加载到内存(或磁盘上的缓冲区)中。如果您看到消费者代码,它已经将其转储到临时文件中。我认为这只是为了说明问题。毕竟,该文件的基本名称是
loadTest
?感谢您一直以来的回复,最终我通过在http4中启用一个功能获得了解决方案。--http4://localhost:8061?disableStreamCache=trueI尝试使用64 MB RAM和使用的web服务,并能够使用上述disableStreamCache属性写入143 MB文件,该属性性能良好,CPU中可能有一个峰值作为其purley I/O。