Package io.grpc

Class Server


  • @ThreadSafe
    public abstract class Server
    extends java.lang.Object
    Server for listening for and dispatching incoming calls. It is not expected to be implemented by application code or interceptors.
    • Constructor Summary

      Constructors 
      Constructor Description
      Server()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract void awaitTermination()
      Waits for the server to become terminated.
      abstract boolean awaitTermination​(long timeout, java.util.concurrent.TimeUnit unit)
      Waits for the server to become terminated, giving up if the timeout is reached.
      java.util.List<ServerServiceDefinition> getImmutableServices()
      Returns immutable services registered with the server, or an empty list if not supported by the implementation.
      java.util.List<? extends java.net.SocketAddress> getListenSockets()
      Returns a list of listening sockets for this server.
      java.util.List<ServerServiceDefinition> getMutableServices()
      Returns mutable services registered with the server, or an empty list if not supported by the implementation.
      int getPort()
      Returns the port number the server is listening on.
      java.util.List<ServerServiceDefinition> getServices()
      Returns all services registered with the server, or an empty list if not supported by the implementation.
      abstract boolean isShutdown()
      Returns whether the server is shutdown.
      abstract boolean isTerminated()
      Returns whether the server is terminated.
      abstract Server shutdown()
      Initiates an orderly shutdown in which preexisting calls continue but new calls are rejected.
      abstract Server shutdownNow()
      Initiates a forceful shutdown in which preexisting and new calls are rejected.
      abstract Server start()
      Bind and start the server.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Server

        public Server()
    • Method Detail

      • start

        public abstract Server start()
                              throws java.io.IOException
        Bind and start the server. After this call returns, clients may begin connecting to the listening socket(s).
        Returns:
        this object
        Throws:
        java.lang.IllegalStateException - if already started or shut down
        java.io.IOException - if unable to bind
        Since:
        1.0.0
      • getPort

        public int getPort()
        Returns the port number the server is listening on. This can return -1 if there is no actual port or the result otherwise does not make sense. Result is undefined after the server is terminated. If there are multiple possible ports, this will return one arbitrarily. Implementations are encouraged to return the same port on each call.
        Throws:
        java.lang.IllegalStateException - if the server has not yet been started.
        Since:
        1.0.0
        See Also:
        getListenSockets()
      • getListenSockets

        @ExperimentalApi("https://github.com/grpc/grpc-java/issues/5332")
        public java.util.List<? extends java.net.SocketAddress> getListenSockets()
        Returns a list of listening sockets for this server. May be different than the originally requested sockets (e.g. listening on port '0' may end up listening on a different port). The list is unmodifiable.
        Throws:
        java.lang.IllegalStateException - if the server has not yet been started.
        Since:
        1.19.0
      • getServices

        @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2222")
        public java.util.List<ServerServiceDefinition> getServices()
        Returns all services registered with the server, or an empty list if not supported by the implementation.
        Since:
        1.1.0
      • getImmutableServices

        @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2222")
        public java.util.List<ServerServiceDefinition> getImmutableServices()
        Returns immutable services registered with the server, or an empty list if not supported by the implementation.
        Since:
        1.1.0
      • getMutableServices

        @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2222")
        public java.util.List<ServerServiceDefinition> getMutableServices()
        Returns mutable services registered with the server, or an empty list if not supported by the implementation.
        Since:
        1.1.0
      • shutdown

        public abstract Server shutdown()
        Initiates an orderly shutdown in which preexisting calls continue but new calls are rejected. After this call returns, this server has released the listening socket(s) and may be reused by another server.

        Note that this method will not wait for preexisting calls to finish before returning. awaitTermination() or awaitTermination(long, TimeUnit) needs to be called to wait for existing calls to finish.

        Calling this method before start() will shut down and terminate the server like normal, but prevents starting the server in the future.

        Returns:
        this object
        Since:
        1.0.0
      • shutdownNow

        public abstract Server shutdownNow()
        Initiates a forceful shutdown in which preexisting and new calls are rejected. Although forceful, the shutdown process is still not instantaneous; isTerminated() will likely return false immediately after this method returns. After this call returns, this server has released the listening socket(s) and may be reused by another server.

        Calling this method before start() will shut down and terminate the server like normal, but prevents starting the server in the future.

        Returns:
        this object
        Since:
        1.0.0
      • isShutdown

        public abstract boolean isShutdown()
        Returns whether the server is shutdown. Shutdown servers reject any new calls, but may still have some calls being processed.
        Since:
        1.0.0
        See Also:
        shutdown(), isTerminated()
      • isTerminated

        public abstract boolean isTerminated()
        Returns whether the server is terminated. Terminated servers have no running calls and relevant resources released (like TCP connections).
        Since:
        1.0.0
        See Also:
        isShutdown()
      • awaitTermination

        public abstract boolean awaitTermination​(long timeout,
                                                 java.util.concurrent.TimeUnit unit)
                                          throws java.lang.InterruptedException
        Waits for the server to become terminated, giving up if the timeout is reached.

        Calling this method before start() or shutdown() is permitted and does not change its behavior.

        Returns:
        whether the server is terminated, as would be done by isTerminated().
        Throws:
        java.lang.InterruptedException
      • awaitTermination

        public abstract void awaitTermination()
                                       throws java.lang.InterruptedException
        Waits for the server to become terminated.

        Calling this method before start() or shutdown() is permitted and does not change its behavior.

        Throws:
        java.lang.InterruptedException
        Since:
        1.0.0