Neil Newman created HTTPCLIENT-1657:
---------------------------------------
Summary: "Connection is not open" in httpClient.execute
Key: HTTPCLIENT-1657
URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1657
Project: HttpComponents HttpClient
Issue Type: Bug
Affects Versions: 4.4.1
Reporter: Neil Newman
I have a program that needs to solve several problems. For each problem, it can
try several different algorithms in parallel. As soon as one algorithm solves
the problem, I would like the others to terminate promptly. Some algorithms are
accessed via HTTP requests: you make a POST with the problem to a server,
block, and the server returns an answer. If another thread finds the answer
before the server returns, I would like that thread to cancel the http request.
My code looks as follows
{code:title=Example.java|borderStyle=solid}
// fields
private final Lock lock;
private final AtomicBoolean activeSolve;
private HttpPost post;
private final CloseableHttpClient httpClient =
HttpClients.createDefault();
public String makePost() {
post = new HttpPost("some url");
final StringEntity stringEntity = new StringEntity("some json string",
ContentType.APPLICATION_JSON);
post.setEntity(stringEntity);
try {
lock.lock();
activeSolve.set(true);
lock.unlock();
try (final CloseableHttpResponse httpResponse =
httpClient.execute(post)) {
final String response =
EntityUtils.toString(httpResponse.getEntity());
return response;
}
} catch (IOException e) {
if (post.isAborted()) {
log.trace("Web request was aborted");
return "aborted";
} else {
throw new RuntimeException("Could not contact server", e);
}
} finally {
lock.lock();
activeSolve.set(false);
lock.unlock();
}
}
public void interrupt() {
lock.lock();
if (activeSolve.get()) {
post.abort();
}
lock.unlock();
}
{code}
{code}
Every ~1000 problems or so, I'll see the following stacktrace:
java.lang.IllegalStateException: Connection is not open
at org.apache.http.util.Asserts.check(Asserts.java:34)
at
org.apache.http.impl.BHttpConnectionBase.ensureOpen(BHttpConnectionBase.java:132)
at
org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseEntity(DefaultBHttpClientConnection.java:177)
at
org.apache.http.impl.conn.CPoolProxy.receiveResponseEntity(CPoolProxy.java:172)
at
org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:274)
at
org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)
at
org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at
org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at
org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at
org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at ... my classes
{code}
If I comment out the interrupt code and disable interrupting, I stop seeing
this error.
Am I using the library incorrectly? I can't figure out what circumstances cause
this to occur.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]