Class AsyncHttpClient

  • All Implemented Interfaces:
    RocketClient

    public class AsyncHttpClient
    extends Object
    implements RocketClient
    Asynchronous HTTP client that executes requests on a dedicated thread pool.

    This client wraps any synchronous RocketClient implementation and provides non-blocking request execution via CompletableFuture.

    Features

    • Non-blocking request execution with CompletableFuture
    • Configurable thread pool size
    • Wraps any RocketClient implementation
    • Proper exception propagation via CompletionException

    Basic Usage

    
     ExecutorService executor = Executors.newFixedThreadPool(4);
     AsyncHttpClient asyncClient = new AsyncHttpClient(
         "https://api.example.com",
         executor
     );
    
     // Execute async request
     CompletableFuture<User> future = asyncClient.executeAsync(request);
    
     // Handle result when ready
     future.thenAccept(user -> System.out.println("Got: " + user.getName()))
           .exceptionally(ex -> {
               System.err.println("Failed: " + ex.getMessage());
               return null;
           });
    
     // Don't forget to shutdown
     asyncClient.shutdown();
     

    Via RocketRest

    
     RocketRest client = new RocketRest(config);
    
     client.async().get("/users/1", User.class)
         .thenAccept(user -> System.out.println(user));
     
    Since:
    1.0.0
    Author:
    guinetik <guinetik@gmail.com>
    See Also:
    RocketClient, RocketClientFactory, RocketRest.async()
    • Constructor Detail

      • AsyncHttpClient

        public AsyncHttpClient​(RocketClient delegate,
                               ExecutorService executor)
        Creates a new AsyncHttpClient with the specified delegate client and executor.
        Parameters:
        delegate - The underlying HTTP client to delegate requests to
        executor - The executor service to run requests on
      • AsyncHttpClient

        public AsyncHttpClient​(String baseUrl,
                               ExecutorService executor)
        Creates a new AsyncHttpClient with a DefaultHttpClient as the delegate.
        Parameters:
        baseUrl - The base URL for API requests
        executor - The executor service to run requests on
      • AsyncHttpClient

        public AsyncHttpClient​(String baseUrl,
                               RocketRestOptions clientOptions,
                               ExecutorService executor)
        Creates a new AsyncHttpClient with a DefaultHttpClient as the delegate and client options.
        Parameters:
        baseUrl - The base URL for API requests
        clientOptions - The client options
        executor - The executor service to run requests on
    • Method Detail

      • configureSsl

        public void configureSsl​(SSLContext sslContext)
        Description copied from interface: RocketClient
        Sets the SSL context to be used for HTTPS requests.
        Specified by:
        configureSsl in interface RocketClient
        Parameters:
        sslContext - The SSL context to use.
      • execute

        public <Req,​Res> Res execute​(RequestSpec<Req,​Res> requestSpec)
                                    throws RocketRestException
        Description copied from interface: RocketClient
        Executes an HTTP request based on the provided request specification.
        Specified by:
        execute in interface RocketClient
        Type Parameters:
        Req - The type of the request body.
        Res - The type of the response.
        Parameters:
        requestSpec - The specification of the request to be executed.
        Returns:
        The response object.
        Throws:
        RocketRestException - If an error occurs during the request execution.
      • executeAsync

        public <Req,​Res> CompletableFuture<Res> executeAsync​(RequestSpec<Req,​Res> requestSpec)
        Executes an HTTP request asynchronously.
        Type Parameters:
        Req - The type of the request body
        Res - The type of the response
        Parameters:
        requestSpec - The request specification
        Returns:
        A CompletableFuture that will complete with the response
      • shutdown

        public void shutdown()
        Shuts down the executor service.