ReqT
- type of message sent one or more times to the server.RespT
- type of message received one or more times from the server.public abstract class ClientCall<ReqT,RespT> extends Object
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. 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. Implementations are not required to be thread-safe.
There is no interaction between the states on the Listener
and ClientCall
, i.e., if Listener.onClose()
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.onClose()
is called with
CANCELLED
. Otherwise, Listener.onClose()
is
called with whatever status the RPC was finished. We ensure that at most one is called.
Example: A simple Unary (1 request, 1 response) RPC would look like this:
call = channel.newCall(method, callOptions); call.start(listener, headers); call.sendMessage(message); call.halfClose(); call.request(1); // wait for listener.onMessage()
Modifier and Type | Class and Description |
---|---|
static class |
ClientCall.Listener<T>
Callbacks for receiving metadata, response messages and completion status from the server.
|
Constructor and Description |
---|
ClientCall() |
Modifier and Type | Method and Description |
---|---|
abstract void |
cancel()
Prevent any further processing for this
ClientCall . |
abstract void |
halfClose()
Close the call for request message sending.
|
boolean |
isReady()
If
true , indicates that the call is capable of sending additional messages
without requiring excessive buffering internally. |
abstract void |
request(int numMessages)
Requests up to the given number of messages from the call to be delivered to
ClientCall.Listener.onMessage(Object) . |
abstract void |
sendMessage(ReqT message)
Send a request message to the server.
|
void |
setMessageCompression(boolean enabled)
Enables per-message compression, if an encoding type has been negotiated.
|
abstract void |
start(ClientCall.Listener<RespT> responseListener,
Metadata headers)
Start a call, using
responseListener for processing response messages. |
public abstract void start(ClientCall.Listener<RespT> responseListener, Metadata headers)
responseListener
for processing response messages.
It must be called prior to any other method on this class. Since Metadata
is not
thread-safe, the caller must not access headers
after this point.
responseListener
- receives response messagesheaders
- which can contain extra call metadata, e.g. authentication credentials.IllegalStateException
- if a method (including start()
) on this class has been
called.public abstract void request(int numMessages)
ClientCall.Listener.onMessage(Object)
. No additional messages will be delivered.
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. Integer.MAX_VALUE
).
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 synchronizaton.
numMessages
- the requested number of messages to be delivered to the listener. Must be
non-negative.public abstract void cancel()
ClientCall
. No further messages may be sent or
will be received. The server is informed of cancellations, but may not stop processing the
call. Cancellation is permitted if previously halfClose()
d. Cancelling an already cancel()
ed ClientCall
has no effect.
No other methods on this class can be called after this method has been called.
public abstract void halfClose()
IllegalStateException
- if call is already halfClose()
d or cancel()
edpublic abstract void sendMessage(ReqT message)
message
- message to be sent to the server.IllegalStateException
- if call is halfClose()
d or explicitly cancel()
edpublic boolean isReady()
true
, indicates that the call is capable of sending additional messages
without requiring excessive buffering internally. This event is
just a suggestion and the application is free to ignore it, however doing so may
result in excessive buffering within the call.
This implementation always returns true
.
@ExperimentalApi public void setMessageCompression(boolean enabled)