public final class ClientBuilder extends Object
URI using the builder pattern. Use the factory
methods in Clients if you do not have many options to override. If you are creating an
WebClient, it is recommended to use the WebClientBuilder or
factory methods in WebClient.
Unlike other options, when a user calls option(ClientOption, Object) or options() with
a ClientOption.DECORATION or a ClientOption.HTTP_HEADERS, this builder will not simply
replace the old option but merge the specified option into the previous option value. For example:
ClientOptionsBuilder b = ClientOptions.builder();
b.option(ClientOption.HTTP_HEADERS, headersA);
b.option(ClientOption.HTTP_HEADERS, headersB);
b.option(ClientOption.DECORATION, decorationA);
b.option(ClientOption.DECORATION, decorationB);
ClientOptions opts = b.build();
HttpHeaders httpHeaders = opts.httpHeaders();
ClientDecoration decorations = opts.decoration();
httpHeaders will contain all HTTP headers of headersA and headersB.
If headersA and headersB have the headers with the same name, the duplicate header in
headerB will replace the one with the same name in headerA.
Similarly, decorations will contain all decorators of decorationA and decorationB,
but there will be no replacement but only addition.| Constructor and Description |
|---|
ClientBuilder(Scheme scheme,
Endpoint endpoint)
Deprecated.
|
ClientBuilder(SessionProtocol protocol,
Endpoint endpoint)
Deprecated.
|
ClientBuilder(String uri)
Deprecated.
|
ClientBuilder(String scheme,
Endpoint endpoint)
Deprecated.
|
ClientBuilder(URI uri)
Deprecated.
Use
Clients.builder(URI). |
| Modifier and Type | Method and Description |
|---|---|
ClientBuilder |
addHttpHeader(CharSequence name,
Object value)
Adds the specified HTTP header.
|
ClientBuilder |
addHttpHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> httpHeaders)
Adds the specified HTTP headers.
|
<T> T |
build(Class<T> clientType)
Returns a newly-created client which implements the specified
clientType, based on the
properties of this builder. |
ClientBuilder |
decorator(DecoratingHttpClientFunction decorator)
Adds the specified HTTP-level
decorator. |
ClientBuilder |
decorator(Function<? super HttpClient,? extends HttpClient> decorator)
Adds the specified HTTP-level
decorator. |
ClientBuilder |
defaultMaxResponseLength(long maxResponseLength)
Deprecated.
|
ClientBuilder |
defaultResponseTimeout(Duration responseTimeout)
Deprecated.
|
ClientBuilder |
defaultResponseTimeoutMillis(long responseTimeoutMillis)
Deprecated.
|
ClientBuilder |
defaultWriteTimeout(Duration writeTimeout)
Deprecated.
|
ClientBuilder |
defaultWriteTimeoutMillis(long writeTimeoutMillis)
Deprecated.
|
ClientBuilder |
endpointRemapper(Function<? super Endpoint,? extends EndpointGroup> endpointRemapper)
|
ClientBuilder |
factory(ClientFactory factory)
Sets the
ClientFactory used for creating a client. |
ClientBuilder |
maxResponseLength(long maxResponseLength)
Sets the maximum allowed length of a server response in bytes.
|
<T> ClientBuilder |
option(ClientOption<T> option,
T value)
Adds the specified
ClientOption and its value. |
<T> ClientBuilder |
option(ClientOptionValue<T> optionValue)
Adds the specified
ClientOptionValue. |
ClientBuilder |
options(ClientOptions options)
Adds the specified
ClientOptions. |
ClientBuilder |
options(ClientOptionValue<?>... options)
Adds the specified
ClientOptionValues. |
ClientBuilder |
options(Iterable<ClientOptionValue<?>> options)
Adds the specified
ClientOptionValues. |
ClientBuilder |
path(String path)
Sets the
path of the client. |
ClientBuilder |
requestIdGenerator(Supplier<RequestId> requestIdGenerator)
|
ClientBuilder |
responseTimeout(Duration responseTimeout)
Sets the timeout of a response.
|
ClientBuilder |
responseTimeoutMillis(long responseTimeoutMillis)
Sets the timeout of a response in milliseconds.
|
ClientBuilder |
rpcDecorator(DecoratingRpcClientFunction decorator)
Adds the specified RPC-level
decorator. |
ClientBuilder |
rpcDecorator(Function<? super RpcClient,? extends RpcClient> decorator)
Adds the specified RPC-level
decorator. |
ClientBuilder |
serializationFormat(SerializationFormat format)
Sets the
SerializationFormat of the client. |
ClientBuilder |
setHttpHeader(CharSequence name,
Object value)
Sets the specified HTTP header.
|
ClientBuilder |
setHttpHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> httpHeaders)
Sets the specified HTTP headers.
|
ClientBuilder |
writeTimeout(Duration writeTimeout)
Sets the timeout of a socket write attempt.
|
ClientBuilder |
writeTimeoutMillis(long writeTimeoutMillis)
Sets the timeout of a socket write attempt in milliseconds.
|
@Deprecated public ClientBuilder(String uri)
Clients.builder(String).ClientBuilder that builds the client that connects to the specified uri.@Deprecated public ClientBuilder(URI uri)
Clients.builder(URI).ClientBuilder that builds the client that connects to the specified URI.@Deprecated public ClientBuilder(String scheme, Endpoint endpoint)
Clients.builder(String, EndpointGroup).ClientBuilder that builds the client that connects to the specified
Endpoint with the scheme.@Deprecated public ClientBuilder(Scheme scheme, Endpoint endpoint)
Clients.builder(Scheme, EndpointGroup).ClientBuilder that builds the client that connects to the specified
Endpoint with the Scheme.@Deprecated public ClientBuilder(SessionProtocol protocol, Endpoint endpoint)
Clients.builder(SessionProtocol, EndpointGroup).ClientBuilder that builds the client that connects to the specified
Endpoint with the SessionProtocol.public ClientBuilder path(String path)
path of the client.public ClientBuilder serializationFormat(SerializationFormat format)
SerializationFormat of the client. The default is SerializationFormat.NONE.public <T> T build(Class<T> clientType)
clientType, based on the
properties of this builder.IllegalArgumentException - if the scheme of the uri specified in
Clients.builder(String) or the specified clientType is
unsupported for the schemepublic ClientBuilder options(ClientOptions options)
ClientOptions.public ClientBuilder options(ClientOptionValue<?>... options)
ClientOptionValues.public ClientBuilder options(Iterable<ClientOptionValue<?>> options)
ClientOptionValues.public <T> ClientBuilder option(ClientOption<T> option, T value)
ClientOption and its value.public <T> ClientBuilder option(ClientOptionValue<T> optionValue)
ClientOptionValue.public ClientBuilder factory(ClientFactory factory)
ClientFactory used for creating a client.
The default is ClientFactory.ofDefault().@Deprecated public ClientBuilder defaultWriteTimeout(Duration writeTimeout)
writeTimeout - the timeout. 0 disables the timeout.@Deprecated public ClientBuilder defaultWriteTimeoutMillis(long writeTimeoutMillis)
writeTimeoutMillis - the timeout in milliseconds. 0 disables the timeout.public ClientBuilder writeTimeout(Duration writeTimeout)
writeTimeout - the timeout. 0 disables the timeout.public ClientBuilder writeTimeoutMillis(long writeTimeoutMillis)
writeTimeoutMillis - the timeout in milliseconds. 0 disables the timeout.@Deprecated public ClientBuilder defaultResponseTimeout(Duration responseTimeout)
responseTimeout - the timeout. 0 disables the timeout.@Deprecated public ClientBuilder defaultResponseTimeoutMillis(long responseTimeoutMillis)
responseTimeoutMillis - the timeout in milliseconds. 0 disables the timeout.public ClientBuilder responseTimeout(Duration responseTimeout)
responseTimeout - the timeout. 0 disables the timeout.public ClientBuilder responseTimeoutMillis(long responseTimeoutMillis)
responseTimeoutMillis - the timeout in milliseconds. 0 disables the timeout.@Deprecated public ClientBuilder defaultMaxResponseLength(long maxResponseLength)
maxResponseLength - the maximum length in bytes. 0 disables the limit.public ClientBuilder maxResponseLength(long maxResponseLength)
maxResponseLength - the maximum length in bytes. 0 disables the limit.public ClientBuilder requestIdGenerator(Supplier<RequestId> requestIdGenerator)
public ClientBuilder endpointRemapper(Function<? super Endpoint,? extends EndpointGroup> endpointRemapper)
Function that remaps an Endpoint into an EndpointGroup.
This ClientOption is useful when you need to override a single target host into
a group of hosts to enable client-side load-balancing, e.g.
MyService.Iface client =
Clients.newClient("tbinary+http://example.com/api",
MyService.Iface.class);
EndpointGroup myGroup = EndpointGroup.of(Endpoint.of("node-1.example.com")),
Endpoint.of("node-2.example.com")));
MyService.Iface derivedClient =
Clients.newDerivedClient(client, options -> {
return options.toBuilder()
.endpointRemapper(endpoint -> {
if (endpoint.host().equals("example.com")) {
return myGroup;
} else {
return endpoint;
}
})
.build();
});
// This request goes to 'node-1.example.com' or 'node-2.example.com'.
derivedClient.call();
Note that the remapping does not occur recursively but only once.
public ClientBuilder decorator(Function<? super HttpClient,? extends HttpClient> decorator)
decorator.decorator - the Function that transforms an HttpClient to anotherpublic ClientBuilder decorator(DecoratingHttpClientFunction decorator)
decorator.decorator - the DecoratingHttpClientFunction that intercepts an invocationpublic ClientBuilder rpcDecorator(Function<? super RpcClient,? extends RpcClient> decorator)
decorator.public ClientBuilder rpcDecorator(DecoratingRpcClientFunction decorator)
decorator.decorator - the DecoratingRpcClientFunction that intercepts an invocationpublic ClientBuilder addHttpHeader(CharSequence name, Object value)
public ClientBuilder addHttpHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> httpHeaders)
public ClientBuilder setHttpHeader(CharSequence name, Object value)
public ClientBuilder setHttpHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> httpHeaders)
Copyright © 2020 LeanCloud. All rights reserved.