Class HttpClient

  • All Implemented Interfaces:
    Measured

    public class HttpClient
    extends Object
    implements Measured
    An asynchronous HTTP client.

    It allows you to make requests to HTTP servers, and a single client can make requests to any server.

    It also allows you to open WebSockets to servers.

    The client can also pool HTTP connections.

    For pooling to occur, keep-alive must be true on the HttpClientOptions (default is true). In this case connections will be pooled and re-used if there are pending HTTP requests waiting to get a connection, otherwise they will be closed.

    This gives the benefits of keep alive when the client is loaded but means we don't keep connections hanging around unnecessarily when there would be no benefits anyway.

    The client also supports pipe-lining of requests. Pipe-lining means another request is sent on the same connection before the response from the preceding one has returned. Pipe-lining is not appropriate for all requests.

    To enable pipe-lining, it must be enabled on the HttpClientOptions (default is false).

    When pipe-lining is enabled the connection will be automatically closed when all in-flight responses have returned and there are no outstanding pending requests to write.

    The client is designed to be reused between requests.

    NOTE: This class has been automatically generated from the original non Mutiny-ified interface using Vert.x codegen.

    • Constructor Detail

      • HttpClient

        public HttpClient​(io.vertx.core.http.HttpClient delegate)
      • HttpClient

        public HttpClient​(Object delegate)
    • Method Detail

      • getDelegate

        public io.vertx.core.http.HttpClient getDelegate()
        Specified by:
        getDelegate in interface Measured
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • isMetricsEnabled

        public boolean isMetricsEnabled()
        Specified by:
        isMetricsEnabled in interface Measured
        Returns:
        true if metrics are enabled
      • request

        @CheckReturnValue
        public io.smallrye.mutiny.Uni<HttpClientRequest> request​(io.vertx.core.http.RequestOptions options)
        Create an HTTP request to send to the server. The handler is called when the request is ready to be sent.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        options - the request options
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • requestAndAwait

        public HttpClientRequest requestAndAwait​(io.vertx.core.http.RequestOptions options)
        Blocking variant of request(RequestOptions).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        options - the request options
        Returns:
        the HttpClientRequest instance produced by the operation.
      • requestAndForget

        public void requestAndForget​(io.vertx.core.http.RequestOptions options)
        Variant of request(RequestOptions) that ignores the result of the operation.

        This method subscribes on the result of request(RequestOptions), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from request(RequestOptions) but you don't need to compose it with other operations.

        Parameters:
        options - the request options
      • request

        @CheckReturnValue
        public io.smallrye.mutiny.Uni<HttpClientRequest> request​(io.vertx.core.http.HttpMethod method,
                                                                 int port,
                                                                 String host,
                                                                 String requestURI)
        Create an HTTP request to send to the server at the host and port. The handler is called when the request is ready to be sent.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        method - the HTTP method
        port - the port
        host - the host
        requestURI - the relative URI
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • requestAndAwait

        public HttpClientRequest requestAndAwait​(io.vertx.core.http.HttpMethod method,
                                                 int port,
                                                 String host,
                                                 String requestURI)
        Blocking variant of request(HttpMethod,int,String,String).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        method - the HTTP method
        port - the port
        host - the host
        requestURI - the relative URI
        Returns:
        the HttpClientRequest instance produced by the operation.
      • request

        @CheckReturnValue
        public io.smallrye.mutiny.Uni<HttpClientRequest> request​(io.vertx.core.http.HttpMethod method,
                                                                 String host,
                                                                 String requestURI)
        Create an HTTP request to send to the server at the host and default port. The handler is called when the request is ready to be sent.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        method - the HTTP method
        host - the host
        requestURI - the relative URI
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • requestAndAwait

        public HttpClientRequest requestAndAwait​(io.vertx.core.http.HttpMethod method,
                                                 String host,
                                                 String requestURI)
        Blocking variant of request(HttpMethod,String,String).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        method - the HTTP method
        host - the host
        requestURI - the relative URI
        Returns:
        the HttpClientRequest instance produced by the operation.
      • requestAndForget

        public void requestAndForget​(io.vertx.core.http.HttpMethod method,
                                     String host,
                                     String requestURI)
        Variant of request(HttpMethod,String,String) that ignores the result of the operation.

        This method subscribes on the result of request(HttpMethod,String,String), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from request(HttpMethod,String,String) but you don't need to compose it with other operations.

        Parameters:
        method - the HTTP method
        host - the host
        requestURI - the relative URI
      • request

        @CheckReturnValue
        public io.smallrye.mutiny.Uni<HttpClientRequest> request​(io.vertx.core.http.HttpMethod method,
                                                                 String requestURI)
        Create an HTTP request to send to the server at the default host and port. The handler is called when the request is ready to be sent.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        method - the HTTP method
        requestURI - the relative URI
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • requestAndAwait

        public HttpClientRequest requestAndAwait​(io.vertx.core.http.HttpMethod method,
                                                 String requestURI)
        Blocking variant of request(HttpMethod,String).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        method - the HTTP method
        requestURI - the relative URI
        Returns:
        the HttpClientRequest instance produced by the operation.
      • requestAndForget

        public void requestAndForget​(io.vertx.core.http.HttpMethod method,
                                     String requestURI)
        Variant of request(HttpMethod,String) that ignores the result of the operation.

        This method subscribes on the result of request(HttpMethod,String), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from request(HttpMethod,String) but you don't need to compose it with other operations.

        Parameters:
        method - the HTTP method
        requestURI - the relative URI
      • webSocket

        @CheckReturnValue
        public io.smallrye.mutiny.Uni<WebSocket> webSocket​(int port,
                                                           String host,
                                                           String requestURI)
        Connect a WebSocket to the specified port, host and relative request URI

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        port - the port
        host - the host
        requestURI - the relative URI
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • webSocketAndAwait

        public WebSocket webSocketAndAwait​(int port,
                                           String host,
                                           String requestURI)
        Blocking variant of webSocket(int,String,String).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        port - the port
        host - the host
        requestURI - the relative URI
        Returns:
        the WebSocket instance produced by the operation.
      • webSocketAndForget

        public void webSocketAndForget​(int port,
                                       String host,
                                       String requestURI)
        Variant of webSocket(int,String,String) that ignores the result of the operation.

        This method subscribes on the result of webSocket(int,String,String), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from webSocket(int,String,String) but you don't need to compose it with other operations.

        Parameters:
        port - the port
        host - the host
        requestURI - the relative URI
      • webSocket

        @CheckReturnValue
        public io.smallrye.mutiny.Uni<WebSocket> webSocket​(String host,
                                                           String requestURI)
        Connect a WebSocket to the host and relative request URI and default port

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        host - the host
        requestURI - the relative URI
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • webSocketAndAwait

        public WebSocket webSocketAndAwait​(String host,
                                           String requestURI)
        Blocking variant of webSocket(String,String).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        host - the host
        requestURI - the relative URI
        Returns:
        the WebSocket instance produced by the operation.
      • webSocketAndForget

        public void webSocketAndForget​(String host,
                                       String requestURI)
        Variant of webSocket(String,String) that ignores the result of the operation.

        This method subscribes on the result of webSocket(String,String), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from webSocket(String,String) but you don't need to compose it with other operations.

        Parameters:
        host - the host
        requestURI - the relative URI
      • webSocket

        @CheckReturnValue
        public io.smallrye.mutiny.Uni<WebSocket> webSocket​(String requestURI)
        Connect a WebSocket at the relative request URI using the default host and port

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        requestURI - the relative URI
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • webSocketAndAwait

        public WebSocket webSocketAndAwait​(String requestURI)
        Blocking variant of webSocket(String).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        requestURI - the relative URI
        Returns:
        the WebSocket instance produced by the operation.
      • webSocketAndForget

        public void webSocketAndForget​(String requestURI)
        Variant of webSocket(String) that ignores the result of the operation.

        This method subscribes on the result of webSocket(String), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from webSocket(String) but you don't need to compose it with other operations.

        Parameters:
        requestURI - the relative URI
      • webSocket

        @CheckReturnValue
        public io.smallrye.mutiny.Uni<WebSocket> webSocket​(io.vertx.core.http.WebSocketConnectOptions options)
        Connect a WebSocket with the specified options.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        options - the request options
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • webSocketAndAwait

        public WebSocket webSocketAndAwait​(io.vertx.core.http.WebSocketConnectOptions options)
        Blocking variant of webSocket(WebSocketConnectOptions).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        options - the request options
        Returns:
        the WebSocket instance produced by the operation.
      • webSocketAndForget

        public void webSocketAndForget​(io.vertx.core.http.WebSocketConnectOptions options)
        Variant of webSocket(WebSocketConnectOptions) that ignores the result of the operation.

        This method subscribes on the result of webSocket(WebSocketConnectOptions), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from webSocket(WebSocketConnectOptions) but you don't need to compose it with other operations.

        Parameters:
        options - the request options
      • webSocketAbs

        @CheckReturnValue
        public io.smallrye.mutiny.Uni<WebSocket> webSocketAbs​(String url,
                                                              MultiMap headers,
                                                              io.vertx.core.http.WebsocketVersion version,
                                                              List<String> subProtocols)
        Connect a WebSocket with the specified absolute url, with the specified headers, using the specified version of WebSockets, and the specified WebSocket sub protocols.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        url - the absolute url
        headers - the headers
        version - the WebSocket version
        subProtocols - the subprotocols to use
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • webSocketAbsAndAwait

        public WebSocket webSocketAbsAndAwait​(String url,
                                              MultiMap headers,
                                              io.vertx.core.http.WebsocketVersion version,
                                              List<String> subProtocols)
        Blocking variant of webSocketAbs(String,io.vertx.mutiny.core.MultiMap,WebsocketVersion,List).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        url - the absolute url
        headers - the headers
        version - the WebSocket version
        subProtocols - the subprotocols to use
        Returns:
        the WebSocket instance produced by the operation.
      • redirectHandler

        public HttpClient redirectHandler​(Function<HttpClientResponse,​io.smallrye.mutiny.Uni<io.vertx.core.http.RequestOptions>> handler)
        Parameters:
        handler - the new redirect handler
        Returns:
        a reference to this, so the API can be used fluently
      • close

        @CheckReturnValue
        public io.smallrye.mutiny.Uni<Void> close()
        Close the client. Closing will close down any pooled connections. Clients should always be closed after use.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • closeAndAwait

        public Void closeAndAwait()
        Blocking variant of close().

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Returns:
        the Void instance produced by the operation.
      • closeAndForget

        public void closeAndForget()
        Variant of close() that ignores the result of the operation.

        This method subscribes on the result of close(), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from close() but you don't need to compose it with other operations.

      • newInstance

        public static HttpClient newInstance​(io.vertx.core.http.HttpClient arg)