Class GrpcChannel
java.lang.Object
com.codename1.backend.grpc.GrpcChannel
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classOne call in progress: send requests, end them, read responses. -
Method Summary
Modifier and TypeMethodDescription<Q,R> GrpcChannel.Call <Q, R> bidiStreaming(String method, ProtoCodec<Q> requestCodec, ProtoCodec<R> responseCodec) Streams both ways: send withGrpcChannel.Call.send(Q), end withGrpcChannel.Call.complete(), read with the iterator, and close it in a finally.<Q,R> R clientStreaming(String method, ProtoCodec<Q> requestCodec, ProtoCodec<R> responseCodec, Iterator<Q> requests) A stream of requests, one response.static GrpcChannel<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.byte[]A unary call whose messages are already encoded: the request bytes go out as they are and the response comes back undecoded.<Q,R> R unary(String method, ProtoCodec<Q> requestCodec, ProtoCodec<R> responseCodec, Q request) One request, one response.withConnectTimeoutMillis(int millis) withDeadlineMillis(long millis) A channel whose calls each have this deadline, sent asgrpc-timeout.withGzip(boolean on) A channel that gzips its request messages.withMaxMessageSize(int bytes) withMetadata(String name, String value) A channel that also sends this metadata on every call.
-
Method Details
-
forTarget
- Parameters:
target-http://host:portorhttps://host:port, no path- Returns:
- a channel to it
-
withMetadata
A channel that also sends this metadata on every call.- Parameters:
name- lower-case header name; a-binname takes base64 textvalue- the value- Returns:
- the new channel
-
withDeadlineMillis
A channel whose calls each have this deadline, sent asgrpc-timeout.- Parameters:
millis- the deadline from the start of each call; negative for none- Returns:
- the new channel
-
withConnectTimeoutMillis
- Parameters:
millis- the connect timeout- Returns:
- the new channel
-
withGzip
A channel that gzips its request messages. Only for servers known to acceptgrpc-encoding: gzip; responses are accepted gzipped regardless.- Parameters:
on- whether to compress- Returns:
- the new channel
-
withMaxMessageSize
- Parameters:
bytes- the largest response message accepted- Returns:
- the new channel
-
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 typeR- response type- Parameters:
method-/package.Service/MethodrequestCodec- the request codecresponseCodec- the response codecrequest- the request- Returns:
- the response
-
unary
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/Methodrequest- 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 aGrpcExceptionwhere the stream fails; read it to its end orGrpcChannel.Call.close()it.- Type Parameters:
Q- request typeR- response type- Parameters:
method-/package.Service/MethodrequestCodec- the request codecresponseCodec- the response codecrequest- 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 typeR- response type- Parameters:
method-/package.Service/MethodrequestCodec- the request codecresponseCodec- the response codecrequests- 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 withGrpcChannel.Call.send(Q), end withGrpcChannel.Call.complete(), read with the iterator, and close it in a finally.- Type Parameters:
Q- request typeR- response type- Parameters:
method-/package.Service/MethodrequestCodec- the request codecresponseCodec- the response codec- Returns:
- the call
-