...

/

Server-Side Implementation of Bidirectional Streaming RPC

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:

@Override
public StreamObserver<MetaData> getFileAttributes(StreamObserver<FileAttributesResult> responseObserver) {
}
getFileAttributes method in the FTPService class

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 the getFileAttributes method. The service implementation invokes the onNext() method on this observer to send a response. The FileAttributesResultFileAttributesResult_message defined in the 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 a StreamObserver<MetaData> instance. The server will process client requests using the provided observerStreamObserver2. It is invoked whenever additional messages are received from the client. The MetaDataMetadata_message is defined ...