2014-07-21 32 views
0

我想實現一個內存有效的HTTP反向代理,只處理流。 Jetty消費者將輸入流放入交換中,我可以將其與http生產者掛鉤以轉發請求。那裏沒問題。但是,我知道的所有http生產者(Jetty,http4,netty-http)都會將響應流讀入堆內存,並以某種形式將其內容放入交換中,而不是處理該流。而且他們都沒有提供讓他們這樣做的選擇。駱駝反向代理 - 沒有響應流緩存

我發現這個thread它描述了同樣的問題,並且還提出了一個解決方案。但是看看Camel 2.13.1中的http4 HttpProducer的代碼,畢竟它看起來並不像所提議的更改成爲Camel代碼庫。

有什麼辦法可以用駱駝來實現只有流的方法嗎?

http4: - 對其他信息的確切位置輸入流中的記憶結束了

<route id="reverse_proxy" streamCache="false"> 
    <from ref="jetty.http.server"/> 
    <bean ref="streamHolder" method="enableCaching"/> 
    <bean ref="streamHolder" method="parsePayloadHeaderInfoAndDoStuff"/> 
    <bean ref="streamHolder" method="resetStream"/> 
    <to ref="http.client"/> <!-- Register completion synchronization hook to close stream. --> 
    <bean ref="streamHolder" method="enableCaching"/> 
    <bean ref="streamHolder" method="parsePayloadResponseHeaderAndDoStuff"/> 
    <bean ref="streamHolder" method="resetStream"/> 
</route> 

編輯:所以,用最小的內存佔用就可以沿着這行做一些事情:一切都發生在org.apache.camel.component.http4.HttpProducer :: process() - > populateResponse(..) - > extractResponseBody(..) - >doExtractResponseBodyAsStream();這裏原始流被複制到CachedOutputStream的實例中。

碼頭:org.eclipse.jetty.client.AsyncHttpConnection ::手柄() - >org.eclipse.jetty.http.HttpParser::parseNext()將填補字節數組中org.eclipse.jetty.client.ContentExchange其爲CachedExchange其是HttpExchange

netty-http:構建將HttpResponse內容組裝爲複合ChannelBuffer的管道。封裝的通道緩衝區組成完整的響應流。

我已經調試了所有三個客戶端,並沒有偶然發現一個未採取的分支,這會讓我將原始輸入流作爲交換主體。

這是重現性如此簡單的路線:

<camelContext id="pep-poc"> 
    <endpoint id="jetty.http.server" uri="jetty:http://{{http.host.server}}:{{http.port.server}}/my/frontend?disableStreamCache=true"/> 
    <endpoint id="http.client" uri="jetty:http://{{http.host.client}}:{{http.port.client}}/large_response.html?bridgeEndpoint=true&amp;throwExceptionOnFailure=false&amp;disableStreamCache=true"/> 

    <route id="reverse_proxy" startupOrder="10" streamCache="false"> 
     <from ref="jetty.http.server"/> 
     <to ref="http.client"/> 
    </route> 
</camelContext> 

我有一個Apache2的返回一個750MB的文件爲large_response.html

EDIT 2

事實上,這是所有可用的HTTP生產的問題。在Camel mailing list和相應的JIRA ticket上查看此線程。

回答