Class ServerBuilder<T extends ServerBuilder<T>>
- java.lang.Object
-
- io.grpc.ServerBuilder<T>
-
- Type Parameters:
T- The concrete type of this builder.
public abstract class ServerBuilder<T extends ServerBuilder<T>> extends java.lang.ObjectA builder forServerinstances.- Since:
- 1.0.0
-
-
Constructor Summary
Constructors Constructor Description ServerBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract TaddService(BindableService bindableService)Adds a service implementation to the handler registry.abstract TaddService(ServerServiceDefinition service)Adds a service implementation to the handler registry.TaddStreamTracerFactory(ServerStreamTracer.Factory factory)Adds aServerStreamTracer.Factoryto measure server-side traffic.TaddTransportFilter(ServerTransportFilter filter)Adds aServerTransportFilter.abstract Serverbuild()Builds a server using the given parameters.abstract TcompressorRegistry(CompressorRegistry registry)Set the compression registry for use in the channel.abstract TdecompressorRegistry(DecompressorRegistry registry)Set the decompression registry for use in the channel.abstract TdirectExecutor()Execute application code directly in the transport thread.abstract Texecutor(java.util.concurrent.Executor executor)Provides a custom executor.abstract TfallbackHandlerRegistry(HandlerRegistry fallbackRegistry)Sets a fallback handler registry that will be looked up in if a method is not found in the primary registry.static ServerBuilder<?>forPort(int port)Static factory for creating a new ServerBuilder.ThandshakeTimeout(long timeout, java.util.concurrent.TimeUnit unit)Sets the permitted time for new connections to complete negotiation handshakes before being killed.Tintercept(ServerInterceptor interceptor)Adds aServerInterceptorthat is run for all services on the server.TmaxInboundMessageSize(int bytes)Sets the maximum message size allowed to be received on the server.TmaxInboundMetadataSize(int bytes)Sets the maximum size of metadata allowed to be received.TsetBinaryLog(BinaryLog binaryLog)Sets the BinaryLog object that this server should log to.abstract TuseTransportSecurity(java.io.File certChain, java.io.File privateKey)Makes the server use TLS.TuseTransportSecurity(java.io.InputStream certChain, java.io.InputStream privateKey)Makes the server use TLS.
-
-
-
Method Detail
-
forPort
public static ServerBuilder<?> forPort(int port)
Static factory for creating a new ServerBuilder.- Parameters:
port- the port to listen on- Since:
- 1.0.0
-
directExecutor
public abstract T directExecutor()
Execute application code directly in the transport thread.Depending on the underlying transport, using a direct executor may lead to substantial performance improvements. However, it also requires the application to not block under any circumstances.
Calling this method is semantically equivalent to calling
executor(Executor)and passing in a direct executor. However, this is the preferred way as it may allow the transport to perform special optimizations.- Returns:
- this
- Since:
- 1.0.0
-
executor
public abstract T executor(@Nullable java.util.concurrent.Executor executor)
Provides a custom executor.It's an optional parameter. If the user has not provided an executor when the server is built, the builder will use a static cached thread pool.
The server won't take ownership of the given executor. It's caller's responsibility to shut down the executor when it's desired.
- Returns:
- this
- Since:
- 1.0.0
-
addService
public abstract T addService(ServerServiceDefinition service)
Adds a service implementation to the handler registry.- Parameters:
service- ServerServiceDefinition object- Returns:
- this
- Since:
- 1.0.0
-
addService
public abstract T addService(BindableService bindableService)
Adds a service implementation to the handler registry. If bindableService implementsInternalNotifyOnServerBuild, the service will receive a reference to the generated server instance upon build().- Parameters:
bindableService- BindableService object- Returns:
- this
- Since:
- 1.0.0
-
intercept
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/3117") public T intercept(ServerInterceptor interceptor)
Adds aServerInterceptorthat is run for all services on the server. Interceptors added through this method always run before per-service interceptors added throughServerInterceptors. Interceptors run in the reverse order in which they are added, just as with consecutive calls toServerInterceptors.intercept().- Parameters:
interceptor- the all-service interceptor- Returns:
- this
- Since:
- 1.5.0
-
addTransportFilter
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2132") public T addTransportFilter(ServerTransportFilter filter)
Adds aServerTransportFilter. The order of filters being added is the order they will be executed.- Returns:
- this
- Since:
- 1.2.0
-
addStreamTracerFactory
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2861") public T addStreamTracerFactory(ServerStreamTracer.Factory factory)
Adds aServerStreamTracer.Factoryto measure server-side traffic. The order of factories being added is the order they will be executed. Tracers should not- Returns:
- this
- Since:
- 1.3.0
-
fallbackHandlerRegistry
public abstract T fallbackHandlerRegistry(@Nullable HandlerRegistry fallbackRegistry)
Sets a fallback handler registry that will be looked up in if a method is not found in the primary registry. The primary registry (configured viaaddService()) is faster but immutable. The fallback registry is more flexible and allows implementations to mutate over time and load services on-demand.- Returns:
- this
- Since:
- 1.0.0
-
useTransportSecurity
public abstract T useTransportSecurity(java.io.File certChain, java.io.File privateKey)
Makes the server use TLS.- Parameters:
certChain- file containing the full certificate chainprivateKey- file containing the private key- Returns:
- this
- Throws:
java.lang.UnsupportedOperationException- if the server does not support TLS.- Since:
- 1.0.0
-
useTransportSecurity
public T useTransportSecurity(java.io.InputStream certChain, java.io.InputStream privateKey)
Makes the server use TLS.- Parameters:
certChain- InputStream containing the full certificate chainprivateKey- InputStream containing the private key- Returns:
- this
- Throws:
java.lang.UnsupportedOperationException- if the server does not support TLS, or does not support reading these files from an InputStream.- Since:
- 1.12.0
-
decompressorRegistry
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704") public abstract T decompressorRegistry(@Nullable DecompressorRegistry registry)
Set the decompression registry for use in the channel. This is an advanced API call and shouldn't be used unless you are using custom message encoding. The default supported decompressors are inDecompressorRegistry.getDefaultInstance.- Returns:
- this
- Since:
- 1.0.0
-
compressorRegistry
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704") public abstract T compressorRegistry(@Nullable CompressorRegistry registry)
Set the compression registry for use in the channel. This is an advanced API call and shouldn't be used unless you are using custom message encoding. The default supported compressors are inCompressorRegistry.getDefaultInstance.- Returns:
- this
- Since:
- 1.0.0
-
handshakeTimeout
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/3706") public T handshakeTimeout(long timeout, java.util.concurrent.TimeUnit unit)
Sets the permitted time for new connections to complete negotiation handshakes before being killed.- Returns:
- this
- Throws:
java.lang.IllegalArgumentException- if timeout is negativejava.lang.UnsupportedOperationException- if unsupported- Since:
- 1.8.0
-
maxInboundMessageSize
public T maxInboundMessageSize(int bytes)
Sets the maximum message size allowed to be received on the server. If not called, defaults to 4 MiB. The default provides protection to servers who haven't considered the possibility of receiving large messages while trying to be large enough to not be hit in normal usage.This method is advisory, and implementations may decide to not enforce this. Currently, the only known transport to not enforce this is
InProcessServer.- Parameters:
bytes- the maximum number of bytes a single message can be.- Returns:
- this
- Throws:
java.lang.IllegalArgumentException- if bytes is negative.java.lang.UnsupportedOperationException- if unsupported.- Since:
- 1.13.0
-
maxInboundMetadataSize
public T maxInboundMetadataSize(int bytes)
Sets the maximum size of metadata allowed to be received.Integer.MAX_VALUEdisables the enforcement. The default is implementation-dependent, but is not generally less than 8 KiB and may be unlimited.This is cumulative size of the metadata. The precise calculation is implementation-dependent, but implementations are encouraged to follow the calculation used for HTTP/2's SETTINGS_MAX_HEADER_LIST_SIZE. It sums the bytes from each entry's key and value, plus 32 bytes of overhead per entry.
- Parameters:
bytes- the maximum size of received metadata- Returns:
- this
- Throws:
java.lang.IllegalArgumentException- if bytes is non-positive- Since:
- 1.17.0
-
setBinaryLog
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4017") public T setBinaryLog(BinaryLog binaryLog)
Sets the BinaryLog object that this server should log to. The server does not take ownership of the object, and users are responsible for callingCloseable.close().- Parameters:
binaryLog- the object to provide logging.- Returns:
- this
- Since:
- 1.13.0
-
build
public abstract Server build()
Builds a server using the given parameters.The returned service will not been started or be bound a port. You will need to start it with
Server.start().- Returns:
- a new Server
- Since:
- 1.0.0
-
-