io.sphere.sdk.meta.SphereClientTuningDocumentation Maven / Gradle / Ivy
package io.sphere.sdk.meta;
import com.ning.http.client.AsyncHttpClient;
import io.sphere.sdk.client.SphereRequest;
/**
Blocking Client
Usage of the Blocking Client
In a lot of frameworks there is no support for asynchronous execution and so it is necessary to wait for the responses.
The {@link io.sphere.sdk.client.BlockingSphereClient} can wait for responses with {@link io.sphere.sdk.client.BlockingSphereClient#executeBlocking(SphereRequest)}. This method enforces a timeout for resilience
and throws directly {@link io.sphere.sdk.models.SphereException}s. For creation refer to {@link io.sphere.sdk.client.BlockingSphereClient}.
{@include.example io.sphere.sdk.meta.BlockingClientValueGetDemo}
Examples of bad usages of the default client in a blocking manner
Here follow some examples how to NOT use the {@link io.sphere.sdk.client.SphereClient}:
This examples lacks a timeout, so in an unfortunate case the thread is blocked forever:
{@include.example io.sphere.sdk.client.WrongBlockingWithJoin}
The following examples are even worse because they lack a timeout and they need to deal with checked exceptions:
{@include.example io.sphere.sdk.client.WrongBlockingWithGetAndSwallowing}
{@include.example io.sphere.sdk.client.WrongBlockingWithGetAndSignature}
Timeout Client
See {@link io.sphere.sdk.client.TimeoutSphereClientDecorator}.
Limit the amount of parallel requests
See {@link io.sphere.sdk.client.QueueSphereClientDecorator}.
Using design patterns to add functionality to the clients
The clients are interfaces which have a default implementation (add "Impl" to the interface name).
This enables you to use the decorator pattern to configure the cross concern behaviour of the client:
- setup recover mechanisms like returning empty lists or retry the request
- log events
- set timeouts (depending on the future implementation)
- return fake answers for tests
- configure throttling.
The following listing shows a pimped client which updates metrics on responses, retries commands and sets default values:
{@include.example io.sphere.sdk.client.WrappedClientDemo}
Configure the underlying http client.
{@link io.sphere.sdk.http.HttpClient} is an abstraction to perform http requests.
{@link io.sphere.sdk.http.AsyncHttpClientAdapter#of(AsyncHttpClient)} wraps a ning {@link AsyncHttpClient} as {@link io.sphere.sdk.http.HttpClient}.
The following example creates a configured HTTP client and initializes a {@link io.sphere.sdk.client.SphereClient} with it.
{@include.example io.sphere.sdk.client.CustomClientConfigDemoTest}
For configuration parameters refer to github.com/AsyncHttpClient/async-http-client.
*/
public class SphereClientTuningDocumentation {
private SphereClientTuningDocumentation() {
}
}