Instances are created
by a Channel and used by stubs to invoke their remote behavior.
More advanced usages may consume this interface directly as opposed to using a stub. Common reasons for doing so would be the need to interact with flow-control or when acting as a generic proxy for arbitrary operations.
start must be called prior to calling any other methods, with the exception of
cancel. Whereas cancel must not be followed by any other methods,
but can be called more than once, while only the first one has effect.
No generic method for determining message receipt or providing acknowledgement is provided. Applications are expected to utilize normal payload messages for such signals, as a response naturally acknowledges its request.
Methods are guaranteed to be non-blocking. Not thread-safe except for request, which
may be called from any thread.
There is no interaction between the states on the Listener and ClientCall, i.e., if Listener. is called, it has no bearing on
the permitted operations on ClientCall (but it may impact whether they do anything).
There is a race between cancel and the completion/failure of the RPC in other ways.
If cancel won the race, Listener. is called with
CANCELLED. Otherwise, Listener. is
called with whatever status the RPC was finished. We ensure that at most one is called.
call = channel.newCall(unaryMethod, callOptions); call.start(listener, headers); call.sendMessage(message); call.halfClose(); call.request(1); // wait for listener.onMessage()
The following snippet demonstrates a bi-directional streaming case, where the client sends
requests produced by a fictional makeNextRequest() in a flow-control-compliant
manner, and notifies gRPC library to receive additional response after one is consumed by
a fictional processResponse().
call = channel.newCall(bidiStreamingMethod, callOptions);
listener = new ClientCall.Listener<FooResponse>() {
@Override
public void onMessage(FooResponse response) {
processResponse(response);
// Notify gRPC to receive one additional response.
call.request(1);
}
@Override
public void onReady() {
while (call.isReady()) {
FooRequest nextRequest = makeNextRequest();
if (nextRequest == null) { // No more requests to send
call.halfClose();
return;
}
call.sendMessage(nextRequest);
}
}
}
call.start(listener, headers);
// Notify gRPC to receive one response. Without this line, onMessage() would never be called.
call.request(1);
DO NOT MOCK: Use InProcessServerBuilder and make a test server instead.
| Modifier and Type | Class and Description |
|---|---|
| public abstract static class | ClientCall.Listener<
type of message received. T>Callbacks for receiving metadata, response messages and completion status from the server. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public abstract void | |
| public Attributes | |
| public abstract void | |
| public boolean | isReady()
If |
| public abstract void | request(int
the requested number of messages to be delivered to the listener. Must be
non-negative. numMessages)Requests up to the given number of messages from the call to be delivered to
|
| public abstract void | |
| public void | setMessageCompression(boolean enabled)
Enables per-message compression, if an encoding type has been negotiated. |
| public abstract void | start(ClientCall.
receives response messages responseListener,which can contain extra call metadata, e.g. authentication credentials. headers)Start a call, using |
| ClientCall | back to summary |
|---|---|
| public ClientCall() | |
| cancel | back to summary |
|---|---|
| public abstract void cancel(String message, Throwable cause) Prevent any further processing for this No other methods on this class can be called after this method has been called. It is recommended that at least one of the arguments to be non- | |
| getAttributes | back to summary |
|---|---|
| public Attributes getAttributes() Returns additional properties of the call. May only be called after
| |
| halfClose | back to summary |
|---|---|
| public abstract void halfClose() Close the call for request message sending. Incoming response messages are unaffected. This should be called when no more messages will be sent from the client.
| |
| isReady | back to summary |
|---|---|
| public boolean isReady() If If If the type of the call is either This abstract class's implementation always returns | |
| request | back to summary |
|---|---|
| public abstract void request(int numMessages) Requests up to the given number of messages from the call to be delivered to
Message delivery is guaranteed to be sequential in the order received. In addition, the listener methods will not be accessed concurrently. While it is not guaranteed that the same thread will always be used, it is guaranteed that only a single thread will access the listener at a time. If it is desired to bypass inbound flow control, a very large number of messages can be
specified (e.g. If called multiple times, the number of messages able to delivered will be the sum of the calls. This method is safe to call from multiple threads without external synchronization.
| |
| sendMessage | back to summary |
|---|---|
| public abstract void sendMessage(ReqT message) Send a request message to the server. May be called zero or more times depending on how many messages the server is willing to accept for the operation.
| |
| setMessageCompression | back to summary |
|---|---|
| public void setMessageCompression(boolean enabled) Enables per-message compression, if an encoding type has been negotiated. If no message encoding has been negotiated, this is a no-op. By default per-message compression is enabled, but may not have any effect if compression is not enabled on the call. | |
| start | back to summary |
|---|---|
| public abstract void start(ClientCall. Start a call, using It must be called prior to any other method on this class, except for Since
| |
Implementations are free to block for extended periods of time. Implementations are not required to be thread-safe, but they must not be thread-hostile. The caller is free to call an instance from multiple threads, but only one call simultaneously. A single thread may interleave calls to multiple instances, so implementations using ThreadLocals must be careful to avoid leaking inappropriate state (e.g., clearing the ThreadLocal before returning).
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| public void | |
| public void | onReady()
This indicates that the ClientCall may now be capable of sending additional messages (via
|
| Listener | back to summary |
|---|---|
| public Listener() | |
| onClose | back to summary |
|---|---|
| public void onClose(Status status, Metadata trailers) The ClientCall has been closed. Any additional calls to the Since If This method should not throw. If this method throws, there is no way to be notified of the exception. Implementations should therefore be careful of exceptions which can accidentally leak resources. | |
| onHeaders | back to summary |
|---|---|
| public void onHeaders(Metadata headers) The response headers have been received. Headers always precede messages. Since
| |
| onMessage | back to summary |
|---|---|
| public void onMessage(T message) A response message has been received. May be called zero or more times depending on whether the call response is empty, a single message or a stream of messages.
| |
| onReady | back to summary |
|---|---|
| public void onReady() This indicates that the ClientCall may now be capable of sending additional messages (via
Because there is a processing delay to deliver this notification, it is possible for
concurrent writes to cause If the type of a call is either | |