ReqT
- parsed type of request message.RespT
- parsed type of response message.public abstract class ServerCall<ReqT,RespT> extends Object
Headers must be sent before any messages, which must be sent before closing.
No generic method for determining message receipt or providing acknowledgement is provided. Applications are expected to utilize normal 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.
Modifier and Type | Class and Description |
---|---|
static class |
ServerCall.Listener<ReqT>
Callbacks for consuming incoming RPC messages.
|
Modifier and Type | Field and Description |
---|---|
static Attributes.Key<SocketAddress> |
REMOTE_ADDR_KEY
Attributes.Key for the remote address of server call attributes
attributes() |
static Attributes.Key<SSLSession> |
SSL_SESSION_KEY
Attributes.Key for the SSL session of server call attributes
attributes() |
Constructor and Description |
---|
ServerCall() |
Modifier and Type | Method and Description |
---|---|
Attributes |
attributes()
Returns properties of a single call.
|
abstract void |
close(Status status,
Metadata trailers)
Close the call with the provided status.
|
abstract MethodDescriptor<ReqT,RespT> |
getMethodDescriptor()
The
MethodDescriptor for the call. |
abstract boolean |
isCancelled()
Returns
true when the call is cancelled and the server is encouraged to abort
processing to save resources, since the client will not be processing any further methods. |
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
ServerCall.Listener.onMessage(Object) . |
abstract void |
sendHeaders(Metadata headers)
Send response header metadata prior to sending a response message.
|
abstract void |
sendMessage(RespT message)
Send a response message.
|
void |
setCompression(String compressor)
Sets the compression algorithm for this call.
|
void |
setMessageCompression(boolean enabled)
Enables per-message compression, if an encoding type has been negotiated.
|
@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/1710") public static final Attributes.Key<SocketAddress> REMOTE_ADDR_KEY
Attributes.Key
for the remote address of server call attributes
attributes()
@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/1710") public static final Attributes.Key<SSLSession> SSL_SESSION_KEY
Attributes.Key
for the SSL session of server call attributes
attributes()
public abstract void request(int numMessages)
ServerCall.Listener.onMessage(Object)
. Once numMessages
have been delivered
no further request messages will be delivered until more messages are requested by
calling this method again.
Servers use this mechanism to provide back-pressure to the client for flow-control.
This method is safe to call from multiple threads without external synchronizaton.
numMessages
- the requested number of messages to be delivered to the listener.public abstract void sendHeaders(Metadata headers)
sendMessage(RespT)
or close(io.grpc.Status, io.grpc.Metadata)
.
Since Metadata
is not thread-safe, the caller must not access headers
after
this point.
headers
- metadata to send prior to any response body.IllegalStateException
- if close
has been called, a message has been sent, or
headers have already been sentpublic abstract void sendMessage(RespT message)
message
- response message.IllegalStateException
- if headers not sent or call is close(io.grpc.Status, io.grpc.Metadata)
dpublic 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
.
public abstract void close(Status status, Metadata trailers)
Status.isOk()
is false
, then the call is said to have failed.
If no errors or cancellations are known to have occurred, then a ServerCall.Listener.onComplete()
notification should be expected, independent of status
. Otherwise ServerCall.Listener.onCancel()
has been or will be called.
Since Metadata
is not thread-safe, the caller must not access trailers
after
this point.
IllegalStateException
- if call is already close
dpublic abstract boolean isCancelled()
true
when the call is cancelled and the server is encouraged to abort
processing to save resources, since the client will not be processing any further methods.
Cancellations can be caused by timeouts, explicit cancel by client, network errors, and
similar.
This method may safely be called concurrently from multiple threads.
@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/1704") public void setMessageCompression(boolean enabled)
@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/1704") public void setCompression(String compressor)
sendHeaders(io.grpc.Metadata)
.
The compressor to use will be looked up in the CompressorRegistry
. Default gRPC
servers support the "gzip" compressor.compressor
- the name of the compressor to use.IllegalArgumentException
- if the compressor name can not be found.@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/1779") public Attributes attributes()
public abstract MethodDescriptor<ReqT,RespT> getMethodDescriptor()
MethodDescriptor
for the call.