Class OkHttpChannelBuilder
- java.lang.Object
-
- io.grpc.ManagedChannelBuilder<T>
-
- io.grpc.ForwardingChannelBuilder2<OkHttpChannelBuilder>
-
- io.grpc.okhttp.OkHttpChannelBuilder
-
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1785") public final class OkHttpChannelBuilder extends ForwardingChannelBuilder2<OkHttpChannelBuilder>
Convenience class for building channels with the OkHttp transport.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class io.grpc.ManagedChannelBuilder
ManagedChannelBuilder.InterceptorFactory
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_FLOW_CONTROL_WINDOW
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description OkHttpChannelBuilderconnectionSpec(ConnectionSpec connectionSpec)For secure connection, provides a ConnectionSpec to specify Cipher suite and TLS versions.protected ManagedChannelBuilder<?>delegate()Returns the delegatedManagedChannelBuilder.OkHttpChannelBuilderflowControlWindow(int flowControlWindow)Sets the flow control window in bytes.static OkHttpChannelBuilderforAddress(String host, int port)Creates a new builder for the given server host and port.static OkHttpChannelBuilderforAddress(String host, int port, ChannelCredentials creds)Creates a new builder with the given host and port.static OkHttpChannelBuilderforTarget(String target)Creates a new builder for the given target that will be resolved byNameResolver.static OkHttpChannelBuilderforTarget(String target, ChannelCredentials creds)Creates a new builder for the given target that will be resolved byNameResolver.OkHttpChannelBuilderhostnameVerifier(HostnameVerifier hostnameVerifier)Set the hostname verifier to use when using TLS negotiation.OkHttpChannelBuilderkeepAliveTime(long keepAliveTime, TimeUnit timeUnit)Sets the time without read activity before sending a keepalive ping.OkHttpChannelBuilderkeepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit)Sets the time waiting for read activity after sending a keepalive ping.OkHttpChannelBuilderkeepAliveWithoutCalls(boolean enable)Sets whether keepalive will be performed when there are no outstanding RPC on a connection.OkHttpChannelBuildermaxInboundMessageSize(int max)Sets the maximum message size allowed for a single gRPC frame.OkHttpChannelBuildermaxInboundMetadataSize(int bytes)Sets the maximum size of metadata allowed to be received.OkHttpChannelBuildernegotiationType(NegotiationType type)Deprecated.useusePlaintext()oruseTransportSecurity()instead.OkHttpChannelBuilderscheduledExecutorService(ScheduledExecutorService scheduledExecutorService)Provides a custom scheduled executor service.OkHttpChannelBuildersocketFactory(SocketFactory socketFactory)Override the defaultSocketFactoryused to create sockets.OkHttpChannelBuildersslSocketFactory(SSLSocketFactory factory)Override the defaultSSLSocketFactoryand enable TLS negotiation.OkHttpChannelBuildertlsConnectionSpec(String[] tlsVersions, String[] cipherSuites)Sets the connection specification used for secure connections.OkHttpChannelBuildertransportExecutor(Executor transportExecutor)Override the default executor necessary for internal transport use.OkHttpChannelBuilderusePlaintext()Sets the negotiation type for the HTTP/2 connection to plaintext.OkHttpChannelBuilderuseTransportSecurity()Sets the negotiation type for the HTTP/2 connection to TLS (this is the default).-
Methods inherited from class io.grpc.ForwardingChannelBuilder2
addMetricSink, addTransportFilter, build, compressorRegistry, decompressorRegistry, defaultLoadBalancingPolicy, defaultServiceConfig, directExecutor, disableRetry, disableServiceConfigLookUp, enableRetry, executor, idleTimeout, intercept, intercept, interceptWithTarget, maxHedgedAttempts, maxRetryAttempts, maxTraceEvents, nameResolverFactory, offloadExecutor, overrideAuthority, perRpcBufferLimit, proxyDetector, retryBufferSize, setBinaryLog, setNameResolverArg, toString, userAgent
-
-
-
-
Field Detail
-
DEFAULT_FLOW_CONTROL_WINDOW
public static final int DEFAULT_FLOW_CONTROL_WINDOW
- See Also:
- Constant Field Values
-
-
Method Detail
-
forAddress
public static OkHttpChannelBuilder forAddress(String host, int port)
Creates a new builder for the given server host and port.
-
forAddress
public static OkHttpChannelBuilder forAddress(String host, int port, ChannelCredentials creds)
Creates a new builder with the given host and port.
-
forTarget
public static OkHttpChannelBuilder forTarget(String target)
Creates a new builder for the given target that will be resolved byNameResolver.
-
forTarget
public static OkHttpChannelBuilder forTarget(String target, ChannelCredentials creds)
Creates a new builder for the given target that will be resolved byNameResolver.
-
delegate
@Internal protected ManagedChannelBuilder<?> delegate()
Description copied from class:ForwardingChannelBuilder2Returns the delegatedManagedChannelBuilder.- Specified by:
delegatein classForwardingChannelBuilder2<OkHttpChannelBuilder>
-
transportExecutor
public OkHttpChannelBuilder transportExecutor(@Nullable Executor transportExecutor)
Override the default executor necessary for internal transport use.The channel does not take ownership of the given executor. It is the caller' responsibility to shutdown the executor when appropriate.
-
socketFactory
public OkHttpChannelBuilder socketFactory(@Nullable SocketFactory socketFactory)
Override the defaultSocketFactoryused to create sockets. If the socket factory is not set or set to null, a default one will be used.- Since:
- 1.20.0
-
negotiationType
@Deprecated public OkHttpChannelBuilder negotiationType(NegotiationType type)
Deprecated.useusePlaintext()oruseTransportSecurity()instead.Sets the negotiation type for the HTTP/2 connection.If TLS is enabled a default
SSLSocketFactoryis created using the bestProvideravailable and is NOT based onSSLSocketFactory.getDefault(). To more precisely control the TLS configuration callsslSocketFactoryto override the socket factory used.Default:
TLS
-
keepAliveTime
public OkHttpChannelBuilder keepAliveTime(long keepAliveTime, TimeUnit timeUnit)
Sets the time without read activity before sending a keepalive ping. An unreasonably small value might be increased, andLong.MAX_VALUEnano seconds or an unreasonably large value will disable keepalive. Defaults to infinite.Clients must receive permission from the service owner before enabling this option. Keepalives can increase the load on services and are commonly "invisible" making it hard to notice when they are causing excessive load. Clients are strongly encouraged to use only as small of a value as necessary.
- Overrides:
keepAliveTimein classForwardingChannelBuilder2<OkHttpChannelBuilder>- Since:
- 1.3.0
- See Also:
- gRFC A8 Client-side Keepalive
-
keepAliveTimeout
public OkHttpChannelBuilder keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit)
Sets the time waiting for read activity after sending a keepalive ping. If the time expires without any read activity on the connection, the connection is considered dead. An unreasonably small value might be increased. Defaults to 20 seconds.This value should be at least multiple times the RTT to allow for lost packets.
- Overrides:
keepAliveTimeoutin classForwardingChannelBuilder2<OkHttpChannelBuilder>- Since:
- 1.3.0
- See Also:
- gRFC A8 Client-side Keepalive
-
flowControlWindow
public OkHttpChannelBuilder flowControlWindow(int flowControlWindow)
Sets the flow control window in bytes. If not called, the default value isDEFAULT_FLOW_CONTROL_WINDOW).
-
keepAliveWithoutCalls
public OkHttpChannelBuilder keepAliveWithoutCalls(boolean enable)
Sets whether keepalive will be performed when there are no outstanding RPC on a connection. Defaults tofalse.Clients must receive permission from the service owner before enabling this option. Keepalives on unused connections can easilly accidentally consume a considerable amount of bandwidth and CPU.
idleTimeout()should generally be used instead of this option.- Overrides:
keepAliveWithoutCallsin classForwardingChannelBuilder2<OkHttpChannelBuilder>- Since:
- 1.3.0
- See Also:
keepAliveTime(long, TimeUnit)
-
sslSocketFactory
public OkHttpChannelBuilder sslSocketFactory(SSLSocketFactory factory)
Override the defaultSSLSocketFactoryand enable TLS negotiation.
-
hostnameVerifier
public OkHttpChannelBuilder hostnameVerifier(@Nullable HostnameVerifier hostnameVerifier)
Set the hostname verifier to use when using TLS negotiation. The hostnameVerifier is only used if using TLS negotiation. If the hostname verifier is not set, a default hostname verifier is used.Be careful when setting a custom hostname verifier! By setting a non-null value, you are replacing all default verification behavior. If the hostname verifier you supply does not effectively supply the same checks, you may be removing the security assurances that TLS aims to provide.
This method should not be used to avoid hostname verification, even during testing, since
ForwardingChannelBuilder2.overrideAuthority(java.lang.String)is a safer alternative as it does not disable any security checks.- Returns:
- this
- Since:
- 1.6.0
- See Also:
OkHostnameVerifier
-
connectionSpec
public OkHttpChannelBuilder connectionSpec(ConnectionSpec connectionSpec)
For secure connection, provides a ConnectionSpec to specify Cipher suite and TLS versions.By default a modern, HTTP/2-compatible spec will be used.
This method is only used when building a secure connection. For plaintext connection, use
usePlaintext()instead.- Throws:
IllegalArgumentException- IfconnectionSpecis not with TLS
-
tlsConnectionSpec
public OkHttpChannelBuilder tlsConnectionSpec(String[] tlsVersions, String[] cipherSuites)
Sets the connection specification used for secure connections.By default a modern, HTTP/2-compatible spec will be used.
This method is only used when building a secure connection. For plaintext connection, use
usePlaintext()instead.- Parameters:
tlsVersions- List of tls versions.cipherSuites- List of cipher suites.- Since:
- 1.43.0
-
usePlaintext
public OkHttpChannelBuilder usePlaintext()
Sets the negotiation type for the HTTP/2 connection to plaintext.- Overrides:
usePlaintextin classForwardingChannelBuilder2<OkHttpChannelBuilder>- Returns:
- this
-
useTransportSecurity
public OkHttpChannelBuilder useTransportSecurity()
Sets the negotiation type for the HTTP/2 connection to TLS (this is the default).With TLS enabled, a default
SSLSocketFactoryis created using the bestProvideravailable and is NOT based onSSLSocketFactory.getDefault(). To more precisely control the TLS configuration callsslSocketFactory(SSLSocketFactory)to override the socket factory used.- Overrides:
useTransportSecurityin classForwardingChannelBuilder2<OkHttpChannelBuilder>- Returns:
- this
-
scheduledExecutorService
public OkHttpChannelBuilder scheduledExecutorService(ScheduledExecutorService scheduledExecutorService)
Provides a custom scheduled executor service.It's an optional parameter. If the user has not provided a scheduled executor service when the channel is built, the builder will use a static cached thread pool.
- Returns:
- this
- Since:
- 1.11.0
-
maxInboundMetadataSize
public OkHttpChannelBuilder maxInboundMetadataSize(int bytes)
Sets the maximum size of metadata allowed to be received.Integer.MAX_VALUEdisables the enforcement. Defaults to no limit (Integer.MAX_VALUE).The implementation does not currently limit memory usage; this value is checked only after the metadata is decoded from the wire. It does prevent large metadata from being passed to the application.
- Overrides:
maxInboundMetadataSizein classForwardingChannelBuilder2<OkHttpChannelBuilder>- Parameters:
bytes- the maximum size of received metadata- Returns:
- this
- Throws:
IllegalArgumentException- if bytes is non-positive- Since:
- 1.17.0
-
maxInboundMessageSize
public OkHttpChannelBuilder maxInboundMessageSize(int max)
Sets the maximum message size allowed for a single gRPC frame. If an inbound messages larger than this limit is received it will not be processed and the RPC will fail with RESOURCE_EXHAUSTED.- Overrides:
maxInboundMessageSizein classForwardingChannelBuilder2<OkHttpChannelBuilder>- Parameters:
max- the maximum number of bytes a single message can be.- Returns:
- this
-
-