You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 9, 2021. It is now read-only.
Copy file name to clipboardExpand all lines: docs/tutorials/basic/java.md
+11-8Lines changed: 11 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -143,7 +143,7 @@ There are two parts to making our `RouteGuide` service do its job:
143
143
- Implementing the service interface generated from our service definition: doing the actual "work" of our service.
144
144
- Running a gRPC server to listen for requests from clients and return the service responses.
145
145
146
-
You can find our example `RouteGuide` server in [grpc-java/examples/src/main/java/io/grpc/examples/RouteGuideServer.java](https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/RouteGuideServer.java). Let's take a closer look at how it works.
146
+
You can find our example `RouteGuide` server in [grpc-java/examples/src/main/java/io/grpc/examples/RouteGuideServer.java](https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/routeguide/RouteGuideServer.java). Let's take a closer look at how it works.
To return our response to the client and complete the call:
187
187
188
-
1. We construct and populate a `Feature` response object to return to the client, as specified in our service definition. In this example, we do this in a separate private `getFeature()` method.
188
+
1. We construct and populate a `Feature` response object to return to the client, as specified in our service definition. In this example, we do this in a separate private `checkFeature()` method.
189
189
2. We use the response observer's `onValue()` method to return the `Feature`.
190
190
3. We use the response observer's `onCompleted()` method to specify that we've finished dealing with the RPC.
191
191
@@ -221,7 +221,7 @@ private final Collection<Feature> features;
221
221
222
222
Like the simple RPC, this method gets a request object (the `Rectangle` in which our client wants to find `Feature`s) and a `StreamObserver` response observer.
223
223
224
-
This time, we get as many `Feature` objects as we need to return to the client (in this case, we select them from the service's feature collection based on whether they're inside our request `Rectangle`), and write them each in turn to the response observer using its `Write()` method. Finally, as in our simple RPC, we use the response observer's `onCompleted()` method to tell gRPC that we've finished writing responses.
224
+
This time, we get as many `Feature` objects as we need to return to the client (in this case, we select them from the service's feature collection based on whether they're inside our request `Rectangle`), and write them each in turn to the response observer using its `onValue()` method. Finally, as in our simple RPC, we use the response observer's `onCompleted()` method to tell gRPC that we've finished writing responses.
225
225
226
226
#### Client-side streaming RPC
227
227
Now let's look at something a little more complicated: the client-side streaming method `RecordRoute`, where we get a stream of `Point`s from the client and return a single `RouteSummary` with information about their trip.
@@ -239,7 +239,7 @@ Now let's look at something a little more complicated: the client-side streaming
239
239
@Override
240
240
publicvoidonValue(Pointpoint) {
241
241
pointCount++;
242
-
if (RouteGuideUtil.exists(getFeature(point))) {
242
+
if (RouteGuideUtil.exists(checkFeature(point))) {
243
243
featureCount++;
244
244
}
245
245
// For each point after the first, add the incremental distance from the previous point
@@ -270,6 +270,7 @@ Now let's look at something a little more complicated: the client-side streaming
270
270
As you can see, like the previous method types our method gets a `StreamObserver` response observer parameter, but this time it returns a `StreamObserver` for the client to write its `Point`s.
271
271
272
272
In the method body we instantiate an anonymous `StreamObserver` to return, in which we:
273
+
273
274
- Override the `onValue()` method to get features and other information each time the client writes a `Point` to the message stream.
274
275
- Override the `onCompleted()` method (called when the *client* has finished writing messages) to populate and build our `RouteSummary`. We then call our method's own response observer's `onValue()` with our `RouteSummary`, and then call its `onCompleted()` method to finish the call from the server side.
275
276
@@ -333,11 +334,12 @@ To do this, we:
333
334
<aname="client"></a>
334
335
## Creating the client
335
336
336
-
In this section, we'll look at creating a Java client for our `RouteGuide` service. You can see our complete example client code in [grpc-java/examples/src/main/java/io/grpc/examples/RouteGuideClient.java](https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/RouteGuideClient.java).
337
+
In this section, we'll look at creating a Java client for our `RouteGuide` service. You can see our complete example client code in [grpc-java/examples/src/main/java/io/grpc/examples/RouteGuideClient.java](https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/routeguide/RouteGuideClient.java).
337
338
338
339
### Creating a stub
339
340
340
-
To call service methods, we first need to create a *stub*, or rather, two stubs:
341
+
To call service methods, we first need to create a *stub*, or rather, two stubs:
342
+
341
343
- a *blocking/synchronous* stub: this means that the RPC call waits for the server to respond, and will either return a response or raise an exception.
342
344
- a *non-blocking/asynchronous* stub that makes non-blocking calls to the server, where the response is returned asynchronously. You can make certain types of streaming call only using the asynchronous stub.
343
345
@@ -445,6 +447,7 @@ Now for something a little more complicated: the client-side streaming method `R
445
447
```
446
448
447
449
As you can see, to call this method we need to create a `StreamObserver`, which implements a special interface for the server to call with its `RouteSummary` response. In our `StreamObserver` we:
450
+
448
451
- Override the `onValue()` method to print out the returned information when the server writes a `RouteSummary` to the message stream.
449
452
- Override the `onCompleted()` method (called when the *server* has completed the call on its side) to set a `SettableFuture` that we can check to see if the server has finished writing.
0 commit comments