Package com.guinetik.rr.api
Class AsyncApiClient
- java.lang.Object
-
- com.guinetik.rr.api.AbstractApiClient
-
- com.guinetik.rr.api.AsyncApiClient
-
public class AsyncApiClient extends AbstractApiClient
Asynchronous API client that returnsCompletableFuturefor 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
-
-
Field Summary
-
Fields inherited from class com.guinetik.rr.api.AbstractApiClient
baseUrl, config, httpClient, logger, options
-
-
Constructor Summary
Constructors Constructor Description AsyncApiClient(String baseUrl, RocketRestConfig config, AsyncHttpClient asyncClient)Creates a new AsyncApiClient with the specified base URL, configuration, and a pre-configured AsyncHttpClient instance.AsyncApiClient(String baseUrl, RocketRestConfig config, ExecutorService executor)Creates a new AsyncApiClient with the specified base URL, configuration, and executor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <Req,Res>
CompletableFuture<Res>executeAsync(RequestSpec<Req,Res> requestSpec)Executes a request asynchronously.voidshutdown()Shuts down the executor service.-
Methods inherited from class com.guinetik.rr.api.AbstractApiClient
configure, createHeaders, execute, executeWithRetry, executeWithTiming, logRequest, refreshToken, setBaseUrl
-
-
-
-
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 requestsconfig- The RocketRest configurationexecutor- 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 requestsconfig- The RocketRest configurationasyncClient- 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 requestRes- 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.
-
-