Created
November 12, 2014 17:11
-
-
Save hirthwork/6f4ebb047e77a61f644e to your computer and use it in GitHub Desktop.
Simple illustration problems with empty HttpEntities which is not returned to connections pool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.TimeUnit; | |
import org.apache.http.HttpRequest; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.config.RequestConfig; | |
import org.apache.http.client.methods.CloseableHttpResponse; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.config.SocketConfig; | |
import org.apache.http.impl.bootstrap.HttpServer; | |
import org.apache.http.impl.bootstrap.ServerBootstrap; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.HttpClients; | |
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | |
import org.apache.http.protocol.HttpContext; | |
import org.apache.http.protocol.HttpRequestHandler; | |
public class EmptyHttpServer { | |
public static void main(String[] args) throws Exception { | |
SocketConfig socketConfig = SocketConfig.custom() | |
.setSoTimeout(15000) | |
.setTcpNoDelay(true) | |
.build(); | |
RequestConfig requestConfig = RequestConfig.custom() | |
.setConnectionRequestTimeout(1000) | |
.build(); | |
final HttpServer server = ServerBootstrap.bootstrap() | |
.setListenerPort(8080) | |
.setSocketConfig(socketConfig) | |
.registerHandler("*", new EmptyHandler()) | |
.create(); | |
server.start(); | |
PoolingHttpClientConnectionManager connMgr = | |
new PoolingHttpClientConnectionManager(); | |
connMgr.setDefaultMaxPerRoute(1); | |
try (CloseableHttpClient client = HttpClients.custom() | |
.setConnectionManager(connMgr) | |
.setDefaultRequestConfig(requestConfig) | |
.build(); | |
CloseableHttpResponse response1 = client.execute( | |
new HttpGet("http://localhost:8080/")); | |
CloseableHttpResponse response2 = client.execute( | |
new HttpGet("http://localhost:8080/"))) | |
{ | |
} finally { | |
server.shutdown(5, TimeUnit.SECONDS); | |
} | |
} | |
private static class EmptyHandler implements HttpRequestHandler { | |
@Override | |
public void handle( | |
final HttpRequest request, | |
final HttpResponse response, | |
final HttpContext context) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment