I'm using Apache's HttpAsyncClient to proxy, or relay, incoming requests to another server. I have the simple case working (sample code below).
The problem is that the Future<HttpResponse>
will hold the response in memory, waiting until it's done reading before completing. We have some calls that have very large response bodies, so what I want to do is a "pipeline" or "zero copy" strategy, where I just pass along the body contents as they come, without holding the whole response in memory.
Apache Http Client provides a ZeroCopyPost
and ZeroCopyConsumer
, but they operate on Files
rather than streams (i.e. the OutputStream of the response).
How can I write the code to pass the bytes from the remote response directly back to the original response?
My code so far:
try {
httpClient.start();
Future<HttpResponse> future = httpClient.execute(method, null);
HttpResponse remoteResponse = future.get();
relayStatus(remoteResponse, originalResponse);
relayHeaders(remoteResponse, originalResponse);
relayBody(remoteResponse, originalResponse);
}
catch (IOException | InterruptedException |ExecutionException e) {
...
}
I've been having trouble finding direct example code for doing this, but the sample linked in comments takes me a lot farther. I think the answer will be along the lines of using AsyncByteConsumer
Am I on the right track? How can I extend that to use the zero copy strategy to pass the body bytes from the relayed response back to the original response?