use of org.apache.hc.core5.pool.ConnPoolStats in project httpcomponents-core by apache.
the class LoggingConnPoolListener method onRelease.
@Override
public void onRelease(final HttpHost route, final ConnPoolStats<HttpHost> connPoolStats) {
if (connLog.isDebugEnabled()) {
final StringBuilder buf = new StringBuilder();
buf.append("Released ").append(route).append(" ");
final PoolStats totals = connPoolStats.getTotalStats();
buf.append(" total kept alive: ").append(totals.getAvailable()).append("; ");
buf.append("total allocated: ").append(totals.getLeased() + totals.getAvailable());
buf.append(" of ").append(totals.getMax());
connLog.debug(buf.toString());
}
}
use of org.apache.hc.core5.pool.ConnPoolStats in project httpcomponents-core by apache.
the class AsyncReverseProxyExample method main.
public static void main(final String[] args) throws Exception {
if (args.length < 1) {
System.out.println("Usage: <hostname[:port]> [listener port] [--quiet]");
System.exit(1);
}
// Target host
final HttpHost targetHost = HttpHost.create(args[0]);
int port = 8080;
if (args.length > 1) {
port = Integer.parseInt(args[1]);
}
for (final String s : args) {
if ("--quiet".equalsIgnoreCase(s)) {
quiet = true;
break;
}
}
println("Reverse proxy to " + targetHost);
final IOReactorConfig config = IOReactorConfig.custom().setSoTimeout(1, TimeUnit.MINUTES).build();
final HttpAsyncRequester requester = AsyncRequesterBootstrap.bootstrap().setIOReactorConfig(config).setConnPoolListener(new ConnPoolListener<HttpHost>() {
@Override
public void onLease(final HttpHost route, final ConnPoolStats<HttpHost> connPoolStats) {
final StringBuilder buf = new StringBuilder();
buf.append("[proxy->origin] connection leased ").append(route);
println(buf.toString());
}
@Override
public void onRelease(final HttpHost route, final ConnPoolStats<HttpHost> connPoolStats) {
final StringBuilder buf = new StringBuilder();
buf.append("[proxy->origin] connection released ").append(route);
final PoolStats totals = connPoolStats.getTotalStats();
buf.append("; total kept alive: ").append(totals.getAvailable()).append("; ");
buf.append("total allocated: ").append(totals.getLeased() + totals.getAvailable());
buf.append(" of ").append(totals.getMax());
println(buf.toString());
}
}).setStreamListener(new Http1StreamListener() {
@Override
public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
// empty
}
@Override
public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
// empty
}
@Override
public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
println("[proxy<-origin] connection " + connection.getLocalAddress() + "->" + connection.getRemoteAddress() + (keepAlive ? " kept alive" : " cannot be kept alive"));
}
}).setMaxTotal(100).setDefaultMaxPerRoute(20).create();
final HttpAsyncServer server = AsyncServerBootstrap.bootstrap().setIOReactorConfig(config).setStreamListener(new Http1StreamListener() {
@Override
public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
// empty
}
@Override
public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
// empty
}
@Override
public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
println("[client<-proxy] connection " + connection.getLocalAddress() + "->" + connection.getRemoteAddress() + (keepAlive ? " kept alive" : " cannot be kept alive"));
}
}).register("*", () -> new IncomingExchangeHandler(targetHost, requester)).create();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
println("Reverse proxy shutting down");
server.close(CloseMode.GRACEFUL);
requester.close(CloseMode.GRACEFUL);
}));
requester.start();
server.start();
server.listen(new InetSocketAddress(port), URIScheme.HTTP);
println("Listening on port " + port);
server.awaitShutdown(TimeValue.MAX_VALUE);
}
use of org.apache.hc.core5.pool.ConnPoolStats in project httpcomponents-core by apache.
the class ClassicReverseProxyExample method main.
public static void main(final String[] args) throws Exception {
if (args.length < 1) {
System.out.println("Usage: <hostname[:port]> [listener port]");
System.exit(1);
}
final HttpHost targetHost = HttpHost.create(args[0]);
int port = 8080;
if (args.length > 1) {
port = Integer.parseInt(args[1]);
}
System.out.println("Reverse proxy to " + targetHost);
final HttpRequester requester = RequesterBootstrap.bootstrap().setStreamListener(new Http1StreamListener() {
@Override
public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
System.out.println("[proxy->origin] " + Thread.currentThread() + " " + request.getMethod() + " " + request.getRequestUri());
}
@Override
public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
System.out.println("[proxy<-origin] " + Thread.currentThread() + " status " + response.getCode());
}
@Override
public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
System.out.println("[proxy<-origin] " + Thread.currentThread() + " exchange completed; " + "connection " + (keepAlive ? "kept alive" : "cannot be kept alive"));
}
}).setConnPoolListener(new ConnPoolListener<HttpHost>() {
@Override
public void onLease(final HttpHost route, final ConnPoolStats<HttpHost> connPoolStats) {
final StringBuilder buf = new StringBuilder();
buf.append("[proxy->origin] ").append(Thread.currentThread()).append(" connection leased ").append(route);
System.out.println(buf);
}
@Override
public void onRelease(final HttpHost route, final ConnPoolStats<HttpHost> connPoolStats) {
final StringBuilder buf = new StringBuilder();
buf.append("[proxy->origin] ").append(Thread.currentThread()).append(" connection released ").append(route);
final PoolStats totals = connPoolStats.getTotalStats();
buf.append("; total kept alive: ").append(totals.getAvailable()).append("; ");
buf.append("total allocated: ").append(totals.getLeased() + totals.getAvailable());
buf.append(" of ").append(totals.getMax());
System.out.println(buf);
}
}).create();
final HttpServer server = ServerBootstrap.bootstrap().setListenerPort(port).setStreamListener(new Http1StreamListener() {
@Override
public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
System.out.println("[client->proxy] " + Thread.currentThread() + " " + request.getMethod() + " " + request.getRequestUri());
}
@Override
public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
System.out.println("[client<-proxy] " + Thread.currentThread() + " status " + response.getCode());
}
@Override
public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
System.out.println("[client<-proxy] " + Thread.currentThread() + " exchange completed; " + "connection " + (keepAlive ? "kept alive" : "cannot be kept alive"));
}
}).setExceptionListener(new ExceptionListener() {
@Override
public void onError(final Exception ex) {
if (ex instanceof SocketException) {
System.out.println("[client->proxy] " + Thread.currentThread() + " " + ex.getMessage());
} else {
System.out.println("[client->proxy] " + Thread.currentThread() + " " + ex.getMessage());
ex.printStackTrace(System.out);
}
}
@Override
public void onError(final HttpConnection connection, final Exception ex) {
if (ex instanceof SocketTimeoutException) {
System.out.println("[client->proxy] " + Thread.currentThread() + " time out");
} else if (ex instanceof SocketException || ex instanceof ConnectionClosedException) {
System.out.println("[client->proxy] " + Thread.currentThread() + " " + ex.getMessage());
} else {
System.out.println("[client->proxy] " + Thread.currentThread() + " " + ex.getMessage());
ex.printStackTrace(System.out);
}
}
}).register("*", new ProxyHandler(targetHost, requester)).create();
server.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
server.close(CloseMode.GRACEFUL);
requester.close(CloseMode.GRACEFUL);
}));
System.out.println("Listening on port " + port);
server.awaitTermination(TimeValue.MAX_VALUE);
}
use of org.apache.hc.core5.pool.ConnPoolStats in project httpcomponents-core by apache.
the class LoggingConnPoolListener method onLease.
@Override
public void onLease(final HttpHost route, final ConnPoolStats<HttpHost> connPoolStats) {
if (connLog.isDebugEnabled()) {
final StringBuilder buf = new StringBuilder();
buf.append("Leased ").append(route).append(" ");
final PoolStats totals = connPoolStats.getTotalStats();
buf.append(" total kept alive: ").append(totals.getAvailable()).append("; ");
buf.append("total allocated: ").append(totals.getLeased() + totals.getAvailable());
buf.append(" of ").append(totals.getMax());
connLog.debug(buf.toString());
}
}
Aggregations