T
- The concrete type of this builder.public abstract class ServerBuilder<T extends ServerBuilder<T>> extends Object
Server
instances.Constructor and Description |
---|
ServerBuilder() |
Modifier and Type | Method and Description |
---|---|
abstract T |
addService(BindableService bindableService)
Adds a service implementation to the handler registry.
|
abstract T |
addService(ServerServiceDefinition service)
Adds a service implementation to the handler registry.
|
T |
addServices(List<ServerServiceDefinition> services)
Adds a list of service implementations to the handler registry together.
|
T |
addStreamTracerFactory(ServerStreamTracer.Factory factory)
Adds a
ServerStreamTracer.Factory to measure server-side traffic. |
T |
addTransportFilter(ServerTransportFilter filter)
Adds a
ServerTransportFilter . |
abstract Server |
build()
Builds a server using the given parameters.
|
T |
callExecutor(ServerCallExecutorSupplier executorSupplier)
Allows for defining a way to provide a custom executor to handle the server call.
|
abstract T |
compressorRegistry(CompressorRegistry registry)
Set the compression registry for use in the channel.
|
abstract T |
decompressorRegistry(DecompressorRegistry registry)
Set the decompression registry for use in the channel.
|
abstract T |
directExecutor()
Execute application code directly in the transport thread.
|
abstract T |
executor(Executor executor)
Provides a custom executor.
|
abstract T |
fallbackHandlerRegistry(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.
|
T |
handshakeTimeout(long timeout,
TimeUnit unit)
Sets the permitted time for new connections to complete negotiation handshakes before being
killed.
|
T |
intercept(ServerInterceptor interceptor)
Adds a
ServerInterceptor that is run for all services on the server. |
T |
keepAliveTime(long keepAliveTime,
TimeUnit timeUnit)
Sets the time without read activity before sending a keepalive ping.
|
T |
keepAliveTimeout(long keepAliveTimeout,
TimeUnit timeUnit)
Sets a time waiting for read activity after sending a keepalive ping.
|
T |
maxConnectionAge(long maxConnectionAge,
TimeUnit timeUnit)
Sets the maximum connection age, connections lasting longer than which will be gracefully
terminated.
|
T |
maxConnectionAgeGrace(long maxConnectionAgeGrace,
TimeUnit timeUnit)
Sets the grace time for the graceful connection termination.
|
T |
maxConnectionIdle(long maxConnectionIdle,
TimeUnit timeUnit)
Sets the maximum connection idle time, connections being idle for longer than which will be
gracefully terminated.
|
T |
maxInboundMessageSize(int bytes)
Sets the maximum message size allowed to be received on the server.
|
T |
maxInboundMetadataSize(int bytes)
Sets the maximum size of metadata allowed to be received.
|
T |
permitKeepAliveTime(long keepAliveTime,
TimeUnit timeUnit)
Specify the most aggressive keep-alive time clients are permitted to configure.
|
T |
permitKeepAliveWithoutCalls(boolean permit)
Sets whether to allow clients to send keep-alive HTTP/2 PINGs even if there are no outstanding
RPCs on the connection.
|
T |
setBinaryLog(BinaryLog binaryLog)
Sets the BinaryLog object that this server should log to.
|
abstract T |
useTransportSecurity(File certChain,
File privateKey)
Makes the server use TLS.
|
T |
useTransportSecurity(InputStream certChain,
InputStream privateKey)
Makes the server use TLS.
|
public static ServerBuilder<?> forPort(int port)
port
- the port to listen onpublic abstract T directExecutor()
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.
public abstract T executor(@Nullable Executor 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.
@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/8274") public T callExecutor(ServerCallExecutorSupplier executorSupplier)
ServerCallExecutorSupplier.getExecutor(ServerCall, Metadata)
per RPC.
It's an optional parameter. If it is provided, the executor(Executor)
would still
run necessary tasks before the ServerCallExecutorSupplier
is ready to be called, then
it switches over. But if calling ServerCallExecutorSupplier
returns null, the server
call is still handled by the default executor(Executor)
as a fallback.
executorSupplier
- the server call executor providerpublic abstract T addService(ServerServiceDefinition service)
service
- ServerServiceDefinition objectpublic abstract T addService(BindableService bindableService)
bindableService
- BindableService object@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/7925") public final T addServices(List<ServerServiceDefinition> services)
services
- the list of ServerServiceDefinition objectspublic T intercept(ServerInterceptor interceptor)
ServerInterceptor
that is run for all services on the server. Interceptors
added through this method always run before per-service interceptors added through ServerInterceptors
. Interceptors run in the reverse order in which they are added, just as
with consecutive calls to ServerInterceptors.intercept()
.interceptor
- the all-service interceptor@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/2132") public T addTransportFilter(ServerTransportFilter filter)
ServerTransportFilter
. The order of filters being added is the order they will
be executed.@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/2861") public T addStreamTracerFactory(ServerStreamTracer.Factory factory)
ServerStreamTracer.Factory
to measure server-side traffic. The order of
factories being added is the order they will be executed.public abstract T fallbackHandlerRegistry(@Nullable HandlerRegistry fallbackRegistry)
addService()
) is faster but
immutable. The fallback registry is more flexible and allows implementations to mutate over
time and load services on-demand.public abstract T useTransportSecurity(File certChain, File privateKey)
certChain
- file containing the full certificate chainprivateKey
- file containing the private keyUnsupportedOperationException
- if the server does not support TLS.public T useTransportSecurity(InputStream certChain, InputStream privateKey)
certChain
- InputStream containing the full certificate chainprivateKey
- InputStream containing the private keyUnsupportedOperationException
- if the server does not support TLS, or does not support
reading these files from an InputStream.@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/1704") public abstract T decompressorRegistry(@Nullable DecompressorRegistry registry)
DecompressorRegistry.getDefaultInstance
.@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/1704") public abstract T compressorRegistry(@Nullable CompressorRegistry registry)
CompressorRegistry.getDefaultInstance
.@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/3706") public T handshakeTimeout(long timeout, TimeUnit unit)
IllegalArgumentException
- if timeout is negativeUnsupportedOperationException
- if unsupported@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/9009") public T keepAliveTime(long keepAliveTime, TimeUnit timeUnit)
Long.MAX_VALUE
nano seconds or an unreasonably large
value will disable keepalive. The typical default is two hours when supported.IllegalArgumentException
- if time is not positiveUnsupportedOperationException
- if unsupported@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/9009") public T keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit)
This value should be at least multiple times the RTT to allow for lost packets.
IllegalArgumentException
- if timeout is not positiveUnsupportedOperationException
- if unsupported@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/9009") public T maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit)
Long.MAX_VALUE
nano seconds or an unreasonably large value will disable
max connection idle.IllegalArgumentException
- if idle is not positiveUnsupportedOperationException
- if unsupported@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/9009") public T maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit)
Long.MAX_VALUE
nano seconds or an unreasonably large value will disable
max connection age.IllegalArgumentException
- if age is not positiveUnsupportedOperationException
- if unsupported@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/9009") public T maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit)
Long.MAX_VALUE
nano seconds or an
unreasonably large value are considered infinite.IllegalArgumentException
- if grace is negativeUnsupportedOperationException
- if unsupportedmaxConnectionAge(long, TimeUnit)
@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/9009") public T permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit)
Even though a default is defined that allows some keep-alives, clients must not use keep-alive without approval from the service owner. Otherwise, they may experience failures in the future if the service becomes more restrictive. When unthrottled, keep-alives can cause a significant amount of traffic and CPU usage, so clients and servers should be conservative in what they use and accept.
IllegalArgumentException
- if time is negativeUnsupportedOperationException
- if unsupportedpermitKeepAliveWithoutCalls(boolean)
@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/9009") public T permitKeepAliveWithoutCalls(boolean permit)
false
when supported.UnsupportedOperationException
- if unsupportedpermitKeepAliveTime(long, TimeUnit)
public T maxInboundMessageSize(int bytes)
This method is advisory, and implementations may decide to not enforce this. Currently,
the only known transport to not enforce this is InProcessServer
.
bytes
- the maximum number of bytes a single message can be.IllegalArgumentException
- if bytes is negative.UnsupportedOperationException
- if unsupported.public T maxInboundMetadataSize(int bytes)
Integer.MAX_VALUE
disables
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.
bytes
- the maximum size of received metadataIllegalArgumentException
- if bytes is non-positive@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/4017") public T setBinaryLog(BinaryLog binaryLog)
Closeable.close()
.binaryLog
- the object to provide logging.public abstract Server build()
The returned service will not been started or be bound a port. You will need to start it
with Server.start()
.