I am testing vertx httpCient. The codes are :
/*
* this vertice query mock server on every message
*/
public class QueryMockVerticle extends AbstractVerticle {
private static final Logger LOG = LoggerFactory.getLogger(HttpServerVerticle.class);
private HttpClient httpClient;
@Override
public void start() throws Exception {
super.start();
this.httpClient = vertx.createHttpClient(new HttpClientOptions()
.setKeepAlive(true)
.setTcpKeepAlive(true)
.setIdleTimeout(1000 * 60)
.setMaxPoolSize(100)
);
EventBus eventBus = vertx.eventBus();
eventBus.consumer("queryTarget", (msg) -> {
HttpClientRequest clientRequest = httpClient.get(8080, "10.95.123.131", "/test.jsp",
response -> response.bodyHandler(responseBuffer -> {
LOG.info("Get nodejs response, " + this.deploymentID());
msg.reply(responseBuffer.toString());
})
);
clientRequest.exceptionHandler(t -> LOG.error("", t));
clientRequest.putHeader("Connection", "keep-alive"); // if this line is commented out, the jsp below will print 'null'
clientRequest.end();
});
}
@Override
public void stop() throws Exception {
super.stop();
this.httpClient.close();
}
}
this is the verticle to generate load:
public class LoadTestVerticle extends AbstractVerticle {
private long timerId;
@Override
public void start() throws Exception {
super.start();
EventBus eventBus = vertx.eventBus();
timerId = vertx.setPeriodic(5, time -> {
eventBus.send("queryTarget", "", asyncResult ->
System.out.println(asyncResult.result().body())
);
});
}
@Override
public void stop() throws Exception {
super.stop();
vertx.cancelTimer(timerId);
}
}
The main class :
public class HelloWorldEmbedded {
public static void main(String[] args) {
System.out.println("cpu count: " + Runtime.getRuntime().availableProcessors());
VertxOptions vertxOptions = new VertxOptions();
System.out.println("event loop threads: " + vertxOptions.getEventLoopPoolSize());
Vertx vertx = Vertx.vertx(vertxOptions);
Verticle httpServerVerticle = new HttpServerVerticle();
vertx.deployVerticle(httpServerVerticle);
//int queryVerticleCount = vertxOptions.getEventLoopPoolSize() > 1 ? vertxOptions.getEventLoopPoolSize() - 1 : 1;
int queryVerticleCount = 10;
for (int i = 0; i < queryVerticleCount; i++) {
vertx.deployVerticle(new QueryMockVerticle());
}
vertx.deployVerticle(new LoadTestVerticle());
}
}
The jsp is :
<% System.out.println(request.getHeader("Connection")); %>OK
After vertx started up, I got too many "TIME_WAIT" TCP connections.
Every 1.0s: netstat -4 | grep TIME_WAIT | wc -l
25173
So, what's wrong with my code? Why doesn't connection pool work? Anyon can help me ?