Class InProcessServerBuilder


  • @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1783")
    public final class InProcessServerBuilder
    extends io.grpc.ForwardingServerBuilder<InProcessServerBuilder>
    Builder for a server that services in-process requests. Clients identify the in-process server by its name.

    The server is intended to be fully-featured, high performance, and useful in testing.

    Using JUnit TestRule

    The class "GrpcServerRule" (from "grpc-java/testing") is a JUnit TestRule that creates a InProcessServer and a ManagedChannel. This test rule contains the boilerplate code shown below. The classes "HelloWorldServerTest" and "HelloWorldClientTest" (from "grpc-java/examples") demonstrate basic usage.

    Usage example

    Server and client channel setup

       String uniqueName = InProcessServerBuilder.generateName();
       Server server = InProcessServerBuilder.forName(uniqueName)
           .directExecutor() // directExecutor is fine for unit tests
           .addService(/* your code here */)
           .build().start();
       ManagedChannel channel = InProcessChannelBuilder.forName(uniqueName)
           .directExecutor()
           .build();
     

    Usage in tests

    The channel can be used normally. A blocking stub example:
       TestServiceGrpc.TestServiceBlockingStub blockingStub =
           TestServiceGrpc.newBlockingStub(channel);
     
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      InProcessServerBuilder deadlineTicker​(io.grpc.Deadline.Ticker ticker)
      Provides a custom deadline ticker that this server will use to create incoming Deadlines.
      protected io.grpc.ServerBuilder<?> delegate()  
      static InProcessServerBuilder forAddress​(java.net.SocketAddress listenAddress)
      Create a server builder which listens on the given address.
      static InProcessServerBuilder forName​(java.lang.String name)
      Create a server builder that will bind with the given name.
      static InProcessServerBuilder forPort​(int port)
      Always fails.
      static java.lang.String generateName()
      Generates a new server name that is unique each time.
      InProcessServerBuilder maxInboundMetadataSize​(int bytes)
      Sets the maximum size of metadata allowed to be received.
      InProcessServerBuilder scheduledExecutorService​(java.util.concurrent.ScheduledExecutorService scheduledExecutorService)
      Provides a custom scheduled executor service.
      InProcessServerBuilder useTransportSecurity​(java.io.File certChain, java.io.File privateKey)  
      • Methods inherited from class io.grpc.ForwardingServerBuilder

        addService, addService, addStreamTracerFactory, addTransportFilter, build, callExecutor, compressorRegistry, decompressorRegistry, directExecutor, executor, fallbackHandlerRegistry, handshakeTimeout, intercept, keepAliveTime, keepAliveTimeout, maxConnectionAge, maxConnectionAgeGrace, maxConnectionIdle, maxInboundMessageSize, permitKeepAliveTime, permitKeepAliveWithoutCalls, setBinaryLog, toString, useTransportSecurity
      • Methods inherited from class io.grpc.ServerBuilder

        addServices
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • forName

        public static InProcessServerBuilder forName​(java.lang.String name)
        Create a server builder that will bind with the given name.
        Parameters:
        name - the identity of the server for clients to connect to
        Returns:
        a new builder
      • forAddress

        public static InProcessServerBuilder forAddress​(java.net.SocketAddress listenAddress)
        Create a server builder which listens on the given address.
        Parameters:
        listenAddress - The SocketAddress this server will listen on.
        Returns:
        a new builder
      • generateName

        public static java.lang.String generateName()
        Generates a new server name that is unique each time.
      • delegate

        @Internal
        protected io.grpc.ServerBuilder<?> delegate()
        Specified by:
        delegate in class io.grpc.ForwardingServerBuilder<InProcessServerBuilder>
      • scheduledExecutorService

        public InProcessServerBuilder scheduledExecutorService​(java.util.concurrent.ScheduledExecutorService scheduledExecutorService)
        Provides a custom scheduled executor service.

        It's an optional parameter. If the user has not provided a scheduled executor service when the channel is built, the builder will use a static cached thread pool.

        Returns:
        this
        Since:
        1.11.0
      • deadlineTicker

        public InProcessServerBuilder deadlineTicker​(io.grpc.Deadline.Ticker ticker)
        Provides a custom deadline ticker that this server will use to create incoming Deadlines.

        This is intended for unit tests that fake out the clock. You should also have a fake ScheduledExecutorService whose clock is synchronized with this ticker and set it to scheduledExecutorService(java.util.concurrent.ScheduledExecutorService). DO NOT use this in production.

        Returns:
        this
        Since:
        1.24.0
        See Also:
        Deadline.after(long, TimeUnit, Deadline.Ticker)
      • maxInboundMetadataSize

        public InProcessServerBuilder maxInboundMetadataSize​(int bytes)
        Sets the maximum size of metadata allowed to be received. Integer.MAX_VALUE disables the enforcement. Defaults to no limit (Integer.MAX_VALUE).

        There is potential for performance penalty when this setting is enabled, as the Metadata must actually be serialized. Since the current implementation of Metadata pre-serializes, it's currently negligible. But Metadata is free to change its implementation.

        Overrides:
        maxInboundMetadataSize in class io.grpc.ForwardingServerBuilder<InProcessServerBuilder>
        Parameters:
        bytes - the maximum size of received metadata
        Returns:
        this
        Throws:
        java.lang.IllegalArgumentException - if bytes is non-positive
        Since:
        1.17.0
      • useTransportSecurity

        public InProcessServerBuilder useTransportSecurity​(java.io.File certChain,
                                                           java.io.File privateKey)
        Overrides:
        useTransportSecurity in class io.grpc.ForwardingServerBuilder<InProcessServerBuilder>