Skip to content

Instantly share code, notes, and snippets.

@oli-h
Created April 20, 2018 10:17
Show Gist options
  • Save oli-h/fe5607132509c51a57d130f821271189 to your computer and use it in GitHub Desktop.
Save oli-h/fe5607132509c51a57d130f821271189 to your computer and use it in GitHub Desktop.
package ch.oli.vertx;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpServer;
public class ReproducerTcpClose {
private static final Vertx VERTX = Vertx.vertx();
public static void main(String[] args) throws Exception {
startBackendServer(); // listen on Port 7020
HttpClientOptions clOpts = new HttpClientOptions().setMaxPoolSize(1).setKeepAlive(true);
final HttpClient httpClient = VERTX.createHttpClient(clOpts);
for (int i = 1; ; i++) {
String uri = "id-" + i;
HttpClientRequest req = httpClient.get(7020, "localhost", uri);
req.handler(resp -> {
int status = resp.statusCode();
if (status != 200) {
System.out.println("Request " + uri + ": Non-200-Status " + status);
}
resp.handler(data -> {
// System.out.println("Request #" + uri + ": received " + data.length() + " bytes");
});
resp.endHandler(end -> {
System.out.println("Request " + uri + ": finished (response complete)");
});
});
req.connectionHandler(conn -> {
System.out.println("Connection established for " + uri);
});
req.end();
System.out.println("\n\nRequest " + uri + ": starting");
Thread.sleep(5000);
}
}
private static void startBackendServer() {
final HttpServer httpServer = VERTX.createHttpServer();
httpServer.requestHandler(req -> {
System.out.println("-------------------------- Backend-Server received request " + req.uri());
VERTX.setTimer(500, id -> {
req.response().end("my data");
System.out.println("-------------------------- Backend-Server finished response " + req.uri());
});
});
httpServer.listen(7020);
}
}
@oli-h
Copy link
Author

oli-h commented Apr 20, 2018

Note: vertx-core master branch (3.6.0-SNAPSHOT) again behaves like vertx-3.5.0 - so it really makes use of "keep-alive" connections between two http-requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment