-`StreamObserver<Helloworld.HelloReply>`: a response observer, which is
@ -318,9 +312,7 @@ To return our response to the client and complete the call:
1. We construct and populate a `HelloReply` response object with our exciting
message, as specified in our interface definition.
2. We call `responseObserver.onValue()` with the `HelloReply` that we want to send back to the client.
3. Finally, we call `responseObserver.onCompleted()` to indicate that we're
finished dealing with this RPC.
2. We use the`responseObserver` to return the `HelloReply` to the client and then specify that we've finished dealing with the RPC
#### Server implementation
@ -342,17 +334,11 @@ implementation available from the network.
```
The `GreetingsServer` class has a `ServerImpl` member that actually runs the
server. To create an appropriate `ServerImpl`, we use a special `ServerBuilder`
class (in this case a `NettyServerBuilder`) in the `GreetingsServer`'s `start`
method, binding the `GreetingsService` implementation that we created to a
Here we create an appropriate gRPC server, binding the `GreetingsService` implementation that we created to a
port. Then we start the server running: the server is now ready to receive
requests from `Greetings` service clients on our specified port. We'll cover
how all this works in a bit more detail in our language-specific documentation.
`GreetingsServer` also has a `stop` method that takes care of shutting down
the service and cleaning up when the program exits.
#### Build it
Once we've implemented everything, we use Maven to build the server:
@ -376,12 +362,7 @@ Again, we're not going to go into much detail about how to implement a client
#### Connecting to the service
First let's look at how we connect to the `Greetings` server. The internet
address
is configured in the client constructor. gRPC `Channel` provides the
abstraction layer over
transport handling; its constructor accepts the host name and port of the
service. The channel in turn is used to construct the stub instance.
First let's look at how we connect to the `Greetings` server. First we need to create a gRPC channel, specifying the hostname and port of the server we want to connect to. Then we use the channel to construct the stub instance.
```java
@ -402,14 +383,12 @@ for the server to respond, and will either return a response or raise an
exception. gRPC Java has other kinds of stubs that make non-blocking calls
to the server, where the response is returned asynchronously.
#### Obtaining a greeting
#### Calling an RPC
The `greet()` method uses the stub to contact the service and obtain
a greeting.
To do this:
Now we can contact the service and obtain a greeting:
1. We construct and fill in a `HelloRequest` to send to the stub.
2. We call the RPC with our request and get a `HelloReply`from the stub,
1. We construct and fill in a `HelloRequest` to send to the service.
2. We call the stub's `hello()`RPC with our request and get a `HelloReply` back,
from which we can get our greeting.
@ -429,27 +408,10 @@ from which we can get our greeting.
```
#### Running from the command line
The main method puts together the example so that it can be run from a command
line.
```java
/* Access a service running on the local machine on port 50051 */
HelloClient client = new HelloClient("localhost", 50051);
String user = "world";
if (args.length > 1) {
user = args[1];
}
client.greet(user);
```
#### Build the client
This is the same as building the server: our client and server are part of
the same maven
package so the same command builds both.
the same maven package so the same command builds both.
```
$ mvn package
@ -473,6 +435,8 @@ $ ./run_greetings_client.sh
### Adding another client
Finally, let's look at one of gRPC's most useful features - interoperability between code in different languages. So far, we've just generated Java code from our `Greetings` service definition.