Stay organized with collectionsSave and categorize content based on your preferences.
publicinterfaceService
Abstract base interface for protocol-buffer-based RPC services. Services themselves are abstract
classes (implemented either by servers or as stubs), but they subclass this base interface. The
methods of this interface can be used to call the methods of the service without knowing its
exact type at compile time (analogous to the Message interface).
Starting with version 2.3.0, RPC implementations should not try to build on this, but should
instead provide code generator plugins which generate code specific to the particular RPC
implementation. This way the generated code can be more appropriate for the implementation in use
and can avoid unnecessary layers of indirection.
Call a method of the service specified by MethodDescriptor. This is normally implemented as a
simpleswitch()that calls the standard definitions of the service's methods.
Preconditions:
method.getService() == getDescriptorForType()
requestis of the exact same class as the object returned bygetRequestPrototype(method).
controlleris of the correct type for the RPC implementation being used by this
Service. For stubs, the "correct type" depends on the RpcChannel which the stub is using.
Server-side Service implementations are expected to accept whatever type ofRpcControllerthe server-side RPC implementation uses.
Postconditions:
donewill be called when the method is complete. This may be beforecallMethod()returns or it may be at some point in the future.
The parameter todoneis the response. It must be of the exact same type as would
be returned bygetResponsePrototype(method).
If the RPC failed, the parameter todonewill benull. Further details
about the failure can be found by queryingcontroller.
callMethod()requires that the request passed in is of a particular subclass ofMessage.getRequestPrototype()gets the default instances of this type for a given
method. You can then callMessage.newBuilderForType()on this instance to construct a
builder to build an object which you can then pass tocallMethod().
LikegetRequestPrototype(), but gets a prototype of the response message.getResponsePrototype()is generally not needed because theServiceimplementation
constructs the response message itself, but it may be useful in some cases to know ahead of
time what type of object will be returned.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Interface Service (3.19.4)\n\n public interface Service\n\nAbstract base interface for protocol-buffer-based RPC services. Services themselves are abstract\nclasses (implemented either by servers or as stubs), but they subclass this base interface. The\nmethods of this interface can be used to call the methods of the service without knowing its\nexact type at compile time (analogous to the Message interface).\n\nStarting with version 2.3.0, RPC implementations should not try to build on this, but should\ninstead provide code generator plugins which generate code specific to the particular RPC\nimplementation. This way the generated code can be more appropriate for the implementation in use\nand can avoid unnecessary layers of indirection.\n\nMethods\n-------\n\n### callMethod(Descriptors.MethodDescriptor method, RpcController controller, Message request, RpcCallback\\\u003cMessage\\\u003e done)\n\n public abstract void callMethod(Descriptors.MethodDescriptor method, RpcController controller, Message request, RpcCallback\u003cMessage\u003e done)\n\nCall a method of the service specified by MethodDescriptor. This is normally implemented as a\nsimple `switch()` that calls the standard definitions of the service's methods.\n\nPreconditions:\n\n- `method.getService() == getDescriptorForType()`\n- `request` is of the exact same class as the object returned by `\n getRequestPrototype(method)`.\n- `controller` is of the correct type for the RPC implementation being used by this Service. For stubs, the \"correct type\" depends on the RpcChannel which the stub is using. Server-side Service implementations are expected to accept whatever type of `\n RpcController` the server-side RPC implementation uses.\n\nPostconditions:\n\n- `done` will be called when the method is complete. This may be before `\n callMethod()` returns or it may be at some point in the future.\n- The parameter to `done` is the response. It must be of the exact same type as would be returned by `getResponsePrototype(method)`.\n- If the RPC failed, the parameter to `done` will be `null`. Further details about the failure can be found by querying `controller`.\n\n### getDescriptorForType()\n\n public abstract Descriptors.ServiceDescriptor getDescriptorForType()\n\nGet the `ServiceDescriptor` describing this service and its methods.\n\n### getRequestPrototype(Descriptors.MethodDescriptor method)\n\n public abstract Message getRequestPrototype(Descriptors.MethodDescriptor method)\n\n`callMethod()` requires that the request passed in is of a particular subclass of `\nMessage`. `getRequestPrototype()` gets the default instances of this type for a given\nmethod. You can then call `Message.newBuilderForType()` on this instance to construct a\nbuilder to build an object which you can then pass to `callMethod()`.\n\nExample:\n\nMethodDescriptor method =\nservice.getDescriptorForType().findMethodByName(\"Foo\");\nMessage request =\nstub.getRequestPrototype(method).newBuilderForType()\n.mergeFrom(input).build();\nservice.callMethod(method, request, callback);\n\n### getResponsePrototype(Descriptors.MethodDescriptor method)\n\n public abstract Message getResponsePrototype(Descriptors.MethodDescriptor method)\n\nLike `getRequestPrototype()`, but gets a prototype of the response message. `\ngetResponsePrototype()` is generally not needed because the `Service` implementation\nconstructs the response message itself, but it may be useful in some cases to know ahead of\ntime what type of object will be returned."]]