Class RequestExecutor
- java.lang.Object
-
- com.cognite.client.servicesV1.executor.RequestExecutor
-
public abstract class RequestExecutor extends Object
This class will execute an okhttp3 request on a separate thread and publish the result via aCompletableFuture. This allows the client code to spin off multiple concurrent request without blocking the main thread. This represents the "blocking IO on a separate thread" pattern, and will work fine for client workloads (limited number of concurrent requests).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRequestExecutor.Builder
-
Field Summary
Fields Modifier and Type Field Description protected org.slf4j.LoggerLOG
-
Constructor Summary
Constructors Constructor Description RequestExecutor()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ResponseBinaryexecuteRequest(okhttp3.Request request)Executes a given request.CompletableFuture<ResponseBinary>executeRequestAsync(okhttp3.Request request)Executes a given request.static RequestExecutorof(okhttp3.OkHttpClient client)RequestExecutorwithExecutor(Executor executor)Sets the executor to use for running the api requests.RequestExecutorwithHttpClient(okhttp3.OkHttpClient client)Sets the http client to use for executing the http requests.RequestExecutorwithMaxRetries(int retries)Sets the maximum number of retries.RequestExecutorwithValidResponseCodes(List<Integer> validResponseCodes)Specifies a set of valid http response codes *in addition* to the 200-range.
-
-
-
Method Detail
-
of
public static RequestExecutor of(okhttp3.OkHttpClient client)
-
withHttpClient
public RequestExecutor withHttpClient(okhttp3.OkHttpClient client)
Sets the http client to use for executing the http requests.- Parameters:
client- the http client to use for requests.- Returns:
- the RequestExecutor with the applied configuration.
-
withExecutor
public RequestExecutor withExecutor(Executor executor)
Sets the executor to use for running the api requests. The default executor is aForkJoinPoolwith a target parallelism of four threads per core.- Parameters:
executor- the executor to use for running the api requests.- Returns:
- the RequestExecutor with the applied configuration.
-
withMaxRetries
public RequestExecutor withMaxRetries(int retries)
Sets the maximum number of retries. The default setting is 3.- Parameters:
retries- the max number of retries- Returns:
- the RequestExecutor with the applied configuration.
-
withValidResponseCodes
public RequestExecutor withValidResponseCodes(List<Integer> validResponseCodes)
Specifies a set of valid http response codes *in addition* to the 200-range. By default, any 2xx response is considered a valid response. By specifying additional codes, this executor will return responses from outside the 200-range. This could be useful in case you want to handle non-200 responses with custom logic. For example, duplicate detection and constraint violations are reported as non-200 responses from the Cognite API.- Parameters:
validResponseCodes- A list of valid response codes.- Returns:
- the RequestExecutor with the applied configuration.
-
executeRequestAsync
public CompletableFuture<ResponseBinary> executeRequestAsync(okhttp3.Request request)
Executes a given request. Checks for transient server errors and retires the request until a valid response is produced, or the max number of retries is reached. This method executes as a blocking I/O operation on a separate thread--the calling thread is not blocked and can continue working on its tasks. Each retry is performed with exponential back-off in case the api is overloaded. If no valid response can be produced, this method will throw an exception.- Parameters:
request- The request to execute- Returns:
- a
CompletableFutureencapsulating the future response.
-
executeRequest
public ResponseBinary executeRequest(okhttp3.Request request) throws Exception
Executes a given request. Checks for transient server errors and retires the request until a valid response is produced, or the max number of retries is reached. This method blocks until aResponseis produced. Each retry is performed with exponential back-off in case the api is overloaded. If no valid response can be produced, this method will throw an exception. The async version of this method isexecuteRequestAsync- Throws:
Exception
-
-