Package com.guinetik.rr.http
Class AsyncHttpClient
- java.lang.Object
-
- com.guinetik.rr.http.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
RocketClientimplementation and provides non-blocking request execution viaCompletableFuture.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 Summary
Constructors Constructor Description AsyncHttpClient(RocketClient delegate, ExecutorService executor)Creates a new AsyncHttpClient with the specified delegate client and executor.AsyncHttpClient(String baseUrl, RocketRestOptions clientOptions, ExecutorService executor)Creates a new AsyncHttpClient with a DefaultHttpClient as the delegate and client options.AsyncHttpClient(String baseUrl, ExecutorService executor)Creates a new AsyncHttpClient with a DefaultHttpClient as the delegate.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidconfigureSsl(SSLContext sslContext)Sets the SSL context to be used for HTTPS requests.<Req,Res>
Resexecute(RequestSpec<Req,Res> requestSpec)Executes an HTTP request based on the provided request specification.<Req,Res>
CompletableFuture<Res>executeAsync(RequestSpec<Req,Res> requestSpec)Executes an HTTP request asynchronously.voidsetBaseUrl(String baseUrl)voidshutdown()Shuts down the executor service.
-
-
-
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 toexecutor- 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 requestsexecutor- 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 requestsclientOptions- The client optionsexecutor- The executor service to run requests on
-
-
Method Detail
-
configureSsl
public void configureSsl(SSLContext sslContext)
Description copied from interface:RocketClientSets the SSL context to be used for HTTPS requests.- Specified by:
configureSslin interfaceRocketClient- Parameters:
sslContext- The SSL context to use.
-
setBaseUrl
public void setBaseUrl(String baseUrl)
- Specified by:
setBaseUrlin interfaceRocketClient
-
execute
public <Req,Res> Res execute(RequestSpec<Req,Res> requestSpec) throws RocketRestException
Description copied from interface:RocketClientExecutes an HTTP request based on the provided request specification.- Specified by:
executein interfaceRocketClient- 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 bodyRes- 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.
-
-