You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Volker Hochstein <v....@highstone.de> on 2024/02/26 13:45:54 UTC
ErrorStream already closed reading response.getBody using FutureRequestExecutionService
Hi,
I am triggering two http requests in parallel using
FutureRequestExecutionService.
I am trying to read response body in Callback completed:
EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)
However, I am getting exception: java.io.IOException: Attempted read on
closed stream
I am using Apache HTTP Client 4.5.2 in a Adobe AEM (Sling Framework)
project. (Java based)
I am calling getEntity() only once in code and I am not debugging it (so
no watch by IDE or whatever).
If I execute both http requests "standard" sequentially everything is
working as expected.
Any help would be greatly appreciated since I ve run out of options what
else to try.
--
Volker
Relevant parts of the code:
ExecutorService executor = Executors.newFixedThreadPool(2);
HttpClientBuilder clientBuilder =
HttpRequestUtility.createDefaultHttpClientBuilder().setMaxConnPerRoute(2);
CloseableHttpClient httpClient = clientBuilder.build();
FutureRequestExecutionService requestExecService = new
FutureRequestExecutionService(httpClient, executor);
CountDownLatch latch = new CountDownLatch(2); // Wait for 3 responses
Map<String, HttpResponseInfo> responseMap = new HashMap<>();
requestExecService.execute(reqSalesforceAccountContacts.getHttpGet(),
HttpClientContext.create(), new
SalesforceRestAPIContactPoc.DefaultResponseHandler(), new
SalesforceRestAPIContactPoc.MyCallback(reqSalesforceAccountContacts.getResponseInfo(),
latch, responseMap));
requestExecService.execute(reqOpenCases.getHttpGet(),
HttpClientContext.create(), new
SalesforceRestAPIContactPoc.DefaultResponseHandler(), new
SalesforceRestAPIContactPoc.MyCallback(reqOpenCases.getResponseInfo(),
latch, responseMap));
latch.await(); // Wait for all responses
Callback Class:
private final class MyCallback implements FutureCallback<HttpResponse> {
private final HttpResponseInfo responseInfo;
private final CountDownLatch latch;
private final Map<String, HttpResponseInfo> responseMap;
public MyCallback(HttpResponseInfo responseInfo, CountDownLatch
latch, Map<String, HttpResponseInfo> responseMap) {
this.responseInfo = responseInfo;
this.latch = latch;
this.responseMap = responseMap;
}
@Override
public void completed(HttpResponse response) {
logger.error("Callback completed: {}",
responseInfo.getRequestId());
responseInfo.setStatusCode(response.getStatusLine().getStatusCode())
.setHeaders(response.getAllHeaders());
try {
responseInfo.setBody(EntityUtils.toString(response.getEntity(),
StandardCharsets.UTF_8));
} catch (ParseException|IOException e) {
// Exception triggered here
logger.error("Failed to get Body Contacts", e);
}
if (responseInfo.isOk()) {
responseInfo.logRequestFinished();
} else {
responseInfo.logAsError();
}
responseMap.put(responseInfo.getRequestId(), responseInfo);
latch.countDown(); // Signal completion
}
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: ErrorStream already closed reading response.getBody using FutureRequestExecutionService
Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2024-02-26 at 14:45 +0100, Volker Hochstein wrote:
> Hi,
>
> I am triggering two http requests in parallel using
> FutureRequestExecutionService.
> I am trying to read response body in Callback completed:
> EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)
> However, I am getting exception: java.io.IOException: Attempted read
> on
> closed stream
>
> I am using Apache HTTP Client 4.5.2 in a Adobe AEM (Sling Framework)
> project. (Java based)
>
> I am calling getEntity() only once in code and I am not debugging it
> (so
> no watch by IDE or whatever).
> If I execute both http requests "standard" sequentially everything is
> working as expected.
>
> Any help would be greatly appreciated since I ve run out of options
> what
> else to try.
>
Hi Volker
You need to make sure that the response handler fully processes the
response entity internally and returns it as an operation result. The
underlying data stream will no longer be readable once the response
handler returns control.
Hope this helps
Oleg
> --
> Volker
>
> Relevant parts of the code:
>
> ExecutorService executor = Executors.newFixedThreadPool(2);
> HttpClientBuilder clientBuilder =
> HttpRequestUtility.createDefaultHttpClientBuilder().setMaxConnPerRout
> e(2);
> CloseableHttpClient httpClient = clientBuilder.build();
> FutureRequestExecutionService requestExecService = new
> FutureRequestExecutionService(httpClient, executor);
> CountDownLatch latch = new CountDownLatch(2); // Wait for 3 responses
> Map<String, HttpResponseInfo> responseMap = new HashMap<>();
>
> requestExecService.execute(reqSalesforceAccountContacts.getHttpGet(),
> HttpClientContext.create(), new
> SalesforceRestAPIContactPoc.DefaultResponseHandler(), new
> SalesforceRestAPIContactPoc.MyCallback(reqSalesforceAccountContacts.g
> etResponseInfo(),
> latch, responseMap));
> requestExecService.execute(reqOpenCases.getHttpGet(),
> HttpClientContext.create(), new
> SalesforceRestAPIContactPoc.DefaultResponseHandler(), new
> SalesforceRestAPIContactPoc.MyCallback(reqOpenCases.getResponseInfo()
> ,
> latch, responseMap));
>
> latch.await(); // Wait for all responses
>
>
> Callback Class:
> private final class MyCallback implements
> FutureCallback<HttpResponse> {
> private final HttpResponseInfo responseInfo;
> private final CountDownLatch latch;
> private final Map<String, HttpResponseInfo> responseMap;
>
> public MyCallback(HttpResponseInfo responseInfo,
> CountDownLatch
> latch, Map<String, HttpResponseInfo> responseMap) {
> this.responseInfo = responseInfo;
> this.latch = latch;
> this.responseMap = responseMap;
> }
>
> @Override
> public void completed(HttpResponse response) {
> logger.error("Callback completed: {}",
> responseInfo.getRequestId());
>
> responseInfo.setStatusCode(response.getStatusLine().getStatusCode())
> .setHeaders(response.getAllHeaders());
>
> try {
> responseInfo.setBody(EntityUtils.toString(response.getEntity(),
> StandardCharsets.UTF_8));
> } catch (ParseException|IOException e) {
> // Exception triggered here
> logger.error("Failed to get Body Contacts", e);
> }
> if (responseInfo.isOk()) {
> responseInfo.logRequestFinished();
> } else {
> responseInfo.logAsError();
> }
> responseMap.put(responseInfo.getRequestId(),
> responseInfo);
> latch.countDown(); // Signal completion
> }
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org