Skip to content
This repository was archived by the owner on Apr 9, 2021. It is now read-only.

Commit e4e968f

Browse files
committed
Merge pull request #50 from LisaFC/feature/docs
Small updates to example code/link fixes
2 parents 8026bdc + a962c66 commit e4e968f

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

docs/tutorials/basic/java.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ There are two parts to making our `RouteGuide` service do its job:
143143
- Implementing the service interface generated from our service definition: doing the actual "work" of our service.
144144
- Running a gRPC server to listen for requests from clients and return the service responses.
145145

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.
147147

148148
### Implementing RouteGuide
149149

@@ -160,13 +160,13 @@ private static class RouteGuideService implements RouteGuideGrpc.RouteGuide {
160160
```java
161161
@Override
162162
public void getFeature(Point request, StreamObserver<Feature> responseObserver) {
163-
responseObserver.onValue(getFeature(request));
163+
responseObserver.onValue(checkFeature(request));
164164
responseObserver.onCompleted();
165165
}
166166

167167
...
168168

169-
private Feature getFeature(Point location) {
169+
private Feature checkFeature(Point location) {
170170
for (Feature feature : features) {
171171
if (feature.getLocation().getLatitude() == location.getLatitude()
172172
&& feature.getLocation().getLongitude() == location.getLongitude()) {
@@ -185,7 +185,7 @@ private static class RouteGuideService implements RouteGuideGrpc.RouteGuide {
185185

186186
To return our response to the client and complete the call:
187187

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.
189189
2. We use the response observer's `onValue()` method to return the `Feature`.
190190
3. We use the response observer's `onCompleted()` method to specify that we've finished dealing with the RPC.
191191

@@ -221,7 +221,7 @@ private final Collection<Feature> features;
221221

222222
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.
223223

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.
225225

226226
#### Client-side streaming RPC
227227
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
239239
@Override
240240
public void onValue(Point point) {
241241
pointCount++;
242-
if (RouteGuideUtil.exists(getFeature(point))) {
242+
if (RouteGuideUtil.exists(checkFeature(point))) {
243243
featureCount++;
244244
}
245245
// 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
270270
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.
271271

272272
In the method body we instantiate an anonymous `StreamObserver` to return, in which we:
273+
273274
- Override the `onValue()` method to get features and other information each time the client writes a `Point` to the message stream.
274275
- 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.
275276

@@ -333,11 +334,12 @@ To do this, we:
333334
<a name="client"></a>
334335
## Creating the client
335336

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).
337338

338339
### Creating a stub
339340

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+
341343
- 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.
342344
- 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.
343345

@@ -445,6 +447,7 @@ Now for something a little more complicated: the client-side streaming method `R
445447
```
446448

447449
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+
448451
- Override the `onValue()` method to print out the returned information when the server writes a `RouteSummary` to the message stream.
449452
- 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.
450453

0 commit comments

Comments
 (0)