Class ClientFactoryBuilder
- All Implemented Interfaces:
TlsSetters
ClientFactory
.
Example
final ClientFactory factory =
ClientFactory.builder()
// Set the connection timeout to 5 seconds.
.connectTimeoutMillis(5000)
// Set the socket send buffer to 1 MiB.
.socketOption(ChannelOption.SO_SNDBUF, 1048576)
// Disable certificate verification; never do this in production!
.tlsNoVerify()
.build();
-
Method Summary
Modifier and TypeMethodDescriptionaddressResolverGroupFactory
(Function<? super EventLoopGroup, ? extends AddressResolverGroup<? extends InetSocketAddress>> addressResolverGroupFactory) Sets the factory that creates aAddressResolverGroup
which resolves remote addresses intoInetSocketAddress
es.build()
Returns a newly-createdClientFactory
based on the properties of this builder.channelOption
(ChannelOption<T> option, T value) Sets the options of sockets created by theClientFactory
.connectionPoolListener
(ConnectionPoolListener connectionPoolListener) Sets the listener which is notified on a connection pool event.connectTimeout
(Duration connectTimeout) Sets the timeout of a socket connection attempt.connectTimeoutMillis
(long connectTimeoutMillis) Sets the timeout of a socket connection attempt in milliseconds.domainNameResolverCustomizer
(Consumer<? super DnsResolverGroupBuilder> dnsResolverGroupCustomizer) Adds the specifiedConsumer
which customizes the givenDnsNameResolverBuilder
.eventLoopSchedulerFactory
(Function<? super EventLoopGroup, ? extends EventLoopScheduler> eventLoopSchedulerFactory) Sets the factory that creates anEventLoopScheduler
which is responsible for assigning anEventLoop
to handle a connection to the specifiedEndpoint
.http1HeaderNaming
(Http1HeaderNaming http1HeaderNaming) Sets theHttp1HeaderNaming
which converts a lower-cased HTTP/2 header name into another HTTP/1 header name.http1MaxChunkSize
(int http1MaxChunkSize) Sets the maximum length of each chunk in an HTTP/1 response content.http1MaxHeaderSize
(int http1MaxHeaderSize) Sets the maximum length of all headers in an HTTP/1 response.http1MaxInitialLineLength
(int http1MaxInitialLineLength) Sets the maximum length of an HTTP/1 response initial line.http2InitialConnectionWindowSize
(int http2InitialConnectionWindowSize) http2InitialStreamWindowSize
(int http2InitialStreamWindowSize) Sets the SETTINGS_INITIAL_WINDOW_SIZE for HTTP/2 stream-level flow control.http2MaxFrameSize
(int http2MaxFrameSize) Sets the SETTINGS_MAX_FRAME_SIZE that indicates the size of the largest frame payload that this client is willing to receive.http2MaxHeaderListSize
(long http2MaxHeaderListSize) Sets the SETTINGS_MAX_HEADER_LIST_SIZE that indicates the maximum size of header list that the client is prepared to accept, in octets.idleTimeout
(Duration idleTimeout) Sets the idle timeout of a socket connection.idleTimeoutMillis
(long idleTimeoutMillis) Sets the idle timeout of a socket connection in milliseconds.maxConnectionAge
(Duration maxConnectionAge) Sets the maximum allowed age of a connection for keep-alive.maxConnectionAgeMillis
(long maxConnectionAgeMillis) Sets the maximum allowed age of a connection in millis for keep-alive.maxNumEventLoopsFunction
(ToIntFunction<Endpoint> maxNumEventLoopsFunction) Sets theToIntFunction
which takes anEndpoint
and produces the maximum number ofEventLoop
s which will be used to handle connections to the specifiedEndpoint
.maxNumEventLoopsPerEndpoint
(int maxNumEventLoopsPerEndpoint) Sets the maximum number ofEventLoop
s which will be used to handle HTTP/2 connections except the ones specified bymaxNumEventLoopsFunction(ToIntFunction)
.maxNumEventLoopsPerHttp1Endpoint
(int maxNumEventLoopsPerEndpoint) Sets the maximum number ofEventLoop
s which will be used to handle HTTP/1.1 connections except the ones specified bymaxNumEventLoopsFunction(ToIntFunction)
.maxNumRequestsPerConnection
(int maxNumRequestsPerConnection) Sets the maximum allowed number of requests that can be sent through one connection.meterRegistry
(MeterRegistry meterRegistry) Sets theMeterRegistry
which collects various stats.option
(ClientFactoryOption<T> option, T value) Adds the specifiedClientFactoryOption
and itsvalue
.option
(ClientFactoryOptionValue<T> optionValue) Adds the specifiedClientFactoryOptionValue
.options
(ClientFactoryOptions options) Adds the specifiedClientFactoryOptions
.pingInterval
(Duration pingInterval) Sets the PING interval.pingIntervalMillis
(long pingIntervalMillis) Sets the PING interval in milliseconds.proxyConfig
(ProxyConfig proxyConfig) Sets theProxyConfig
which contains proxy related configuration.proxyConfig
(ProxyConfigSelector proxyConfigSelector) Sets theProxyConfigSelector
which determines theProxyConfig
to be used.proxyConfig
(ProxySelector proxySelector) Sets theProxySelector
which determines theProxyConfig
to be used.Configures SSL or TLS for client certificate authentication with the specifiedkeyCertChainFile
and cleartextkeyFile
.Configures SSL or TLS for client certificate authentication with the specifiedkeyCertChainFile
,keyFile
andkeyPassword
.tls
(InputStream keyCertChainInputStream, InputStream keyInputStream) Configures SSL or TLS for client certificate authentication with the specifiedkeyCertChainInputStream
and cleartextkeyInputStream
.tls
(InputStream keyCertChainInputStream, InputStream keyInputStream, @Nullable String keyPassword) Configures SSL or TLS for client certificate authentication with the specifiedkeyCertChainInputStream
andkeyInputStream
andkeyPassword
.tls
(PrivateKey key, @Nullable String keyPassword, Iterable<? extends X509Certificate> keyCertChain) Configures SSL or TLS for client certificate authentication with the specifiedPrivateKey
,keyPassword
andX509Certificate
chain.tls
(PrivateKey key, @Nullable String keyPassword, X509Certificate... keyCertChain) Configures SSL or TLS for client certificate authentication with the specifiedPrivateKey
,keyPassword
andX509Certificate
chain.tls
(PrivateKey key, Iterable<? extends X509Certificate> keyCertChain) Configures SSL or TLS for client certificate authentication with the specified cleartextPrivateKey
andX509Certificate
chain.tls
(PrivateKey key, X509Certificate... keyCertChain) Configures SSL or TLS for client certificate authentication with the specified cleartextPrivateKey
andX509Certificate
chain.tls
(KeyManagerFactory keyManagerFactory) Configures SSL or TLS for client certificate authentication with the specifiedKeyManagerFactory
.Deprecated.It's not recommended to enable this option.tlsAllowUnsafeCiphers
(boolean tlsAllowUnsafeCiphers) Deprecated.It's not recommended to enable this option.tlsCustomizer
(Consumer<? super SslContextBuilder> tlsCustomizer) Adds theConsumer
which can arbitrarily configure theSslContextBuilder
that will be applied to the SSL session.Disables the verification of server's TLS certificate chain.tlsNoVerifyHosts
(String... insecureHosts) Disables the verification of server's TLS certificate chain for specific hosts.toString()
useHttp1Pipelining
(boolean useHttp1Pipelining) Sets whether to use HTTP pipelining for HTTP/1 connections.useHttp2Preface
(boolean useHttp2Preface) Sets whether to send an HTTP/2 preface string instead of an HTTP/1 upgrade request to negotiate the protocol version of a cleartext HTTP connection.useHttp2WithoutAlpn
(boolean useHttp2WithoutAlpn) Sets whether to use HTTP/2 without ALPN.workerGroup
(int numThreads) Uses a newly createdEventLoopGroup
with the specified number of threads for performing socket I/O and runningClient.execute(ClientRequestContext, Request)
.workerGroup
(EventLoopGroup workerGroup, boolean shutdownOnClose) Sets the workerEventLoopGroup
which is responsible for performing socket I/O and runningClient.execute(ClientRequestContext, Request)
.
-
Method Details
-
workerGroup
Sets the workerEventLoopGroup
which is responsible for performing socket I/O and runningClient.execute(ClientRequestContext, Request)
. If not set, the common worker group is used.- Parameters:
shutdownOnClose
- whether to shut down the workerEventLoopGroup
when theClientFactory
is closed
-
workerGroup
Uses a newly createdEventLoopGroup
with the specified number of threads for performing socket I/O and runningClient.execute(ClientRequestContext, Request)
. The workerEventLoopGroup
will be shut down when theClientFactory
is closed.- Parameters:
numThreads
- the number of event loop threads
-
eventLoopSchedulerFactory
public ClientFactoryBuilder eventLoopSchedulerFactory(Function<? super EventLoopGroup, ? extends EventLoopScheduler> eventLoopSchedulerFactory) Sets the factory that creates anEventLoopScheduler
which is responsible for assigning anEventLoop
to handle a connection to the specifiedEndpoint
. -
maxNumEventLoopsPerHttp1Endpoint
Sets the maximum number ofEventLoop
s which will be used to handle HTTP/1.1 connections except the ones specified bymaxNumEventLoopsFunction(ToIntFunction)
. 1 is used by default. -
maxNumEventLoopsPerEndpoint
Sets the maximum number ofEventLoop
s which will be used to handle HTTP/2 connections except the ones specified bymaxNumEventLoopsFunction(ToIntFunction)
. 1 is used by default. -
maxNumEventLoopsFunction
public ClientFactoryBuilder maxNumEventLoopsFunction(ToIntFunction<Endpoint> maxNumEventLoopsFunction) Sets theToIntFunction
which takes anEndpoint
and produces the maximum number ofEventLoop
s which will be used to handle connections to the specifiedEndpoint
. The function should return0
or a negative value for theEndpoint
s which it doesn't want to handle. For example:ToIntFunction<Endpoint> function = endpoint -> { if (endpoint.equals(Endpoint.of("foo.com"))) { return 5; } if (endpoint.host().contains("bar.com")) { return Integer.MAX_VALUE; // The value will be clamped at the number of event loops. } return -1; // Should return 0 or a negative value to use the default value. }
-
connectTimeout
Sets the timeout of a socket connection attempt. -
connectTimeoutMillis
Sets the timeout of a socket connection attempt in milliseconds. -
channelOption
Sets the options of sockets created by theClientFactory
. -
tlsNoVerify
Disables the verification of server's TLS certificate chain. If you want to disable verification for only specific hosts, usetlsNoVerifyHosts(String...)
. Note: You should never use this in production but only for a testing purpose. -
tlsNoVerifyHosts
Disables the verification of server's TLS certificate chain for specific hosts. If you want to disable all verification, usetlsNoVerify()
. Note: You should never use this in production but only for a testing purpose.- See Also:
-
tls
Configures SSL or TLS for client certificate authentication with the specifiedkeyCertChainFile
and cleartextkeyFile
.- Specified by:
tls
in interfaceTlsSetters
-
tls
public ClientFactoryBuilder tls(File keyCertChainFile, File keyFile, @Nullable @Nullable String keyPassword) Configures SSL or TLS for client certificate authentication with the specifiedkeyCertChainFile
,keyFile
andkeyPassword
.- Specified by:
tls
in interfaceTlsSetters
-
tls
Configures SSL or TLS for client certificate authentication with the specifiedkeyCertChainInputStream
and cleartextkeyInputStream
.- Specified by:
tls
in interfaceTlsSetters
-
tls
public ClientFactoryBuilder tls(InputStream keyCertChainInputStream, InputStream keyInputStream, @Nullable @Nullable String keyPassword) Configures SSL or TLS for client certificate authentication with the specifiedkeyCertChainInputStream
andkeyInputStream
andkeyPassword
.- Specified by:
tls
in interfaceTlsSetters
-
tls
Configures SSL or TLS for client certificate authentication with the specified cleartextPrivateKey
andX509Certificate
chain.- Specified by:
tls
in interfaceTlsSetters
-
tls
Configures SSL or TLS for client certificate authentication with the specified cleartextPrivateKey
andX509Certificate
chain.- Specified by:
tls
in interfaceTlsSetters
-
tls
public ClientFactoryBuilder tls(PrivateKey key, @Nullable @Nullable String keyPassword, X509Certificate... keyCertChain) Configures SSL or TLS for client certificate authentication with the specifiedPrivateKey
,keyPassword
andX509Certificate
chain.- Specified by:
tls
in interfaceTlsSetters
-
tls
public ClientFactoryBuilder tls(PrivateKey key, @Nullable @Nullable String keyPassword, Iterable<? extends X509Certificate> keyCertChain) Configures SSL or TLS for client certificate authentication with the specifiedPrivateKey
,keyPassword
andX509Certificate
chain.- Specified by:
tls
in interfaceTlsSetters
-
tls
Configures SSL or TLS for client certificate authentication with the specifiedKeyManagerFactory
.- Specified by:
tls
in interfaceTlsSetters
-
tlsCustomizer
Adds theConsumer
which can arbitrarily configure theSslContextBuilder
that will be applied to the SSL session. For example, useSslContextBuilder.trustManager(TrustManagerFactory)
to configure a custom server CA orSslContextBuilder.keyManager(KeyManagerFactory)
to configure a client certificate for SSL authorization.- Specified by:
tlsCustomizer
in interfaceTlsSetters
-
tlsAllowUnsafeCiphers
Deprecated.It's not recommended to enable this option. Use it only when you have no other way to communicate with an insecure peer than this.Allows the bad cipher suites listed in RFC7540 for TLS handshake.Note that enabling this option increases the security risk of your connection. Use it only when you must communicate with a legacy system that does not support secure cipher suites. See Section 9.2.2, RFC7540 for more information. This option is disabled by default.
-
tlsAllowUnsafeCiphers
Deprecated.It's not recommended to enable this option. Use it only when you have no other way to communicate with an insecure peer than this.Allows the bad cipher suites listed in RFC7540 for TLS handshake.Note that enabling this option increases the security risk of your connection. Use it only when you must communicate with a legacy system that does not support secure cipher suites. See Section 9.2.2, RFC7540 for more information. This option is disabled by default.
- Parameters:
tlsAllowUnsafeCiphers
- Whether to allow the unsafe ciphers
-
addressResolverGroupFactory
public ClientFactoryBuilder addressResolverGroupFactory(Function<? super EventLoopGroup, ? extends AddressResolverGroup<? extends InetSocketAddress>> addressResolverGroupFactory) Sets the factory that creates aAddressResolverGroup
which resolves remote addresses intoInetSocketAddress
es.- Throws:
IllegalStateException
- ifdomainNameResolverCustomizer(Consumer)
was called already.
-
domainNameResolverCustomizer
public ClientFactoryBuilder domainNameResolverCustomizer(Consumer<? super DnsResolverGroupBuilder> dnsResolverGroupCustomizer) Adds the specifiedConsumer
which customizes the givenDnsNameResolverBuilder
. This method is useful when you want to change the behavior of the default domain name resolver, such as changing the DNS server list.- Throws:
IllegalStateException
- ifaddressResolverGroupFactory(Function)
was called already.
-
http2InitialConnectionWindowSize
Sets the initial connection flow-control window size. The HTTP/2 connection is first established with 65535 bytes of connection flow-control window size, and it is changed if and only ifhttp2InitialConnectionWindowSize
is set. Note that this setting affects the connection-level window size, not the window size of streams.- See Also:
-
http2InitialStreamWindowSize
Sets the SETTINGS_INITIAL_WINDOW_SIZE for HTTP/2 stream-level flow control. Note that this setting affects the window size of all streams, not the connection-level window size.- See Also:
-
http2MaxFrameSize
Sets the SETTINGS_MAX_FRAME_SIZE that indicates the size of the largest frame payload that this client is willing to receive. -
http2MaxHeaderListSize
Sets the SETTINGS_MAX_HEADER_LIST_SIZE that indicates the maximum size of header list that the client is prepared to accept, in octets. -
http1MaxInitialLineLength
Sets the maximum length of an HTTP/1 response initial line. -
http1MaxHeaderSize
Sets the maximum length of all headers in an HTTP/1 response. -
http1MaxChunkSize
Sets the maximum length of each chunk in an HTTP/1 response content. The content or a chunk longer than this value will be split into smaller chunks so that their lengths never exceed it. -
idleTimeout
Sets the idle timeout of a socket connection. The connection is closed if there is no request in progress for this amount of time. -
idleTimeoutMillis
Sets the idle timeout of a socket connection in milliseconds. The connection is closed if there is no request in progress for this amount of time. -
pingIntervalMillis
Sets the PING interval in milliseconds. When neither read nor write was performed for the givenpingIntervalMillis
, a PING frame is sent for HTTP/2 or an OPTIONS request with an asterisk ("*") is sent for HTTP/1.Note that this settings is only in effect when
idleTimeoutMillis(long)
} oridleTimeout(Duration)
is greater than the specified PING interval.The minimum allowed PING interval is 1000L milliseconds.
0
means the client will not send a PING.- Throws:
IllegalArgumentException
- if the specifiedpingIntervalMillis
is smaller than 1000L milliseconds.
-
pingInterval
Sets the PING interval. When neither read nor write was performed for the givenpingInterval
, a PING frame is sent for HTTP/2 or an OPTIONS request with an asterisk ("*") is sent for HTTP/1.Note that this settings is only in effect when
idleTimeoutMillis(long)
} oridleTimeout(Duration)
is greater than the specified PING interval.The minimum allowed PING interval is 1000L milliseconds.
0
means the client will not send a PING.- Throws:
IllegalArgumentException
- if the specifiedpingInterval
is smaller than 1000L milliseconds.
-
maxConnectionAgeMillis
Sets the maximum allowed age of a connection in millis for keep-alive. A connection is disconnected after the specifiedmaxConnectionAgeMillis
since the connection was established. This option is disabled by default, which means unlimited.- Parameters:
maxConnectionAgeMillis
- the maximum connection age in millis.0
disables the limit.- Throws:
IllegalArgumentException
- if the specifiedmaxConnectionAgeMillis
is smaller than 1000L milliseconds.
-
maxConnectionAge
Sets the maximum allowed age of a connection for keep-alive. A connection is disconnected after the specifiedmaxConnectionAge
since the connection was established. This option is disabled by default, which means unlimited.- Parameters:
maxConnectionAge
- the maximum connection age.0
disables the limit.- Throws:
IllegalArgumentException
- if the specifiedmaxConnectionAge
is smaller than 1000L milliseconds.
-
maxNumRequestsPerConnection
Sets the maximum allowed number of requests that can be sent through one connection. This option is disabled by default, which means unlimited.- Parameters:
maxNumRequestsPerConnection
- the maximum number of requests per connection.0
disables the limit.
-
useHttp2Preface
Sets whether to send an HTTP/2 preface string instead of an HTTP/1 upgrade request to negotiate the protocol version of a cleartext HTTP connection. -
useHttp2WithoutAlpn
Sets whether to use HTTP/2 without ALPN. This is useful if you want to communicate with an HTTP/2 server over TLS but the server does not support ALPN. -
useHttp1Pipelining
Sets whether to use HTTP pipelining for HTTP/1 connections. This does not affect HTTP/2 connections. This option is disabled by default. -
connectionPoolListener
Sets the listener which is notified on a connection pool event. -
meterRegistry
Sets theMeterRegistry
which collects various stats. -
proxyConfig
Sets theProxyConfig
which contains proxy related configuration. -
proxyConfig
Sets theProxySelector
which determines theProxyConfig
to be used.This method makes a best effort to provide compatibility with
ProxySelector
, but it has some limitations. SeeProxyConfigSelector.of(ProxySelector)
for more information. -
proxyConfig
Sets theProxyConfigSelector
which determines theProxyConfig
to be used. -
http1HeaderNaming
Sets theHttp1HeaderNaming
which converts a lower-cased HTTP/2 header name into another HTTP/1 header name. This is useful when communicating with a legacy system that only supports case-sensitive HTTP/1 headers. -
option
Adds the specifiedClientFactoryOption
and itsvalue
. -
option
Adds the specifiedClientFactoryOptionValue
. -
options
Adds the specifiedClientFactoryOptions
. -
build
Returns a newly-createdClientFactory
based on the properties of this builder. -
toString
-