Class AbstractClientOptionsBuilder
- Direct Known Subclasses:
AbstractWebClientBuilder,ArmeriaRetrofitBuilder,ClientBuilder,ClientOptionsBuilder,GrpcClientBuilder,ThriftClientBuilder
ClientOptions.-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a new instance.protectedCreates a new instance with the specified base options. -
Method Summary
Modifier and TypeMethodDescriptionaddHeader(CharSequence name, Object value) Adds the default HTTP header for anHttpRequestthat will be sent by thisClient.addHeaders(Iterable<? extends Map.Entry<? extends CharSequence, ?>> headers) Adds the default HTTP headers for anHttpRequestthat will be sent by thisClient.Sets theAuthTokenheader usingHttpHeaderNames.AUTHORIZATION.auth(BasicToken token) Deprecated.auth(OAuth1aToken token) Deprecated.Useauth(AuthToken)instead.auth(OAuth2Token token) Deprecated.Useauth(AuthToken)instead.protected final ClientOptionsBuildsClientOptionswith the given options and the default options.protected final ClientOptionsbuildOptions(@Nullable ClientOptions baseOptions) BuildsClientOptionswith the specifiedbaseOptionsand the options which were set to this builder.Clears all HTTP-level and RPC-level decorators set so far.contextCustomizer(Consumer<? super ClientRequestContext> contextCustomizer) Adds the specifiedClientRequestContextcustomizer function so that it customizes the context in the thread that initiated the client call.decorator(DecoratingHttpClientFunction decorator) Adds the specified HTTP-leveldecorator.decorator(Function<? super HttpClient, ? extends HttpClient> decorator) Adds the specified HTTP-leveldecorator.endpointRemapper(Function<? super Endpoint, ? extends EndpointGroup> endpointRemapper) factory(ClientFactory factory) Sets theClientFactoryused for creating a client.Enables automatic redirection.followRedirects(RedirectConfig redirectConfig) Sets theRedirectConfigto enable automatic redirection.maxResponseLength(long maxResponseLength) Sets the maximum allowed length of a server response in bytes.option(ClientOption<T> option, T value) Adds the specifiedClientOptionand itsvalue.option(ClientOptionValue<T> optionValue) Adds the specifiedClientOptionValue.options(ClientOptions options) Adds the specifiedClientOptions.options(ClientOptionValue<?>... options) Adds the specifiedClientOptionValues.options(Iterable<ClientOptionValue<?>> options) Adds the specifiedClientOptionValues.requestIdGenerator(Supplier<RequestId> requestIdGenerator) responseTimeout(Duration responseTimeout) Sets the timeout of a response.responseTimeoutMillis(long responseTimeoutMillis) Sets the timeout of a response in milliseconds.rpcDecorator(DecoratingRpcClientFunction decorator) Adds the specified RPC-leveldecorator.rpcDecorator(Function<? super RpcClient, ? extends RpcClient> decorator) Adds the specified RPC-leveldecorator.setHeader(CharSequence name, Object value) Sets the default HTTP header for anHttpRequestthat will be sent by thisClient.setHeaders(Iterable<? extends Map.Entry<? extends CharSequence, ?>> headers) Sets the default HTTP headers for anHttpRequestthat will be sent by thisClient.successFunction(SuccessFunction successFunction) Sets aSuccessFunctionthat determines whether a request was handled successfully or not.writeTimeout(Duration writeTimeout) Sets the timeout of a socket write attempt.writeTimeoutMillis(long writeTimeoutMillis) Sets the timeout of a socket write attempt in milliseconds.
-
Constructor Details
-
AbstractClientOptionsBuilder
protected AbstractClientOptionsBuilder()Creates a new instance. -
AbstractClientOptionsBuilder
Creates a new instance with the specified base options.
-
-
Method Details
-
options
Adds the specifiedClientOptions. -
options
Adds the specifiedClientOptionValues. -
options
Adds the specifiedClientOptionValues. -
option
Adds the specifiedClientOptionand itsvalue. -
option
Adds the specifiedClientOptionValue. -
factory
Sets theClientFactoryused for creating a client. The default isClientFactory.ofDefault(). -
writeTimeout
Sets the timeout of a socket write attempt.- Parameters:
writeTimeout- the timeout.0disables the timeout.
-
writeTimeoutMillis
Sets the timeout of a socket write attempt in milliseconds.- Parameters:
writeTimeoutMillis- the timeout in milliseconds.0disables the timeout.
-
responseTimeout
Sets the timeout of a response.- Parameters:
responseTimeout- the timeout.0disables the timeout.
-
responseTimeoutMillis
Sets the timeout of a response in milliseconds.- Parameters:
responseTimeoutMillis- the timeout in milliseconds.0disables the timeout.
-
maxResponseLength
Sets the maximum allowed length of a server response in bytes.- Parameters:
maxResponseLength- the maximum length in bytes.0disables the limit.
-
requestIdGenerator
-
successFunction
Sets aSuccessFunctionthat determines whether a request was handled successfully or not. If unspecified,SuccessFunction.ofDefault()is used. -
endpointRemapper
public AbstractClientOptionsBuilder endpointRemapper(Function<? super Endpoint, ? extends EndpointGroup> endpointRemapper) Sets aFunctionthat remaps anEndpointinto anEndpointGroup. ThisClientOptionis 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.
-
decorator
public AbstractClientOptionsBuilder decorator(Function<? super HttpClient, ? extends HttpClient> decorator) Adds the specified HTTP-leveldecorator.- Parameters:
decorator- theFunctionthat transforms anHttpClientto another
-
decorator
Adds the specified HTTP-leveldecorator.- Parameters:
decorator- theDecoratingHttpClientFunctionthat intercepts an invocation
-
clearDecorators
Clears all HTTP-level and RPC-level decorators set so far. -
rpcDecorator
public AbstractClientOptionsBuilder rpcDecorator(Function<? super RpcClient, ? extends RpcClient> decorator) Adds the specified RPC-leveldecorator. -
rpcDecorator
Adds the specified RPC-leveldecorator.- Parameters:
decorator- theDecoratingRpcClientFunctionthat intercepts an invocation
-
addHeader
Adds the default HTTP header for anHttpRequestthat will be sent by thisClient.Note that the values of the default HTTP headers could be overridden if the same
HttpHeaderNamesare defined in theHttpRequest.headers()orClientRequestContext.additionalRequestHeaders(). -
addHeaders
public AbstractClientOptionsBuilder addHeaders(Iterable<? extends Map.Entry<? extends CharSequence, ?>> headers) Adds the default HTTP headers for anHttpRequestthat will be sent by thisClient.Note that the values of the default HTTP headers could be overridden if the same
HttpHeaderNamesare defined in theHttpRequest.headers()orClientRequestContext.additionalRequestHeaders(). -
setHeader
Sets the default HTTP header for anHttpRequestthat will be sent by thisClient.Note that the default HTTP header could be overridden if the same
HttpHeaderNamesare defined inHttpRequest.headers()orClientRequestContext.additionalRequestHeaders(). -
setHeaders
public AbstractClientOptionsBuilder setHeaders(Iterable<? extends Map.Entry<? extends CharSequence, ?>> headers) Sets the default HTTP headers for anHttpRequestthat will be sent by thisClient.Note that the values of the default HTTP headers could be overridden if the same
HttpHeaderNamesare defined inHttpRequest.headers()orClientRequestContext.additionalRequestHeaders(). -
auth
Deprecated.Useauth(AuthToken)instead.Sets the HTTP basic access authentication header usingHttpHeaderNames.AUTHORIZATION. -
auth
Deprecated.Useauth(AuthToken)instead.Sets the OAuth Core 1.0 Revision A header usingHttpHeaderNames.AUTHORIZATION. -
auth
Deprecated.Useauth(AuthToken)instead.Sets the OAuth 2.0 header usingHttpHeaderNames.AUTHORIZATION. -
auth
Sets theAuthTokenheader usingHttpHeaderNames.AUTHORIZATION. -
followRedirects
Enables automatic redirection. -
followRedirects
Sets theRedirectConfigto enable automatic redirection. -
contextCustomizer
@UnstableApi public AbstractClientOptionsBuilder contextCustomizer(Consumer<? super ClientRequestContext> contextCustomizer) Adds the specifiedClientRequestContextcustomizer function so that it customizes the context in the thread that initiated the client call. The given customizer function is evaluated before the customizer function specified byClients.withContextCustomizer(Consumer).static final ThreadLocal<String> USER_ID = new ThreadLocal<>(); static final AttributeKey<String> USER_ID_ATTR = AttributeKey.valueOf("USER_ID"); ... MyClientStub client = Clients.builder(...) .contextCustomizer(ctx -> { // This customizer will be invoked from the thread that initiates a client call. ctx.setAttr(USER_ID_ATTR, USER_ID.get()); }) .build(MyClientStub.class); // Good: // The context data is set by the thread that initiates the client call. USER_ID.set("user1"); try { client.executeSomething1(..); ... client.executeSomethingN(..); } finally { // Should clean up the thread local storage. USER_ID.remove(); } // Bad: USER_ID.set("user1"); executor.execute(() -> { // The variable in USER_ID won't be propagated to the context. // The variable is not valid at the moment client.executeSomething1() is called. client.executeSomething1(..); });Note that certain properties of
may beClientRequestContext, such as:nullwhile the customizer function runs, because the target host of theRequestis not determined yet.- See Also:
-
buildOptions
BuildsClientOptionswith the given options and the default options. -
buildOptions
BuildsClientOptionswith the specifiedbaseOptionsand the options which were set to this builder.
-
auth(AuthToken)instead.