The structure of the generated Java gRPC classes

This project contains a basic gRPC service:

The project can generate the Java classes according to the .proto files defined in the project, and here is the class diagram of the greeting related classes together with generated classes:

From the above class diagram, there are several notes:

In addition, the handwritten GreetingServerImpl class extends the GreetingServiceGrpc.GreetingServiceImplBase inner class:

package greeting.server;

import com.proto.greeting.GreetingRequest;
import com.proto.greeting.GreetingResponse;
import com.proto.greeting.GreetingServiceGrpc;
import io.grpc.stub.StreamObserver;

public class GreetingServerImpl extends GreetingServiceGrpc.GreetingServiceImplBase {

    @Override
    public void greet(GreetingRequest request, StreamObserver<GreetingResponse> responseObserver) {
        responseObserver.onNext(GreetingResponse.newBuilder()
                .setResult("Hello : "+request.getFirstName()).build());
        responseObserver.onCompleted();
    }
}

And the GreetingServer uses the GreetingServerImpl:

package greeting.server;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.protobuf.services.ProtoReflectionService;

import java.io.IOException;

public class GreetingServer {
    public static void main(String[] args) throws IOException, InterruptedException {
        int port = 50052;

        Server server = ServerBuilder.forPort(port)
                .addService(new GreetingServerImpl())
                .addService(ProtoReflectionService.newInstance())
                .build();

        server.start();
        System.out.println("Server started");
        System.out.println("Listing on port : "+ port);

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("Received Shutdown Request");
            server.shutdown();
            System.out.println("Server Stopped");
        }));

        server.awaitTermination();
    }
}

Above is the structure of the generated Java gRPC classes.

References

My Github Page: https://github.com/liweinan

Powered by Jekyll and Theme by solid

If you have any question want to ask or find bugs regarding with my blog posts, please report it here:
https://github.com/liweinan/liweinan.github.io/issues