Server-Side Implementation of Bidirectional Streaming RPC
Implement the server side of a bidirectional streaming getFileAttributes RPC in the FTP service.
In this lesson, we demonstrate the server-side implementation of the bidirectional streaming getFileAttributes
RPC. The client will send a stream of file names to the server, and the server will respond by streaming the file attributes of each file back to the client.
The server-side logic is located in the ftp-service
module. If you are following along on your local machine, open the FTPService
class in the src/main/java/io/datajek/ftpservice/server
package. This class implements the FTP service as defined in the proto file. We will add the getFileAttributes
method in the FTPService
class as follows:
@Overridepublic StreamObserver<MetaData> getFileAttributes(StreamObserver<FileAttributesResult> responseObserver) {}
getFileAttributes
method
The method shown above takes a responseObserver
of type StreamObserver<FileAttributesResult>
as a parameter and returns StreamObserver<MetaData>
. Let's understand the use of both stream observers.
StreamObserver<FileAttributesResult>
is used by the server to send responses back to the client. It is provided by the client when calling thegetFileAttributes
method. The service implementation invokes theonNext()
method on this observer to send a response. The defined in theFileAttributesResult
FileAttributesResult_message ftp-service.proto
file contains the outcome of the server’s operations in response to the client’s requests.StreamObserver<MetaData>
: Since the client can send multiple messages to the service, the service implementation returns aStreamObserver<MetaData>
instance. The server will process client requests using the provided . It is invoked whenever additional messages are received from the client. Theobserver StreamObserver2 is defined ...MetaData
Metadata_message