Class OkHttpChannelBuilder


  • @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1785")
    public final class OkHttpChannelBuilder
    extends io.grpc.internal.AbstractManagedChannelImplBuilder<OkHttpChannelBuilder>
    Convenience class for building channels with the OkHttp transport.
    • 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​(java.lang.String host,
                                                      int port)
        Creates a new builder for the given server host and port.
      • forAddress

        public static OkHttpChannelBuilder forAddress​(java.lang.String host,
                                                      int port,
                                                      io.grpc.ChannelCredentials creds)
        Creates a new builder with the given host and port.
      • forTarget

        public static OkHttpChannelBuilder forTarget​(java.lang.String target)
        Creates a new builder for the given target that will be resolved by NameResolver.
      • forTarget

        public static OkHttpChannelBuilder forTarget​(java.lang.String target,
                                                     io.grpc.ChannelCredentials creds)
        Creates a new builder for the given target that will be resolved by NameResolver.
      • delegate

        @Internal
        protected io.grpc.ManagedChannelBuilder<?> delegate()
        Specified by:
        delegate in class io.grpc.internal.AbstractManagedChannelImplBuilder<OkHttpChannelBuilder>
      • transportExecutor

        public OkHttpChannelBuilder transportExecutor​(@Nullable
                                                      java.util.concurrent.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
                                                  javax.net.SocketFactory socketFactory)
        Override the default SocketFactory used 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.
        Sets the negotiation type for the HTTP/2 connection.

        If TLS is enabled a default SSLSocketFactory is created using the best Provider available and is NOT based on SSLSocketFactory.getDefault(). To more precisely control the TLS configuration call sslSocketFactory to override the socket factory used.

        Default: TLS

      • keepAliveTime

        public OkHttpChannelBuilder keepAliveTime​(long keepAliveTime,
                                                  java.util.concurrent.TimeUnit timeUnit)
        Overrides:
        keepAliveTime in class io.grpc.internal.AbstractManagedChannelImplBuilder<OkHttpChannelBuilder>
        Since:
        1.3.0
      • keepAliveTimeout

        public OkHttpChannelBuilder keepAliveTimeout​(long keepAliveTimeout,
                                                     java.util.concurrent.TimeUnit timeUnit)
        Overrides:
        keepAliveTimeout in class io.grpc.internal.AbstractManagedChannelImplBuilder<OkHttpChannelBuilder>
        Since:
        1.3.0
      • sslSocketFactory

        public OkHttpChannelBuilder sslSocketFactory​(javax.net.ssl.SSLSocketFactory factory)
        Override the default SSLSocketFactory and enable TLS negotiation.
      • hostnameVerifier

        public OkHttpChannelBuilder hostnameVerifier​(@Nullable
                                                     javax.net.ssl.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 AbstractManagedChannelImplBuilder.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​(com.squareup.okhttp.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:
        java.lang.IllegalArgumentException - If connectionSpec is not with TLS
      • tlsConnectionSpec

        public OkHttpChannelBuilder tlsConnectionSpec​(java.lang.String[] tlsVersions,
                                                      java.lang.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:
        usePlaintext in class io.grpc.internal.AbstractManagedChannelImplBuilder<OkHttpChannelBuilder>
      • useTransportSecurity

        public OkHttpChannelBuilder useTransportSecurity()
        Sets the negotiation type for the HTTP/2 connection to TLS (this is the default).

        With TLS enabled, a default SSLSocketFactory is created using the best Provider available and is NOT based on SSLSocketFactory.getDefault(). To more precisely control the TLS configuration call sslSocketFactory(SSLSocketFactory) to override the socket factory used.

        Overrides:
        useTransportSecurity in class io.grpc.internal.AbstractManagedChannelImplBuilder<OkHttpChannelBuilder>
      • scheduledExecutorService

        public OkHttpChannelBuilder scheduledExecutorService​(java.util.concurrent.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_VALUE disables 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:
        maxInboundMetadataSize in class io.grpc.internal.AbstractManagedChannelImplBuilder<OkHttpChannelBuilder>
        Parameters:
        bytes - the maximum size of received metadata
        Returns:
        this
        Throws:
        java.lang.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:
        maxInboundMessageSize in class io.grpc.internal.AbstractManagedChannelImplBuilder<OkHttpChannelBuilder>