Example usage for io.vertx.core.http HttpHeaders KEEP_ALIVE

List of usage examples for io.vertx.core.http HttpHeaders KEEP_ALIVE

Introduction

In this page you can find the example usage for io.vertx.core.http HttpHeaders KEEP_ALIVE.

Prototype

CharSequence KEEP_ALIVE

To view the source code for io.vertx.core.http HttpHeaders KEEP_ALIVE.

Click Source Link

Document

keep-alive header value

Usage

From source file:io.advantageous.qbit.vertx.http.client.HttpVertxClient.java

License:Apache License

@Override
public void sendHttpRequest(final HttpRequest request) {

    checkClosed();/*from   w w w.  ja  v a 2s  . c o  m*/

    if (trace) {
        logger.debug(sputs("HTTP CLIENT: sendHttpRequest:: \n{}\n", request, "\nparams\n", request.params()));
    }

    final String uri = getURICreateParamsIfNeeded(request);

    final HttpMethod httpMethod = HttpMethod.valueOf(request.getMethod());

    final HttpClientRequest httpClientRequest = httpClient.request(httpMethod, uri,
            httpClientResponse -> handleResponse(request, httpClientResponse));

    final MultiMap<String, String> headers = request.getHeaders();

    httpClientRequest.exceptionHandler(error -> {

        if (error instanceof ConnectException) {
            closed.set(true);
            try {
                stop();
            } catch (Exception ex) {

                errorHandler.accept(ex);
                logger.warn("Unable to stop client " + "after failed connection", ex);
            }
            request.getReceiver().errorWithCode("\"Client connection was closed\"",
                    HttpStatus.SERVICE_UNAVAILABLE);
            logger.warn("Connection error", error);
        } else {
            logger.error("Unable to connect to " + host + " port " + port, error);
        }

        errorHandler.accept(error);
    });

    if (headers != null) {

        for (String key : headers.keySet()) {
            httpClientRequest.putHeader(key, headers.getAll(key));
        }
    }

    final byte[] body = request.getBody();

    if (keepAlive) {
        httpClientRequest.putHeader(HttpHeaders.CONNECTION, HttpHeaders.KEEP_ALIVE);
    }

    if (body != null && body.length > 0) {

        httpClientRequest.putHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(body.length));
        if (request.getContentType() != null) {

            httpClientRequest.putHeader("Content-Type", request.getContentType());
        }
        httpClientRequest.end(Buffer.buffer(request.getBody()));

    } else {
        httpClientRequest.end();
    }

    if (trace)
        logger.trace("HttpClientVertx::SENT \n{}", request);

}