Java Code Examples for io.vertx.core.http.HttpClientOptions#setKeepAlive()

The following examples show how to use io.vertx.core.http.HttpClientOptions#setKeepAlive() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProxyVerticle.java    From quarantyne with Apache License 2.0 5 votes vote down vote up
@Override
public void start(Future<Void> startFuture) {
  this.bouncer = new Bouncer(vertx, configSupplier, configArgs);
  // proxy server (this server)
  HttpServerOptions httpServerOptions = new HttpServerOptions();
  httpServerOptions.setHost(configArgs.getIngress().getIp());
  httpServerOptions.setUsePooledBuffers(true);
  HttpServer httpServer = vertx.createHttpServer(httpServerOptions);

  // http client to remote
  HttpClientOptions httpClientOptions = new HttpClientOptions();
  httpClientOptions.setKeepAlive(true);
  httpClientOptions.setLogActivity(true);

  if (configArgs.getEgress().isSsl()) {
    httpClientOptions.setSsl(true);
  }
  httpClientOptions.setDefaultHost(configArgs.getEgress().getHost());
  httpClientOptions.setDefaultPort(configArgs.getEgress().getPort());

  this.httpClient = vertx.createHttpClient(httpClientOptions);

  httpServer.requestHandler(frontReq -> {
    if (frontReq.method().equals(HttpMethod.POST) || frontReq.method().equals(HttpMethod.PUT)) {
      frontReq.bodyHandler(reqBody -> {
        proxiedRequestHandler(frontReq, reqBody);
      });
    } else {
      proxiedRequestHandler(frontReq, null);
    }
  }).exceptionHandler(ex -> {
    log.error("HTTP server error", ex);
  }).listen(configArgs.getIngress().getPort(), configArgs.getIngress().getIp(), h -> {
    if (h.failed()) {
      log.error("proxy failed to start", h.cause());
      startFuture.fail(h.cause());
    }
  });
}
 
Example 2
Source File: Bouncer.java    From quarantyne with Apache License 2.0 5 votes vote down vote up
public Bouncer(Vertx vertx, Supplier<Config> configSupplier, ConfigArgs configArgs) {

    this.configSupplier = configSupplier;
    this.configArgs = configArgs;

    String blockedPage = configSupplier.get().getBlockedRequestPage();
    egressUrl = configArgs.getEgress();
    path = null;
    if (!blockedPage.startsWith("/") && blockedPage.startsWith("http")) {
      try {
        URL url = new URL(blockedPage);
        int port = url.getPort();
        if (url.getPort() < 0 && url.getProtocol().equals("https")) {
          port = 443;
        }
        if (url.getPort() < 0 && url.getProtocol().equals("http")) {
          port = 80;
        }
        egressUrl = new EgressUrl(url.getProtocol(), url.getHost(), port);
        path = url.getPath();
      } catch (MalformedURLException ex) {
        path = "/";
        log.error("cannot parse bounced page URL, defaulting to remote /");
      }
    } else {
      path = blockedPage;
    }

    HttpClientOptions httpClientOptions = new HttpClientOptions();
    httpClientOptions.setKeepAlive(true);
    if (egressUrl.isSsl()) {
      httpClientOptions.setSsl(true);
    }

    this.httpClient = vertx.createHttpClient(httpClientOptions);
  }
 
Example 3
Source File: HttpClientOptionsSPI.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
static HttpClientOptions createHttpClientOptions(HttpClientOptionsSPI spi) {
  HttpClientOptions httpClientOptions = new HttpClientOptions();

  httpClientOptions.setProtocolVersion(spi.getHttpVersion());
  httpClientOptions.setConnectTimeout(spi.getConnectTimeoutInMillis());
  httpClientOptions.setIdleTimeout(spi.getIdleTimeoutInSeconds());
  httpClientOptions.setTryUseCompression(spi.isTryUseCompression());
  httpClientOptions.setMaxWaitQueueSize(spi.getMaxWaitQueueSize());
  httpClientOptions.setMaxPoolSize(spi.getMaxPoolSize());
  httpClientOptions.setKeepAlive(spi.isKeepAlive());
  httpClientOptions.setMaxHeaderSize(spi.getMaxHeaderSize());
  httpClientOptions.setKeepAliveTimeout(spi.getKeepAliveTimeout());

  if (spi.isProxyEnable()) {
    ProxyOptions proxy = new ProxyOptions();
    proxy.setHost(spi.getProxyHost());
    proxy.setPort(spi.getProxyPort());
    proxy.setUsername(spi.getProxyUsername());
    proxy.setPassword(
        Encryptions.decode(spi.getProxyPassword(), spi.getConfigTag()));
    httpClientOptions.setProxyOptions(proxy);
  }

  if (spi.getHttpVersion() == HttpVersion.HTTP_2) {
    httpClientOptions.setHttp2ClearTextUpgrade(false);
    httpClientOptions.setUseAlpn(spi.isUseAlpn());
    httpClientOptions.setHttp2MultiplexingLimit(spi.getHttp2MultiplexingLimit());
    httpClientOptions.setHttp2MaxPoolSize(spi.getHttp2MaxPoolSize());
  }

  if (spi.isSsl()) {
    VertxTLSBuilder.buildHttpClientOptions(spi.getConfigTag(), httpClientOptions);
  }

  return httpClientOptions;
}
 
Example 4
Source File: S2ClientVerticle.java    From ocraft-s2client with MIT License 5 votes vote down vote up
private void initHttpClient() {
    HttpClientOptions httpClientOptions = new HttpClientOptions();
    httpClientOptions.setKeepAlive(true);
    httpClientOptions.setPipelining(true);
    httpClientOptions.setConnectTimeout(config().getInteger(CFG_CONNECT_TIMEOUT));
    httpClientOptions.setMaxWebsocketFrameSize(MAX_WEBSOCKET_FRAME_SIZE_IN_BYTES);
    httpClient = vertx.createHttpClient(httpClientOptions);
}
 
Example 5
Source File: EventSourceImpl.java    From vertx-sse with Apache License 2.0 4 votes vote down vote up
public EventSourceImpl(Vertx vertx, HttpClientOptions options) {
	options.setKeepAlive(true);
	this.vertx = vertx;
	this.options = options;
	eventHandlers = new HashMap<>();
}