public interface BlockingSphereClient extends SphereClient
Creation requires a SphereClient
and you specify the general timeout for all requests:
public class BlockingClientCreationDemo {
public static BlockingSphereClient createBlockingClient(final SphereClient sphereClient) {
return BlockingSphereClient.of(sphereClient, 500, TimeUnit.MILLISECONDS);
}
}
See the test code.
Get a value blocking with executeBlocking(SphereRequest)
:
public class BlockingClientValueGetDemo {
public static void demo(final BlockingSphereClient client, final String expectedProjectKey) {
final SphereRequest<Project> sphereRequest = ProjectGet.of();
final Project project = client.executeBlocking(sphereRequest);//returns result directly, no CompletionStage
assertThat(project.getKey()).isEqualTo(expectedProjectKey);
}
}
See the test code.
Get a value without blocking like in SphereClient.execute(SphereRequest)
:
public class BlockingClientValueAsyncGetDemo {
public static void demo(final BlockingSphereClient client, final String expectedProjectKey) {
final SphereRequest<Project> sphereRequest = ProjectGet.of();
final CompletionStage<Project> projectCompletionStage = client.execute(sphereRequest);
assertThat(client)
.as("blocking client can also used as regular sphere client")
.isInstanceOf(SphereClient.class)
.isInstanceOf(BlockingSphereClient.class);
}
}
See the test code.
SphereTimeoutException
occurs if the answer is not available in the configured timeout:
final BlockingSphereClient blockingSphereClient = createBlockingSphereClient();//reusable instance among threads
final Throwable throwable = catchThrowable(() -> blockingSphereClient.executeBlocking(DummySphereRequest.of()));
assertThat(throwable).isInstanceOf(SphereTimeoutException.class);
See the test code.
In case of errors sphere exceptions are directly thrown:
public class BlockingClientSphereExceptionDemo {
public static void demo(final BlockingSphereClient client) {
final CartCreateCommand sphereRequest = CartCreateCommand.of(CartDraft.of(null));
final Throwable throwable = catchThrowable(() -> client.executeBlocking(sphereRequest));
assertThat(throwable)
.isInstanceOf(ErrorResponseException.class)
.isInstanceOf(SphereException.class);
}
}
See the test code.
Modifier and Type | Method and Description |
---|---|
void |
close()
Shuts down the client to save resources like connections and threads.
|
<T> CompletionStage<T> |
execute(SphereRequest<T> sphereRequest)
Executes asynchronously a request to Composable Commerce.
|
<T> T |
executeBlocking(SphereRequest<T> sphereRequest)
Executes a request and blocks the caller thread until the response is available or a client specific timeout occurs.
|
<T> T |
executeBlocking(SphereRequest<T> sphereRequest,
Duration duration)
Executes a request and blocks the caller thread until the response is available or a request specific timeout occurs.
|
<T> T |
executeBlocking(SphereRequest<T> sphereRequest,
long timeout,
TimeUnit unit)
Executes a request and blocks the caller thread until the response is available or a request specific timeout occurs.
|
static BlockingSphereClient |
of(SphereClient delegate,
Duration defaultTimeout)
Creates a blocking client with a configured default timeout for blocking requests.
|
static BlockingSphereClient |
of(SphereClient delegate,
long defaultTimeout,
TimeUnit unit)
Creates a blocking client with a configured default timeout for blocking requests.
|
getConfig, of, of
void close()
SphereClient
close
in interface AutoCloseable
close
in interface SphereClient
<T> CompletionStage<T> execute(SphereRequest<T> sphereRequest)
SphereClient
execute
in interface SphereClient
T
- type of the result for the requestsphereRequest
- request to Composable Commerce to perform<T> T executeBlocking(SphereRequest<T> sphereRequest)
T
- type of the result for the requestsphereRequest
- request to Composable Commerce to performSphereTimeoutException
- if a timeout occurs<T> T executeBlocking(SphereRequest<T> sphereRequest, long timeout, TimeUnit unit)
T
- type of the result for the requestsphereRequest
- request to Composable Commerce to performtimeout
- the maximum time to wait for this single requestunit
- the time unit of the timeout argumentSphereTimeoutException
- if a timeout occurs<T> T executeBlocking(SphereRequest<T> sphereRequest, Duration duration)
T
- type of the result for the requestsphereRequest
- request to Composable Commerce to performduration
- the maximum duration to wait for this single requestSphereTimeoutException
- if a timeout occursstatic BlockingSphereClient of(SphereClient delegate, long defaultTimeout, TimeUnit unit)
delegate
- underlying sphere client which may be initialized with the SphereClientFactory
.defaultTimeout
- the default maximum time to wait (to block the thread) which should be greater than the timeout of the underlying HTTP clientunit
- the time unit of the defaultTimeout argumentstatic BlockingSphereClient of(SphereClient delegate, Duration defaultTimeout)
delegate
- underlying sphere client which may be initialized with the SphereClientFactory
.defaultTimeout
- the default maximum duration to wait (to block the thread) which should be greater than the timeout of the underlying HTTP client