Package com.linecorp.armeria.client
Class ClientBuilder
java.lang.Object
com.linecorp.armeria.client.AbstractClientOptionsBuilder
com.linecorp.armeria.client.ClientBuilder
public final class ClientBuilder extends AbstractClientOptionsBuilder
Creates a new client that connects to the specified
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
.
How are decorators and HTTP headers configured?
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.-
Method Summary
Modifier and Type Method 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.ClientBuilder
auth(BasicToken token)
Sets the HTTP basic access authentication header usingHttpHeaderNames.AUTHORIZATION
.ClientBuilder
auth(OAuth1aToken token)
Sets the OAuth Core 1.0 Revision A header usingHttpHeaderNames.AUTHORIZATION
.ClientBuilder
auth(OAuth2Token token)
Sets the OAuth 2.0 header usingHttpHeaderNames.AUTHORIZATION
.<T> T
build(Class<T> clientType)
Returns a newly-created client which implements the specifiedclientType
, based on the properties of this builder.ClientBuilder
decorator(DecoratingHttpClientFunction decorator)
Adds the specified HTTP-leveldecorator
.ClientBuilder
decorator(Function<? super HttpClient,? extends HttpClient> decorator)
Adds the specified HTTP-leveldecorator
.ClientBuilder
endpointRemapper(Function<? super Endpoint,? extends EndpointGroup> endpointRemapper)
ClientBuilder
factory(ClientFactory factory)
Sets theClientFactory
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 specifiedClientOption
and itsvalue
.<T> ClientBuilder
option(ClientOptionValue<T> optionValue)
Adds the specifiedClientOptionValue
.ClientBuilder
options(ClientOptions options)
Adds the specifiedClientOptions
.ClientBuilder
options(ClientOptionValue<?>... options)
Adds the specifiedClientOptionValue
s.ClientBuilder
options(Iterable<ClientOptionValue<?>> options)
Adds the specifiedClientOptionValue
s.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-leveldecorator
.ClientBuilder
rpcDecorator(Function<? super RpcClient,? extends RpcClient> decorator)
Adds the specified RPC-leveldecorator
.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.Methods inherited from class com.linecorp.armeria.client.AbstractClientOptionsBuilder
buildOptions, buildOptions
-
Method Details
-
build
Returns a newly-created client which implements the specifiedclientType
, based on the properties of this builder.- Throws:
IllegalArgumentException
- if the scheme of theuri
specified inClients.builder(String)
or the specifiedclientType
is unsupported for the scheme
-
options
Description copied from class:AbstractClientOptionsBuilder
Adds the specifiedClientOptions
.- Overrides:
options
in classAbstractClientOptionsBuilder
-
options
Description copied from class:AbstractClientOptionsBuilder
Adds the specifiedClientOptionValue
s.- Overrides:
options
in classAbstractClientOptionsBuilder
-
options
Description copied from class:AbstractClientOptionsBuilder
Adds the specifiedClientOptionValue
s.- Overrides:
options
in classAbstractClientOptionsBuilder
-
option
Description copied from class:AbstractClientOptionsBuilder
Adds the specifiedClientOption
and itsvalue
.- Overrides:
option
in classAbstractClientOptionsBuilder
-
option
Description copied from class:AbstractClientOptionsBuilder
Adds the specifiedClientOptionValue
.- Overrides:
option
in classAbstractClientOptionsBuilder
-
factory
Description copied from class:AbstractClientOptionsBuilder
Sets theClientFactory
used for creating a client. The default isClientFactory.ofDefault()
.- Overrides:
factory
in classAbstractClientOptionsBuilder
-
writeTimeout
Description copied from class:AbstractClientOptionsBuilder
Sets the timeout of a socket write attempt.- Overrides:
writeTimeout
in classAbstractClientOptionsBuilder
- Parameters:
writeTimeout
- the timeout.0
disables the timeout.
-
writeTimeoutMillis
Description copied from class:AbstractClientOptionsBuilder
Sets the timeout of a socket write attempt in milliseconds.- Overrides:
writeTimeoutMillis
in classAbstractClientOptionsBuilder
- Parameters:
writeTimeoutMillis
- the timeout in milliseconds.0
disables the timeout.
-
responseTimeout
Description copied from class:AbstractClientOptionsBuilder
Sets the timeout of a response.- Overrides:
responseTimeout
in classAbstractClientOptionsBuilder
- Parameters:
responseTimeout
- the timeout.0
disables the timeout.
-
responseTimeoutMillis
Description copied from class:AbstractClientOptionsBuilder
Sets the timeout of a response in milliseconds.- Overrides:
responseTimeoutMillis
in classAbstractClientOptionsBuilder
- Parameters:
responseTimeoutMillis
- the timeout in milliseconds.0
disables the timeout.
-
maxResponseLength
Description copied from class:AbstractClientOptionsBuilder
Sets the maximum allowed length of a server response in bytes.- Overrides:
maxResponseLength
in classAbstractClientOptionsBuilder
- Parameters:
maxResponseLength
- the maximum length in bytes.0
disables the limit.
-
requestIdGenerator
Description copied from class:AbstractClientOptionsBuilder
- Overrides:
requestIdGenerator
in classAbstractClientOptionsBuilder
-
endpointRemapper
public ClientBuilder endpointRemapper(Function<? super Endpoint,? extends EndpointGroup> endpointRemapper)Description copied from class:AbstractClientOptionsBuilder
Sets aFunction
that remaps anEndpoint
into anEndpointGroup
. ThisClientOption
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.
- Overrides:
endpointRemapper
in classAbstractClientOptionsBuilder
- See Also:
ClientOption.ENDPOINT_REMAPPER
,ClientOptions.endpointRemapper()
-
decorator
Description copied from class:AbstractClientOptionsBuilder
Adds the specified HTTP-leveldecorator
.- Overrides:
decorator
in classAbstractClientOptionsBuilder
- Parameters:
decorator
- theFunction
that transforms anHttpClient
to another
-
decorator
Description copied from class:AbstractClientOptionsBuilder
Adds the specified HTTP-leveldecorator
.- Overrides:
decorator
in classAbstractClientOptionsBuilder
- Parameters:
decorator
- theDecoratingHttpClientFunction
that intercepts an invocation
-
rpcDecorator
Description copied from class:AbstractClientOptionsBuilder
Adds the specified RPC-leveldecorator
.- Overrides:
rpcDecorator
in classAbstractClientOptionsBuilder
- Parameters:
decorator
- theFunction
that transforms anRpcClient
to another
-
rpcDecorator
Description copied from class:AbstractClientOptionsBuilder
Adds the specified RPC-leveldecorator
.- Overrides:
rpcDecorator
in classAbstractClientOptionsBuilder
- Parameters:
decorator
- theDecoratingRpcClientFunction
that intercepts an invocation
-
addHttpHeader
Description copied from class:AbstractClientOptionsBuilder
Adds the specified HTTP header.- Overrides:
addHttpHeader
in classAbstractClientOptionsBuilder
-
addHttpHeaders
public ClientBuilder addHttpHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> httpHeaders)Description copied from class:AbstractClientOptionsBuilder
Adds the specified HTTP headers.- Overrides:
addHttpHeaders
in classAbstractClientOptionsBuilder
-
setHttpHeader
Description copied from class:AbstractClientOptionsBuilder
Sets the specified HTTP header.- Overrides:
setHttpHeader
in classAbstractClientOptionsBuilder
-
setHttpHeaders
public ClientBuilder setHttpHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> httpHeaders)Description copied from class:AbstractClientOptionsBuilder
Sets the specified HTTP headers.- Overrides:
setHttpHeaders
in classAbstractClientOptionsBuilder
-
auth
Description copied from class:AbstractClientOptionsBuilder
Sets the HTTP basic access authentication header usingHttpHeaderNames.AUTHORIZATION
.- Overrides:
auth
in classAbstractClientOptionsBuilder
-
auth
Description copied from class:AbstractClientOptionsBuilder
Sets the OAuth Core 1.0 Revision A header usingHttpHeaderNames.AUTHORIZATION
.- Overrides:
auth
in classAbstractClientOptionsBuilder
-
auth
Description copied from class:AbstractClientOptionsBuilder
Sets the OAuth 2.0 header usingHttpHeaderNames.AUTHORIZATION
.- Overrides:
auth
in classAbstractClientOptionsBuilder
-