mirror of https://github.com/grpc/grpc.io.git
				
				
				
			Added comments on quickstart, java: To enhancing user comprehension a… (#1250)
Added comments on quickstart, java: To enhancing user comprehension and readability.
This commit is contained in:
		
							parent
							
								
									5369677bab
								
							
						
					
					
						commit
						4f7949c36f
					
				|  | @ -90,19 +90,21 @@ with the same request and response types as `SayHello()`: | ||||||
| ```protobuf | ```protobuf | ||||||
| // The greeting service definition. | // The greeting service definition. | ||||||
| service Greeter { | service Greeter { | ||||||
|   // Sends a greeting |   // Sends a greeting. Original method. | ||||||
|   rpc SayHello (HelloRequest) returns (HelloReply) {} |   rpc SayHello (HelloRequest) returns (HelloReply) {} | ||||||
|   // Sends another greeting |   // Sends another greeting. New method. | ||||||
|   rpc SayHelloAgain (HelloRequest) returns (HelloReply) {} |   rpc SayHelloAgain (HelloRequest) returns (HelloReply) {} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // The request message containing the user's name. | // The request message containing the user's name. | ||||||
| message HelloRequest { | message HelloRequest { | ||||||
|  |   // The name of the user. | ||||||
|   string name = 1; |   string name = 1; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // The response message containing the greetings | // The response message containing the greetings | ||||||
| message HelloReply { | message HelloReply { | ||||||
|  |   // The greeting message. | ||||||
|   string message = 1; |   string message = 1; | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
|  | @ -126,19 +128,30 @@ In the same directory, open | ||||||
| new method like this: | new method like this: | ||||||
| 
 | 
 | ||||||
| ```java | ```java | ||||||
|  | // Implementation of the gRPC service on the server-side. | ||||||
| private class GreeterImpl extends GreeterGrpc.GreeterImplBase { | private class GreeterImpl extends GreeterGrpc.GreeterImplBase { | ||||||
| 
 | 
 | ||||||
|   @Override |   @Override | ||||||
|   public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) { |   public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) { | ||||||
|  |     // Generate a greeting message for the original method | ||||||
|     HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build(); |     HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build(); | ||||||
|  | 
 | ||||||
|  |     // Send the reply back to the client. | ||||||
|     responseObserver.onNext(reply); |     responseObserver.onNext(reply); | ||||||
|  | 
 | ||||||
|  |     // Indicate that no further messages will be sent to the client. | ||||||
|     responseObserver.onCompleted(); |     responseObserver.onCompleted(); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   @Override |   @Override | ||||||
|   public void sayHelloAgain(HelloRequest req, StreamObserver<HelloReply> responseObserver) { |   public void sayHelloAgain(HelloRequest req, StreamObserver<HelloReply> responseObserver) { | ||||||
|  |     // Generate another greeting message for the new method. | ||||||
|     HelloReply reply = HelloReply.newBuilder().setMessage("Hello again " + req.getName()).build(); |     HelloReply reply = HelloReply.newBuilder().setMessage("Hello again " + req.getName()).build(); | ||||||
|  | 
 | ||||||
|  |     // Send the reply back to the client. | ||||||
|     responseObserver.onNext(reply); |     responseObserver.onNext(reply); | ||||||
|  | 
 | ||||||
|  |     // Indicate that no further messages will be sent to the client. | ||||||
|     responseObserver.onCompleted(); |     responseObserver.onCompleted(); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | @ -151,23 +164,36 @@ In the same directory, open | ||||||
| method like this: | method like this: | ||||||
| 
 | 
 | ||||||
| ```java | ```java | ||||||
|  | // Client-side logic for interacting with the gRPC service. | ||||||
| public void greet(String name) { | public void greet(String name) { | ||||||
|  |   // Log a message indicating the intention to greet a user. | ||||||
|   logger.info("Will try to greet " + name + " ..."); |   logger.info("Will try to greet " + name + " ..."); | ||||||
|  | 
 | ||||||
|  |   // Creating a request with the user's name. | ||||||
|   HelloRequest request = HelloRequest.newBuilder().setName(name).build(); |   HelloRequest request = HelloRequest.newBuilder().setName(name).build(); | ||||||
|   HelloReply response; |   HelloReply response; | ||||||
|   try { |   try { | ||||||
|  |     // Call the original method on the server. | ||||||
|     response = blockingStub.sayHello(request); |     response = blockingStub.sayHello(request); | ||||||
|   } catch (StatusRuntimeException e) { |   } catch (StatusRuntimeException e) { | ||||||
|  |     // Log a warning if the RPC fails. | ||||||
|     logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); |     logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   // Log the response from the original method. | ||||||
|   logger.info("Greeting: " + response.getMessage()); |   logger.info("Greeting: " + response.getMessage()); | ||||||
|  | 
 | ||||||
|   try { |   try { | ||||||
|  |     // Call the new method on the server. | ||||||
|     response = blockingStub.sayHelloAgain(request); |     response = blockingStub.sayHelloAgain(request); | ||||||
|   } catch (StatusRuntimeException e) { |   } catch (StatusRuntimeException e) { | ||||||
|  |     // Log a warning if the RPC fails. | ||||||
|     logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); |     logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   // Log the response from the new method. | ||||||
|   logger.info("Greeting: " + response.getMessage()); |   logger.info("Greeting: " + response.getMessage()); | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue