Class RequestFuture<T>
java.lang.Object
org.apache.kafka.clients.consumer.internals.RequestFuture<T>
- Type Parameters:
T- Return type of the result (Can be Void if there is no response)
- All Implemented Interfaces:
ConsumerNetworkClient.PollCondition
Result of an asynchronous request from
ConsumerNetworkClient. Use ConsumerNetworkClient.poll(Timer)
(and variants) to finish a request future. Use isDone() to check if the future is complete, and
succeeded() to check if the request completed successfully. Typical usage might look like this:
RequestFuture future = client.send(api, request);
client.poll(future);
if (future.succeeded()) {
ClientResponse response = future.value();
// Handle response
} else {
throw future.exception();
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(RequestFutureListener<T> listener) Add a listener which will be notified when the future completesbooleanvoidchain(RequestFuture<T> future) voidComplete the request successfully.<S> RequestFuture<S> compose(RequestFutureAdapter<T, S> adapter) Convert from a request future of one type to another typestatic <T> RequestFuture<T> Get the exception from a failed result (only available if the request failed)booleanfailed()Check if the request failed.static <T> RequestFuture<T> booleanisDone()Check whether the response is ready to be handledbooleanCheck if the request is retriable (convenience method for checking if the exception is an instance ofRetriableException.static <T> RequestFuture<T> voidRaise an exception.voidraise(org.apache.kafka.common.protocol.Errors error) Raise an error.booleanReturn whether the caller is still awaiting an IO event.booleanCheck if the request succeeded;value()Get the value corresponding to this request (only available if the request succeeded)static RequestFuture<Void>
-
Constructor Details
-
RequestFuture
public RequestFuture()
-
-
Method Details
-
isDone
public boolean isDone()Check whether the response is ready to be handled- Returns:
- true if the response is ready, false otherwise
-
awaitDone
- Throws:
InterruptedException
-
value
Get the value corresponding to this request (only available if the request succeeded)- Returns:
- the value set in
complete(Object) - Throws:
IllegalStateException- if the future is not complete or failed
-
succeeded
public boolean succeeded()Check if the request succeeded;- Returns:
- true if the request completed and was successful
-
failed
public boolean failed()Check if the request failed.- Returns:
- true if the request completed with a failure
-
isRetriable
public boolean isRetriable()Check if the request is retriable (convenience method for checking if the exception is an instance ofRetriableException.- Returns:
- true if it is retriable, false otherwise
- Throws:
IllegalStateException- if the future is not complete or completed successfully
-
exception
Get the exception from a failed result (only available if the request failed)- Returns:
- the exception set in
raise(RuntimeException) - Throws:
IllegalStateException- if the future is not complete or completed successfully
-
complete
Complete the request successfully. After this call,succeeded()will return true and the value can be obtained throughvalue().- Parameters:
value- corresponding value (or null if there is none)- Throws:
IllegalStateException- if the future has already been completedIllegalArgumentException- if the argument is an instance ofRuntimeException
-
raise
Raise an exception. The request will be marked as failed, and the caller can either handle the exception or throw it.- Parameters:
e- corresponding exception to be passed to caller- Throws:
IllegalStateException- if the future has already been completed
-
raise
public void raise(org.apache.kafka.common.protocol.Errors error) Raise an error. The request will be marked as failed.- Parameters:
error- corresponding error to be passed to caller
-
addListener
Add a listener which will be notified when the future completes- Parameters:
listener- non-null listener to add
-
compose
Convert from a request future of one type to another type- Type Parameters:
S- The type of the future adapted to- Parameters:
adapter- The adapter which does the conversion- Returns:
- The new future
-
chain
-
failure
-
voidSuccess
-
coordinatorNotAvailable
-
noBrokersAvailable
-
shouldBlock
public boolean shouldBlock()Description copied from interface:ConsumerNetworkClient.PollConditionReturn whether the caller is still awaiting an IO event.- Specified by:
shouldBlockin interfaceConsumerNetworkClient.PollCondition- Returns:
- true if so, false otherwise.
-