From 4f7949c36f6f269a7111e5cfec9aa63c7fc8b922 Mon Sep 17 00:00:00 2001 From: mohammed yusuf <121228834+mohammedyusuf380@users.noreply.github.com> Date: Thu, 25 Jan 2024 02:39:48 +0530 Subject: [PATCH] =?UTF-8?q?Added=20comments=20on=20quickstart,=20java:=20T?= =?UTF-8?q?o=20enhancing=20user=20comprehension=20a=E2=80=A6=20(#1250)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added comments on quickstart, java: To enhancing user comprehension and readability. --- content/en/docs/languages/java/quickstart.md | 30 ++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/content/en/docs/languages/java/quickstart.md b/content/en/docs/languages/java/quickstart.md index 186ba3e..d2585e7 100644 --- a/content/en/docs/languages/java/quickstart.md +++ b/content/en/docs/languages/java/quickstart.md @@ -90,19 +90,21 @@ with the same request and response types as `SayHello()`: ```protobuf // The greeting service definition. service Greeter { - // Sends a greeting + // Sends a greeting. Original method. rpc SayHello (HelloRequest) returns (HelloReply) {} - // Sends another greeting + // Sends another greeting. New method. rpc SayHelloAgain (HelloRequest) returns (HelloReply) {} } // The request message containing the user's name. message HelloRequest { + // The name of the user. string name = 1; } // The response message containing the greetings message HelloReply { + // The greeting message. string message = 1; } ``` @@ -126,19 +128,30 @@ In the same directory, open new method like this: ```java +// Implementation of the gRPC service on the server-side. private class GreeterImpl extends GreeterGrpc.GreeterImplBase { @Override public void sayHello(HelloRequest req, StreamObserver responseObserver) { + // Generate a greeting message for the original method HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build(); + + // Send the reply back to the client. responseObserver.onNext(reply); + + // Indicate that no further messages will be sent to the client. responseObserver.onCompleted(); } @Override public void sayHelloAgain(HelloRequest req, StreamObserver responseObserver) { + // Generate another greeting message for the new method. HelloReply reply = HelloReply.newBuilder().setMessage("Hello again " + req.getName()).build(); + + // Send the reply back to the client. responseObserver.onNext(reply); + + // Indicate that no further messages will be sent to the client. responseObserver.onCompleted(); } } @@ -151,23 +164,36 @@ In the same directory, open method like this: ```java +// Client-side logic for interacting with the gRPC service. public void greet(String name) { + // Log a message indicating the intention to greet a user. logger.info("Will try to greet " + name + " ..."); + + // Creating a request with the user's name. HelloRequest request = HelloRequest.newBuilder().setName(name).build(); HelloReply response; try { + // Call the original method on the server. response = blockingStub.sayHello(request); } catch (StatusRuntimeException e) { + // Log a warning if the RPC fails. logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); return; } + + // Log the response from the original method. logger.info("Greeting: " + response.getMessage()); + try { + // Call the new method on the server. response = blockingStub.sayHelloAgain(request); } catch (StatusRuntimeException e) { + // Log a warning if the RPC fails. logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); return; } + + // Log the response from the new method. logger.info("Greeting: " + response.getMessage()); } ```