Class AsyncApiClient


  • public class AsyncApiClient
    extends AbstractApiClient
    Asynchronous API client that returns CompletableFuture for non-blocking operations.

    This client executes HTTP requests asynchronously using a configurable thread pool, allowing the calling thread to continue processing while waiting for responses. Ideal for high-throughput applications or when making multiple concurrent API calls.

    Basic Usage

    
     ExecutorService executor = Executors.newFixedThreadPool(4);
     AsyncApiClient client = new AsyncApiClient("https://api.example.com", config, executor);
    
     // Execute async request
     CompletableFuture<User> future = client.executeAsync(
         RequestBuilder.get("/users/1")
             .responseType(User.class)
             .build()
     );
    
     // Process result when ready
     future.thenAccept(user -> System.out.println("Got: " + user.getName()));
    
     // Don't forget to shutdown
     client.shutdown();
     

    Multiple Concurrent Requests

    
     CompletableFuture<User> user1 = client.executeAsync(getRequest("/users/1"));
     CompletableFuture<User> user2 = client.executeAsync(getRequest("/users/2"));
     CompletableFuture<User> user3 = client.executeAsync(getRequest("/users/3"));
    
     // Wait for all to complete
     CompletableFuture.allOf(user1, user2, user3)
         .thenRun(() -> System.out.println("All users loaded"));
     

    Error Handling

    
     client.executeAsync(request)
         .thenAccept(user -> processUser(user))
         .exceptionally(ex -> {
             System.err.println("Failed: " + ex.getMessage());
             return null;
         });
     
    Since:
    1.0.0
    Author:
    guinetik <guinetik@gmail.com>
    See Also:
    AbstractApiClient, DefaultApiClient, FluentApiClient
    • Constructor Detail

      • AsyncApiClient

        public AsyncApiClient​(String baseUrl,
                              RocketRestConfig config,
                              ExecutorService executor)
        Creates a new AsyncApiClient with the specified base URL, configuration, and executor.
        Parameters:
        baseUrl - The base URL for API requests
        config - The RocketRest configuration
        executor - The ExecutorService to run requests on
      • AsyncApiClient

        public AsyncApiClient​(String baseUrl,
                              RocketRestConfig config,
                              AsyncHttpClient asyncClient)
        Creates a new AsyncApiClient with the specified base URL, configuration, and a pre-configured AsyncHttpClient instance.
        Parameters:
        baseUrl - The base URL for API requests
        config - The RocketRest configuration
        asyncClient - A pre-configured AsyncHttpClient instance
    • Method Detail

      • executeAsync

        public <Req,​Res> CompletableFuture<Res> executeAsync​(RequestSpec<Req,​Res> requestSpec)
        Executes a request asynchronously.
        Type Parameters:
        Req - The type of the request
        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.