Class GrpcChannel

java.lang.Object
com.codename1.backend.grpc.GrpcChannel

public final class GrpcChannel extends Object

A gRPC client: calls the methods of a server over HTTP/2.

GrpcChannel channel = GrpcChannel.forTarget("http://inventory:50051")
        .withMetadata("authorization", "Bearer " + token)
        .withDeadlineMillis(2000);
Stock stock = channel.unary("/shop.Inventory/Get", ItemCodec.INSTANCE,
        StockCodec.INSTANCE, item);

http:// is cleartext HTTP/2 with prior knowledge, as gRPC servers listen; https:// negotiates h2 over TLS. Every call has a connection of its own and is closed when it ends, so a channel holds nothing and needs no closing.

A non-OK status is thrown as a GrpcException; a call that never reached the server is UNAVAILABLE. Generated @GrpcClient stubs call these methods.

A GrpcChannel.Call is driven from one thread: a bidirectional call sends, then reads, in whatever interleaving the protocol between the two programs needs. A server that waits for more requests before it answers, while the client waits for an answer, deadlocks the way it would with any blocking client.

  • Method Details

    • forTarget

      public static GrpcChannel forTarget(String target)
      Parameters:
      target - http://host:port or https://host:port, no path
      Returns:
      a channel to it
    • withMetadata

      public GrpcChannel withMetadata(String name, String value)
      A channel that also sends this metadata on every call.
      Parameters:
      name - lower-case header name; a -bin name takes base64 text
      value - the value
      Returns:
      the new channel
    • withDeadlineMillis

      public GrpcChannel withDeadlineMillis(long millis)
      A channel whose calls each have this deadline, sent as grpc-timeout.
      Parameters:
      millis - the deadline from the start of each call; negative for none
      Returns:
      the new channel
    • withConnectTimeoutMillis

      public GrpcChannel withConnectTimeoutMillis(int millis)
      Parameters:
      millis - the connect timeout
      Returns:
      the new channel
    • withGzip

      public GrpcChannel withGzip(boolean on)
      A channel that gzips its request messages. Only for servers known to accept grpc-encoding: gzip; responses are accepted gzipped regardless.
      Parameters:
      on - whether to compress
      Returns:
      the new channel
    • withMaxMessageSize

      public GrpcChannel withMaxMessageSize(int bytes)
      Parameters:
      bytes - the largest response message accepted
      Returns:
      the new channel
    • getTarget

      public String getTarget()
      Returns:
      the target
    • unary

      public <Q,R> R unary(String method, ProtoCodec<Q> requestCodec, ProtoCodec<R> responseCodec, Q request)
      One request, one response.
      Type Parameters:
      Q - request type
      R - response type
      Parameters:
      method - /package.Service/Method
      requestCodec - the request codec
      responseCodec - the response codec
      request - the request
      Returns:
      the response
    • unary

      public byte[] unary(String method, byte[] request)
      A unary call whose messages are already encoded: the request bytes go out as they are and the response comes back undecoded. For payloads built by hand, such as OTLP's export requests.
      Parameters:
      method - /package.Service/Method
      request - the encoded request message
      Returns:
      the encoded response message
    • serverStreaming

      public <Q,R> GrpcChannel.Call<Q,R> serverStreaming(String method, ProtoCodec<Q> requestCodec, ProtoCodec<R> responseCodec, Q request)
      One request, a stream of responses, read as they arrive. The iterator throws a GrpcException where the stream fails; read it to its end or GrpcChannel.Call.close() it.
      Type Parameters:
      Q - request type
      R - response type
      Parameters:
      method - /package.Service/Method
      requestCodec - the request codec
      responseCodec - the response codec
      request - the request
      Returns:
      the responses
    • clientStreaming

      public <Q,R> R clientStreaming(String method, ProtoCodec<Q> requestCodec, ProtoCodec<R> responseCodec, Iterator<Q> requests)
      A stream of requests, one response.
      Type Parameters:
      Q - request type
      R - response type
      Parameters:
      method - /package.Service/Method
      requestCodec - the request codec
      responseCodec - the response codec
      requests - the requests, sent in order
      Returns:
      the response
    • bidiStreaming

      public <Q,R> GrpcChannel.Call<Q,R> bidiStreaming(String method, ProtoCodec<Q> requestCodec, ProtoCodec<R> responseCodec)
      Streams both ways: send with GrpcChannel.Call.send(Q), end with GrpcChannel.Call.complete(), read with the iterator, and close it in a finally.
      Type Parameters:
      Q - request type
      R - response type
      Parameters:
      method - /package.Service/Method
      requestCodec - the request codec
      responseCodec - the response codec
      Returns:
      the call