public final class GrpcContainer
extends java.lang.Object
Maintains and deploys several of gRPC services within a single server.
Uses ServerServiceDefinition
s of each service.
Modifier and Type | Class and Description |
---|---|
static class |
GrpcContainer.Builder
The builder for
GrpcContainer allows to define a port and services exposed
by the container. |
static interface |
GrpcContainer.ConfigureServer
Allows to configure the gRPC's
Server instance,
on top of which this GrpcContainer will operate. |
Modifier and Type | Method and Description |
---|---|
void |
addShutdownHook()
Makes the JVM shut down the service when it is shutting down itself.
|
static GrpcContainer.Builder |
atPort(int port)
Initiates creating a container exposed at the given port.
|
void |
awaitTermination()
Waits for the service to become terminated.
|
void |
injectServer(io.grpc.Server server)
Injects a server to this container.
|
static GrpcContainer.Builder |
inProcess(java.lang.String serverName)
Initiates creating an in-process container exposed with the given server name.
|
boolean |
isLive(io.grpc.BindableService service)
Check if the given gRPC service has actually been deployed and is available for interaction
within this container.
|
boolean |
isScheduledForDeployment(io.grpc.BindableService service)
Check if the given gRPC service is scheduled for the deployment in this container.
|
boolean |
isShutdown()
Returns
true if the server is shut down or was not started at all,
false otherwise. |
static GrpcContainer.Builder |
newBuilder()
Deprecated.
please use
GrpcContainer.atPort(int) or GrpcContainer.inProcess(String) |
java.util.Optional<java.lang.Integer> |
port()
Obtains the port at which the container is exposed, or empty
Optional if this
is an in-process container. |
java.util.Optional<java.lang.String> |
serverName()
Obtains the name of the in-process server, or empty
Optinal if the container is
exposed at a port. |
void |
shutdown()
Initiates an orderly shutdown in which existing calls continue but new calls are rejected.
|
void |
shutdownNowAndWait()
Initiates a forceful shutdown in which preexisting and new calls are rejected.
|
void |
start()
Starts the service.
|
void |
start(java.util.concurrent.Executor executor)
Starts the service on top of the given
Executor . |
@Deprecated public static GrpcContainer.Builder newBuilder()
GrpcContainer.atPort(int)
or GrpcContainer.inProcess(String)
public static GrpcContainer.Builder atPort(int port)
public static GrpcContainer.Builder inProcess(java.lang.String serverName)
The container in fully-featured, high performance, and is useful in testing.
public java.util.Optional<java.lang.Integer> port()
Optional
if this
is an in-process container.GrpcContainer.serverName()
public java.util.Optional<java.lang.String> serverName()
Optinal
if the container is
exposed at a port.GrpcContainer.port()
public void start() throws java.io.IOException
A gRPC-default executor is used for the server routines. I.e., in-process gRPC server is going to run on top of a direct executor, and a server exposed at a port will run on top of a shared cached thread pool.
See the ServerBuilder
Javadocs corresponding to the current gRPC version
for more details.
java.io.IOException
- if unable to bindpublic void start(java.util.concurrent.Executor executor) throws java.io.IOException
Executor
.executor
- an executor to use for gRPC serverjava.io.IOException
- if unable to bindpublic boolean isShutdown()
true
if the server is shut down or was not started at all,
false
otherwise.GrpcContainer.shutdown()
public void shutdown()
public void shutdownNowAndWait()
The method returns when the service becomes terminated.
The most common usage scenario for this method is clean-up in unit tests
(e.g. @AfterEach
in JUnit5) that involve gRPC communications.
public void awaitTermination()
public boolean isScheduledForDeployment(io.grpc.BindableService service)
Note that the given gRPC service will become available to the clients, once the gRPC container is started.
To find out whether the service is already available for calls,
use GrpcContainer.isLive(BindableService)
method.
service
- the gRPC service to checktrue
, if the given gRPC service for deployment and false
otherwisepublic boolean isLive(io.grpc.BindableService service)
Returns true
if and only if
a. the service has been previously scheduled for the deployment,
b. the container has been started.
service
- the gRPC servicetrue
, if the service is available for interaction within this container and
false
otherwisepublic void addShutdownHook()
Call this method when running the service in a separate JVM.
public void injectServer(io.grpc.Server server)
All calls to GrpcContainer.createGrpcServer(Executor)
will resolve to the given server
instance. The server instance is used as-is, no other
configuration methods have any effect on it.
A test-only method.