Class Grpc

java.lang.Object
com.jsgalactic.axiom.grpc.Grpc
All Implemented Interfaces:
AutoCloseable

public final class Grpc extends Object implements AutoCloseable
Runs generated gRPC services beside an Axiom application. Each instance owns one gRPC server on its own listener, so the HTTP listener keeps speaking HTTP/1.1 and grpc-java keeps its own HTTP/2 transport. Services are ordinary BindableService implementations; no Netty type appears in this API. Closing the application starts a graceful drain of the gRPC server, and a drain of the application stops it accepting new calls.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Collects services and settings before Grpc.Builder.build().
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Waits for the drain to finish without starting one.
    builder(Application application)
    Starts a builder for one gRPC server.
    void
    Begins a graceful drain without waiting; stop() is the waiting variant.
    boolean
    Reports whether this server should receive traffic.
    boolean
    Reports whether the server accepted calls and has not begun shutting down.
    int
    Returns the bound port, or the configured port before startup.
    void
    Starts the server and binds its listener.
    void
    Begins a graceful drain and waits for it to finish.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • builder

      public static Grpc.Builder builder(Application application)
      Starts a builder for one gRPC server. The application supplies the lifecycle hooks; it need not have an HTTP listener.
      Parameters:
      application - the application whose lifecycle owns the server
      Returns:
      a new builder
      Throws:
      NullPointerException - if the application is null
    • start

      public void start() throws IOException
      Starts the server and binds its listener. Calling this twice fails; starting after the application closed fails as well. The application is started if it is still configuring. Services are intercepted before the server starts, so their handlers can read GrpcCall.current() and exceptions are mapped as configured.
      Throws:
      IOException - if the listener cannot bind or TLS material cannot be used
      IllegalStateException - if the server or the application was already started or closed
    • isRunning

      public boolean isRunning()
      Reports whether the server accepted calls and has not begun shutting down.
      Returns:
      true while the listener is accepting calls
    • isReady

      public boolean isReady()
      Reports whether this server should receive traffic. It is true only while isRunning() and before a drain begins, which makes it suitable as an Axiom readiness check.
      Returns:
      true while the server is accepting calls
    • port

      public int port()
      Returns the bound port, or the configured port before startup. Binding with port zero chooses a free port, which this method reports once the server started.
      Returns:
      the port of this server, or -1 for a transport without a socket
    • stop

      public void stop()
      Begins a graceful drain and waits for it to finish. New calls are refused while calls already running may finish within the configured grace period; a call still running after it is cancelled. Idempotent, and safe before startup.
    • awaitTermination

      public boolean awaitTermination(Duration timeout) throws InterruptedException
      Waits for the drain to finish without starting one.
      Parameters:
      timeout - positive or zero, at most one day
      Returns:
      true when the server terminated or was never started
      Throws:
      InterruptedException - if the waiting thread is interrupted
      IllegalArgumentException - if the timeout is out of range
    • close

      public void close()
      Begins a graceful drain without waiting; stop() is the waiting variant.
      Specified by:
      close in interface AutoCloseable