grpc.io/public/docs/tutorials/basic/android/index.html

469 lines
47 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/css/style.css">
<title>
gRPC Basics - Android Java &ndash; gRPC
</title>
<link rel="apple-touch-icon" href="/favicons/apple-touch-icon.png" sizes="180x180">
<link rel="icon" type="image/png" href="/favicons/android-chrome-192x192.png" sizes="192x192" >
<link rel="icon" type="image/png" href="/favicons/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/favicons/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="/favicons/manifest.json">
<link rel="mask-icon" href="/favicons/safari-pinned-tab.svg" color="#2DA6B0">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/favicons/mstile-150x150.png">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-60127042-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-60127042-1');
</script>
</head>
<body>
<div id="landing-content">
<div class="row">
<div class="topbannersub">
<nav class="navbar navbar-expand-md navbar-dark topnav">
<a class="navbar-brand" href="https://cjyabraham.github.io/">
<img src="https://cjyabraham.github.io/img/grpc-logo.png" width="114" height="50">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="topnav, collapse navbar-collapse" id="navbarSupportedContent" style="float:right !important">
<ul class="navbar-nav ml-auto">
<li class="nav-item ">
<a class="nav-link" href="https://cjyabraham.github.io/about/">About</a>
</li>
<li class="nav-item dropdown active">
<a class="nav-link dropdown-toggle" href="https://cjyabraham.github.io/docs/" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Docs
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="/docs">
Overview
</a>
<a class="dropdown-item" href="/docs/quickstart/">
Quick Start
</a>
<a class="dropdown-item" href="/docs/guides/">
Guides
</a>
<a class="dropdown-item" href="/docs/tutorials/">
Tutorials
</a>
<a class="dropdown-item" href="/docs/reference/">
Reference
</a>
<a class="dropdown-item" href="/docs/samples/">
Samples
</a>
<a class="dropdown-item" href="/docs/talks">
Presentations
</a>
</div>
</li>
<li class="nav-item ">
<a class="nav-link" href="/blog">
Blog
</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="/community">Community</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://packages.grpc.io/">
Packages
</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="https://cjyabraham.github.io/faq/">FAQ</a>
</li>
</ul>
</div>
</nav>
<div class="headertext">Documentation</div>
</div>
</div>
</div>
<div class="subnav d-none d-md-block">
<a href="https://cjyabraham.github.io/docs/" >Overview</a>
| <a href="https://cjyabraham.github.io/docs/quickstart/" >Quick Start</a>
| <a href="https://cjyabraham.github.io/docs/guides/" >Guides</a>
| <a href="https://cjyabraham.github.io/docs/tutorials/" class="active">Tutorials</a>
| <a href="https://cjyabraham.github.io/docs/reference/" >Reference</a>
| <a href="https://cjyabraham.github.io/docs/samples/" >Samples</a>
| <a href="https://cjyabraham.github.io/docs/talks/" >Presentations</a>
</div>
<div class="quickstartcols">
<div class="quickstartcol1">
<h8>Tutorials</h8>
<a href="https://cjyabraham.github.io/docs/tutorials/async/helloasync-cpp/" >Async - C++</a>
<a href="https://cjyabraham.github.io/docs/tutorials/auth/oauth2-objective-c/" >Auth - Objective C</a>
<h8>Basic</h8>
<a href="https://cjyabraham.github.io/docs/tutorials/basic/c/" >C++</a>
<a href="https://cjyabraham.github.io/docs/tutorials/basic/csharp/" >C#</a>
<a href="https://cjyabraham.github.io/docs/tutorials/basic/dart/" >Dart</a>
<a href="https://cjyabraham.github.io/docs/tutorials/basic/go/" >Go</a>
<a href="https://cjyabraham.github.io/docs/tutorials/basic/java/" >Java</a>
<a href="https://cjyabraham.github.io/docs/tutorials/basic/android/" class="active">Android Java</a>
<a href="https://cjyabraham.github.io/docs/tutorials/basic/node/" >Node</a>
<a href="https://cjyabraham.github.io/docs/tutorials/basic/objective-c/" >Objective-C</a>
<a href="https://cjyabraham.github.io/docs/tutorials/basic/php/" >PHP</a>
<a href="https://cjyabraham.github.io/docs/tutorials/basic/python/" >Python</a>
<a href="https://cjyabraham.github.io/docs/tutorials/basic/ruby/" >Ruby</a>
<a href="https://cjyabraham.github.io/docs/tutorials/basic/web/" >Web</a>
</div>
<div class="quickstartcol2" style="margin-top:4%">
<h3 style="margin-top:0px;">gRPC Basics - Android Java</h3>
<p>This tutorial provides a basic Android Java programmer&rsquo;s introduction to working with gRPC.</p>
<p>By walking through this example you&rsquo;ll learn how to:</p>
<ul>
<li>Define a service in a .proto file.</li>
<li>Generate client code using the protocol buffer compiler.</li>
<li>Use the Java gRPC API to write a simple mobile client for your service.</li>
</ul>
<p>It assumes that you have read the <a href="/docs/">Overview</a> and are familiar with <a href="https://developers.google.com/protocol-buffers/docs/overview">protocol buffers</a>.
This guide also does not cover anything on the server side. You can check the <a href="/docs/tutorials/basic/java/">Java guide</a> for more information.</p>
<div id="toc"></div>
<h3 id="why-use-grpc">Why use gRPC?</h3>
<p>Our example is a simple route mapping application that lets clients get information about features on their route, create a summary of their route, and exchange route information such as traffic updates with the server and other clients.</p>
<p>With gRPC we can define our service once in a .proto file and implement clients and servers in any of gRPC&rsquo;s supported languages, which in turn can be run in environments ranging from servers inside Google to your own tablet - all the complexity of communication between different languages and environments is handled for you by gRPC. We also get all the advantages of working with protocol buffers, including efficient serialization, a simple IDL, and easy interface updating.</p>
<h3 id="example-code-and-setup">Example code and setup</h3>
<p>The example code for our tutorial is in <a href="https://github.com/grpc/grpc-java/tree/v1.20.0/examples/android">grpc-java&rsquo;s examples/android</a>. To download the example, clone the <code>grpc-java</code> repository by running the following command:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ git clone -b v1.20.0 https://github.com/grpc/grpc-java.git</code></pre></div>
<p>Then change your current directory to <code>grpc-java/examples/android</code>:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ cd grpc-java/examples/android</code></pre></div>
<p>You also should have the relevant tools installed to generate the client interface code - if you don&rsquo;t already, follow the setup instructions in <a href="https://github.com/grpc/grpc-java/blob/v1.20.0/README.md">the Java README</a>.</p>
<h3 id="defining-the-service">Defining the service</h3>
<p>Our first step (as you&rsquo;ll know from the <a href="/docs/">Overview</a>) is to define the gRPC <em>service</em> and the method <em>request</em> and <em>response</em> types using <a href="https://developers.google.com/protocol-buffers/docs/overview">protocol buffers</a>. You can see the complete .proto file in <a href="https://github.com/grpc/grpc-java/blob/v1.20.0/examples/android/routeguide/app/src/main/proto/route_guide.proto"><code>routeguide/app/src/main/proto/route_guide.proto</code></a>.</p>
<p>As we&rsquo;re generating Java code in this example, we&rsquo;ve specified a <code>java_package</code> file option in our .proto:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-proto" data-lang="proto"><span style="color:#66d9ef">option</span> java_package <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;io.grpc.examples&#34;</span>;</code></pre></div>
<p>This specifies the package we want to use for our generated Java classes. If no explicit <code>java_package</code> option is given in the .proto file, then by default the proto package (specified using the &ldquo;package&rdquo; keyword) will be used. However, proto packages generally do not make good Java packages since proto packages are not expected to start with reverse domain names. If we generate code in another language from this .proto, the <code>java_package</code> option has no effect.</p>
<p>To define a service, we specify a named <code>service</code> in the .proto file:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-proto" data-lang="proto"><span style="color:#66d9ef">service</span> RouteGuide {<span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span> <span style="color:#f92672">...</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span>}</code></pre></div>
<p>Then we define <code>rpc</code> methods inside our service definition, specifying their request and response types. gRPC lets you define four kinds of service method, all of which are used in the <code>RouteGuide</code> service:</p>
<ul>
<li>A <em>simple RPC</em> where the client sends a request to the server using the stub and waits for a response to come back, just like a normal function call.</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-proto" data-lang="proto"><span style="color:#75715e">// Obtains the feature at a given position.
</span><span style="color:#75715e"></span><span style="color:#66d9ef">rpc</span> GetFeature(Point) <span style="color:#66d9ef">returns</span> (Feature) {}</code></pre></div>
<ul>
<li>A <em>server-side streaming RPC</em> where the client sends a request to the server and gets a stream to read a sequence of messages back. The client reads from the returned stream until there are no more messages. As you can see in our example, you specify a server-side streaming method by placing the <code>stream</code> keyword before the <em>response</em> type.</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-proto" data-lang="proto"><span style="color:#75715e">// Obtains the Features available within the given Rectangle. Results are
</span><span style="color:#75715e">// streamed rather than returned at once (e.g. in a response message with a
</span><span style="color:#75715e">// repeated field), as the rectangle may cover a large area and contain a
</span><span style="color:#75715e">// huge number of features.
</span><span style="color:#75715e"></span><span style="color:#66d9ef">rpc</span> ListFeatures(Rectangle) <span style="color:#66d9ef">returns</span> (stream Feature) {}</code></pre></div>
<ul>
<li>A <em>client-side streaming RPC</em> where the client writes a sequence of messages and sends them to the server, again using a provided stream. Once the client has finished writing the messages, it waits for the server to read them all and return its response. You specify a client-side streaming method by placing the <code>stream</code> keyword before the <em>request</em> type.</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-proto" data-lang="proto"><span style="color:#75715e">// Accepts a stream of Points on a route being traversed, returning a
</span><span style="color:#75715e">// RouteSummary when traversal is completed.
</span><span style="color:#75715e"></span><span style="color:#66d9ef">rpc</span> RecordRoute(stream Point) <span style="color:#66d9ef">returns</span> (RouteSummary) {}</code></pre></div>
<ul>
<li>A <em>bidirectional streaming RPC</em> where both sides send a sequence of messages using a read-write stream. The two streams operate independently, so clients and servers can read and write in whatever order they like: for example, the server could wait to receive all the client messages before writing its responses, or it could alternately read a message then write a message, or some other combination of reads and writes. The order of messages in each stream is preserved. You specify this type of method by placing the <code>stream</code> keyword before both the request and the response.</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-proto" data-lang="proto"><span style="color:#75715e">// Accepts a stream of RouteNotes sent while a route is being traversed,
</span><span style="color:#75715e">// while receiving other RouteNotes (e.g. from other users).
</span><span style="color:#75715e"></span><span style="color:#66d9ef">rpc</span> RouteChat(stream RouteNote) <span style="color:#66d9ef">returns</span> (stream RouteNote) {}</code></pre></div>
<p>Our .proto file also contains protocol buffer message type definitions for all the request and response types used in our service methods - for example, here&rsquo;s the <code>Point</code> message type:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-proto" data-lang="proto"><span style="color:#75715e">// Points are represented as latitude-longitude pairs in the E7 representation
</span><span style="color:#75715e">// (degrees multiplied by 10**7 and rounded to the nearest integer).
</span><span style="color:#75715e">// Latitudes should be in the range +/- 90 degrees and longitude should be in
</span><span style="color:#75715e">// the range +/- 180 degrees (inclusive).
</span><span style="color:#75715e"></span><span style="color:#66d9ef">message</span> <span style="color:#a6e22e">Point</span> {<span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span> <span style="color:#66d9ef">int32</span> latitude <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>;<span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span> <span style="color:#66d9ef">int32</span> longitude <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span>;<span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span>}</code></pre></div>
<h3 id="generating-client-code">Generating client code</h3>
<p>Next we need to generate the gRPC client interfaces from our .proto
service definition. We do this using the protocol buffer compiler <code>protoc</code> with
a special gRPC Java plugin. You need to use the
<a href="https://github.com/google/protobuf/releases">proto3</a> compiler (which supports
both proto2 and proto3 syntax) in order to generate gRPC services.</p>
<p>The build system for this example is also part of Java gRPC itself&rsquo;s build. You
can refer to the <a
href="https://github.com/grpc/grpc-java/blob/v1.20.0/README.md">README</a> and
<a href="https://github.com/grpc/grpc-java/blob/v1.20.0/examples/android/routeguide/app/build.gradle#L26">build.gradle</a> for
how to generate code from your own .proto files.
Note that for Android, we will use protobuf lite which is optimized for mobile usecase.</p>
<p>The following classes are generated from our service definition:</p>
<ul>
<li><code>Feature.java</code>, <code>Point.java</code>, <code>Rectangle.java</code>, and others which contain
all the protocol buffer code to populate, serialize, and retrieve our request
and response message types.</li>
<li><code>RouteGuideGrpc.java</code> which contains (along with some other useful code):
<ul>
<li>a base class for <code>RouteGuide</code> servers to implement,
<code>RouteGuideGrpc.RouteGuideImplBase</code>, with all the methods defined in the <code>RouteGuide</code>
service.</li>
<li><em>stub</em> classes that clients can use to talk to a <code>RouteGuide</code> server.</li>
</ul></li>
</ul>
<h3 id="creating-the-client">Creating the client</h3>
<p>In this section, we&rsquo;ll look at creating a Java client for our <code>RouteGuide</code> service. You can see our complete example client code in <a href="https://github.com/grpc/grpc-java/blob/v1.20.0/examples/android/routeguide/app/src/main/java/io/grpc/routeguideexample/RouteGuideActivity.java"><code>routeguide/app/src/main/java/io/grpc/routeguideexample/RouteGuideActivity.java</code></a>.</p>
<h4 id="creating-a-stub">Creating a stub</h4>
<p>To call service methods, we first need to create a <em>stub</em>, or rather, two stubs:</p>
<ul>
<li>a <em>blocking/synchronous</em> stub: this means that the RPC call waits for the server to respond, and will either return a response or raise an exception.</li>
<li>a <em>non-blocking/asynchronous</em> 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.</li>
</ul>
<p>First we need to create a gRPC <em>channel</em> for our stub, specifying the server address and port we want to connect to:
We use a <code>ManagedChannelBuilder</code> to create the channel.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">mChannel <span style="color:#f92672">=</span> ManagedChannelBuilder<span style="color:#f92672">.</span><span style="color:#a6e22e">forAddress</span><span style="color:#f92672">(</span>host<span style="color:#f92672">,</span> port<span style="color:#f92672">).</span><span style="color:#a6e22e">usePlaintext</span><span style="color:#f92672">(</span><span style="color:#66d9ef">true</span><span style="color:#f92672">).</span><span style="color:#a6e22e">build</span><span style="color:#f92672">();</span></code></pre></div>
<p>Now we can use the channel to create our stubs using the <code>newStub</code> and <code>newBlockingStub</code> methods provided in the <code>RouteGuideGrpc</code> class we generated from our .proto.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">blockingStub <span style="color:#f92672">=</span> RouteGuideGrpc<span style="color:#f92672">.</span><span style="color:#a6e22e">newBlockingStub</span><span style="color:#f92672">(</span>mChannel<span style="color:#f92672">);</span>
asyncStub <span style="color:#f92672">=</span> RouteGuideGrpc<span style="color:#f92672">.</span><span style="color:#a6e22e">newStub</span><span style="color:#f92672">(</span>mChannel<span style="color:#f92672">);</span></code></pre></div>
<h4 id="calling-service-methods">Calling service methods</h4>
<p>Now let&rsquo;s look at how we call our service methods.</p>
<h5 id="simple-rpc">Simple RPC</h5>
<p>Calling the simple RPC <code>GetFeature</code> on the blocking stub is as straightforward as calling a local method.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">Point request <span style="color:#f92672">=</span> Point<span style="color:#f92672">.</span><span style="color:#a6e22e">newBuilder</span><span style="color:#f92672">().</span><span style="color:#a6e22e">setLatitude</span><span style="color:#f92672">(</span>lat<span style="color:#f92672">).</span><span style="color:#a6e22e">setLongitude</span><span style="color:#f92672">(</span>lon<span style="color:#f92672">).</span><span style="color:#a6e22e">build</span><span style="color:#f92672">();</span>
Feature feature <span style="color:#f92672">=</span> blockingStub<span style="color:#f92672">.</span><span style="color:#a6e22e">getFeature</span><span style="color:#f92672">(</span>request<span style="color:#f92672">);</span></code></pre></div>
<p>We create and populate a request protocol buffer object (in our case <code>Point</code>), pass it to the <code>getFeature()</code> method on our blocking stub, and get back a <code>Feature</code>.</p>
<h5 id="server-side-streaming-rpc">Server-side streaming RPC</h5>
<p>Next, let&rsquo;s look at a server-side streaming call to <code>ListFeatures</code>, which returns a stream of geographical <code>Feature</code>s:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">Rectangle request <span style="color:#f92672">=</span>
Rectangle<span style="color:#f92672">.</span><span style="color:#a6e22e">newBuilder</span><span style="color:#f92672">()</span>
<span style="color:#f92672">.</span><span style="color:#a6e22e">setLo</span><span style="color:#f92672">(</span>Point<span style="color:#f92672">.</span><span style="color:#a6e22e">newBuilder</span><span style="color:#f92672">().</span><span style="color:#a6e22e">setLatitude</span><span style="color:#f92672">(</span>lowLat<span style="color:#f92672">).</span><span style="color:#a6e22e">setLongitude</span><span style="color:#f92672">(</span>lowLon<span style="color:#f92672">).</span><span style="color:#a6e22e">build</span><span style="color:#f92672">())</span>
<span style="color:#f92672">.</span><span style="color:#a6e22e">setHi</span><span style="color:#f92672">(</span>Point<span style="color:#f92672">.</span><span style="color:#a6e22e">newBuilder</span><span style="color:#f92672">().</span><span style="color:#a6e22e">setLatitude</span><span style="color:#f92672">(</span>hiLat<span style="color:#f92672">).</span><span style="color:#a6e22e">setLongitude</span><span style="color:#f92672">(</span>hiLon<span style="color:#f92672">).</span><span style="color:#a6e22e">build</span><span style="color:#f92672">()).</span><span style="color:#a6e22e">build</span><span style="color:#f92672">();</span>
Iterator<span style="color:#f92672">&lt;</span>Feature<span style="color:#f92672">&gt;</span> features <span style="color:#f92672">=</span> blockingStub<span style="color:#f92672">.</span><span style="color:#a6e22e">listFeatures</span><span style="color:#f92672">(</span>request<span style="color:#f92672">);</span></code></pre></div>
<p>As you can see, it&rsquo;s very similar to the simple RPC we just looked at, except instead of returning a single <code>Feature</code>, the method returns an <code>Iterator</code> that the client can use to read all the returned <code>Feature</code>s.</p>
<h5 id="client-side-streaming-rpc">Client-side streaming RPC</h5>
<p>Now for something a little more complicated: the client-side streaming method <code>RecordRoute</code>, where we send a stream of <code>Point</code>s to the server and get back a single <code>RouteSummary</code>. For this method we need to use the asynchronous stub. If you&rsquo;ve already read <a href="/docs/tutorials/basic/java/#creating-the-server">Creating the server</a> some of this may look very familiar - asynchronous streaming RPCs are implemented in a similar way on both sides.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#66d9ef">private</span> String <span style="color:#a6e22e">recordRoute</span><span style="color:#f92672">(</span>List<span style="color:#f92672">&lt;</span>Point<span style="color:#f92672">&gt;</span> points<span style="color:#f92672">,</span> <span style="color:#66d9ef">int</span> numPoints<span style="color:#f92672">,</span> RouteGuideStub asyncStub<span style="color:#f92672">)</span>
<span style="color:#66d9ef">throws</span> InterruptedException<span style="color:#f92672">,</span> RuntimeException <span style="color:#f92672">{</span>
<span style="color:#66d9ef">final</span> StringBuffer logs <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> StringBuffer<span style="color:#f92672">();</span>
appendLogs<span style="color:#f92672">(</span>logs<span style="color:#f92672">,</span> <span style="color:#e6db74">&#34;*** RecordRoute&#34;</span><span style="color:#f92672">);</span>
<span style="color:#66d9ef">final</span> CountDownLatch finishLatch <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> CountDownLatch<span style="color:#f92672">(</span>1<span style="color:#f92672">);</span>
StreamObserver<span style="color:#f92672">&lt;</span>RouteSummary<span style="color:#f92672">&gt;</span> responseObserver <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> StreamObserver<span style="color:#f92672">&lt;</span>RouteSummary<span style="color:#f92672">&gt;()</span> <span style="color:#f92672">{</span>
<span style="color:#a6e22e">@Override</span>
<span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">onNext</span><span style="color:#f92672">(</span>RouteSummary summary<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
appendLogs<span style="color:#f92672">(</span>logs<span style="color:#f92672">,</span> <span style="color:#e6db74">&#34;Finished trip with {0} points. Passed {1} features. &#34;</span>
<span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;Travelled {2} meters. It took {3} seconds.&#34;</span><span style="color:#f92672">,</span> summary<span style="color:#f92672">.</span><span style="color:#a6e22e">getPointCount</span><span style="color:#f92672">(),</span>
summary<span style="color:#f92672">.</span><span style="color:#a6e22e">getFeatureCount</span><span style="color:#f92672">(),</span> summary<span style="color:#f92672">.</span><span style="color:#a6e22e">getDistance</span><span style="color:#f92672">(),</span>
summary<span style="color:#f92672">.</span><span style="color:#a6e22e">getElapsedTime</span><span style="color:#f92672">());</span>
<span style="color:#f92672">}</span>
<span style="color:#a6e22e">@Override</span>
<span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">onError</span><span style="color:#f92672">(</span>Throwable t<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
failed <span style="color:#f92672">=</span> t<span style="color:#f92672">;</span>
finishLatch<span style="color:#f92672">.</span><span style="color:#a6e22e">countDown</span><span style="color:#f92672">();</span>
<span style="color:#f92672">}</span>
<span style="color:#a6e22e">@Override</span>
<span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">onCompleted</span><span style="color:#f92672">()</span> <span style="color:#f92672">{</span>
appendLogs<span style="color:#f92672">(</span>logs<span style="color:#f92672">,</span> <span style="color:#e6db74">&#34;Finished RecordRoute&#34;</span><span style="color:#f92672">);</span>
finishLatch<span style="color:#f92672">.</span><span style="color:#a6e22e">countDown</span><span style="color:#f92672">();</span>
<span style="color:#f92672">}</span>
<span style="color:#f92672">};</span>
StreamObserver<span style="color:#f92672">&lt;</span>Point<span style="color:#f92672">&gt;</span> requestObserver <span style="color:#f92672">=</span> asyncStub<span style="color:#f92672">.</span><span style="color:#a6e22e">recordRoute</span><span style="color:#f92672">(</span>responseObserver<span style="color:#f92672">);</span>
<span style="color:#66d9ef">try</span> <span style="color:#f92672">{</span>
<span style="color:#75715e">// Send numPoints points randomly selected from the points list.
</span><span style="color:#75715e"></span> Random rand <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Random<span style="color:#f92672">();</span>
<span style="color:#66d9ef">for</span> <span style="color:#f92672">(</span><span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> 0<span style="color:#f92672">;</span> i <span style="color:#f92672">&lt;</span> numPoints<span style="color:#f92672">;</span> <span style="color:#f92672">++</span>i<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
<span style="color:#66d9ef">int</span> index <span style="color:#f92672">=</span> rand<span style="color:#f92672">.</span><span style="color:#a6e22e">nextInt</span><span style="color:#f92672">(</span>points<span style="color:#f92672">.</span><span style="color:#a6e22e">size</span><span style="color:#f92672">());</span>
Point point <span style="color:#f92672">=</span> points<span style="color:#f92672">.</span><span style="color:#a6e22e">get</span><span style="color:#f92672">(</span>index<span style="color:#f92672">);</span>
appendLogs<span style="color:#f92672">(</span>logs<span style="color:#f92672">,</span> <span style="color:#e6db74">&#34;Visiting point {0}, {1}&#34;</span><span style="color:#f92672">,</span> RouteGuideUtil<span style="color:#f92672">.</span><span style="color:#a6e22e">getLatitude</span><span style="color:#f92672">(</span>point<span style="color:#f92672">),</span>
RouteGuideUtil<span style="color:#f92672">.</span><span style="color:#a6e22e">getLongitude</span><span style="color:#f92672">(</span>point<span style="color:#f92672">));</span>
requestObserver<span style="color:#f92672">.</span><span style="color:#a6e22e">onNext</span><span style="color:#f92672">(</span>point<span style="color:#f92672">);</span>
<span style="color:#75715e">// Sleep for a bit before sending the next one.
</span><span style="color:#75715e"></span> Thread<span style="color:#f92672">.</span><span style="color:#a6e22e">sleep</span><span style="color:#f92672">(</span>rand<span style="color:#f92672">.</span><span style="color:#a6e22e">nextInt</span><span style="color:#f92672">(</span>1000<span style="color:#f92672">)</span> <span style="color:#f92672">+</span> 500<span style="color:#f92672">);</span>
<span style="color:#66d9ef">if</span> <span style="color:#f92672">(</span>finishLatch<span style="color:#f92672">.</span><span style="color:#a6e22e">getCount</span><span style="color:#f92672">()</span> <span style="color:#f92672">==</span> 0<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
<span style="color:#75715e">// RPC completed or errored before we finished sending.
</span><span style="color:#75715e"></span> <span style="color:#75715e">// Sending further requests won&#39;t error, but they will just be thrown away.
</span><span style="color:#75715e"></span> <span style="color:#66d9ef">break</span><span style="color:#f92672">;</span>
<span style="color:#f92672">}</span>
<span style="color:#f92672">}</span>
<span style="color:#f92672">}</span> <span style="color:#66d9ef">catch</span> <span style="color:#f92672">(</span>RuntimeException e<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
<span style="color:#75715e">// Cancel RPC
</span><span style="color:#75715e"></span> requestObserver<span style="color:#f92672">.</span><span style="color:#a6e22e">onError</span><span style="color:#f92672">(</span>e<span style="color:#f92672">);</span>
<span style="color:#66d9ef">throw</span> e<span style="color:#f92672">;</span>
<span style="color:#f92672">}</span>
<span style="color:#75715e">// Mark the end of requests
</span><span style="color:#75715e"></span> requestObserver<span style="color:#f92672">.</span><span style="color:#a6e22e">onCompleted</span><span style="color:#f92672">();</span>
<span style="color:#75715e">// Receiving happens asynchronously
</span><span style="color:#75715e"></span> <span style="color:#66d9ef">if</span> <span style="color:#f92672">(!</span>finishLatch<span style="color:#f92672">.</span><span style="color:#a6e22e">await</span><span style="color:#f92672">(</span>1<span style="color:#f92672">,</span> TimeUnit<span style="color:#f92672">.</span><span style="color:#a6e22e">MINUTES</span><span style="color:#f92672">))</span> <span style="color:#f92672">{</span>
<span style="color:#66d9ef">throw</span> <span style="color:#66d9ef">new</span> RuntimeException<span style="color:#f92672">(</span>
<span style="color:#e6db74">&#34;Could not finish rpc within 1 minute, the server is likely down&#34;</span><span style="color:#f92672">);</span>
<span style="color:#f92672">}</span>
<span style="color:#66d9ef">if</span> <span style="color:#f92672">(</span>failed <span style="color:#f92672">!=</span> <span style="color:#66d9ef">null</span><span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
<span style="color:#66d9ef">throw</span> <span style="color:#66d9ef">new</span> RuntimeException<span style="color:#f92672">(</span>failed<span style="color:#f92672">);</span>
<span style="color:#f92672">}</span>
<span style="color:#66d9ef">return</span> logs<span style="color:#f92672">.</span><span style="color:#a6e22e">toString</span><span style="color:#f92672">();</span>
<span style="color:#f92672">}</span></code></pre></div>
<p>As you can see, to call this method we need to create a <code>StreamObserver</code>, which implements a special interface for the server to call with its <code>RouteSummary</code> response. In our <code>StreamObserver</code> we:</p>
<ul>
<li>Override the <code>onNext()</code> method to print out the returned information when the server writes a <code>RouteSummary</code> to the message stream.</li>
<li>Override the <code>onCompleted()</code> method (called when the <em>server</em> has completed the call on its side) to set a <code>SettableFuture</code> that we can check to see if the server has finished writing.</li>
</ul>
<p>We then pass the <code>StreamObserver</code> to the asynchronous stub&rsquo;s <code>recordRoute()</code> method and get back our own <code>StreamObserver</code> request observer to write our <code>Point</code>s to send to the server. Once we&rsquo;ve finished writing points, we use the request observer&rsquo;s <code>onCompleted()</code> method to tell gRPC that we&rsquo;ve finished writing on the client side. Once we&rsquo;re done, we check our <code>SettableFuture</code> to check that the server has completed on its side.</p>
<h5 id="bidirectional-streaming-rpc">Bidirectional streaming RPC</h5>
<p>Finally, let&rsquo;s look at our bidirectional streaming RPC <code>RouteChat()</code>.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#66d9ef">private</span> String <span style="color:#a6e22e">routeChat</span><span style="color:#f92672">(</span>RouteGuideStub asyncStub<span style="color:#f92672">)</span> <span style="color:#66d9ef">throws</span> InterruptedException<span style="color:#f92672">,</span>
RuntimeException <span style="color:#f92672">{</span>
<span style="color:#66d9ef">final</span> StringBuffer logs <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> StringBuffer<span style="color:#f92672">();</span>
appendLogs<span style="color:#f92672">(</span>logs<span style="color:#f92672">,</span> <span style="color:#e6db74">&#34;*** RouteChat&#34;</span><span style="color:#f92672">);</span>
<span style="color:#66d9ef">final</span> CountDownLatch finishLatch <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> CountDownLatch<span style="color:#f92672">(</span>1<span style="color:#f92672">);</span>
StreamObserver<span style="color:#f92672">&lt;</span>RouteNote<span style="color:#f92672">&gt;</span> requestObserver <span style="color:#f92672">=</span>
asyncStub<span style="color:#f92672">.</span><span style="color:#a6e22e">routeChat</span><span style="color:#f92672">(</span><span style="color:#66d9ef">new</span> StreamObserver<span style="color:#f92672">&lt;</span>RouteNote<span style="color:#f92672">&gt;()</span> <span style="color:#f92672">{</span>
<span style="color:#a6e22e">@Override</span>
<span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">onNext</span><span style="color:#f92672">(</span>RouteNote note<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
appendLogs<span style="color:#f92672">(</span>logs<span style="color:#f92672">,</span> <span style="color:#e6db74">&#34;Got message \&#34;{0}\&#34; at {1}, {2}&#34;</span><span style="color:#f92672">,</span> note<span style="color:#f92672">.</span><span style="color:#a6e22e">getMessage</span><span style="color:#f92672">(),</span>
note<span style="color:#f92672">.</span><span style="color:#a6e22e">getLocation</span><span style="color:#f92672">().</span><span style="color:#a6e22e">getLatitude</span><span style="color:#f92672">(),</span>
note<span style="color:#f92672">.</span><span style="color:#a6e22e">getLocation</span><span style="color:#f92672">().</span><span style="color:#a6e22e">getLongitude</span><span style="color:#f92672">());</span>
<span style="color:#f92672">}</span>
<span style="color:#a6e22e">@Override</span>
<span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">onError</span><span style="color:#f92672">(</span>Throwable t<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
failed <span style="color:#f92672">=</span> t<span style="color:#f92672">;</span>
finishLatch<span style="color:#f92672">.</span><span style="color:#a6e22e">countDown</span><span style="color:#f92672">();</span>
<span style="color:#f92672">}</span>
<span style="color:#a6e22e">@Override</span>
<span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">onCompleted</span><span style="color:#f92672">()</span> <span style="color:#f92672">{</span>
appendLogs<span style="color:#f92672">(</span>logs<span style="color:#f92672">,</span><span style="color:#e6db74">&#34;Finished RouteChat&#34;</span><span style="color:#f92672">);</span>
finishLatch<span style="color:#f92672">.</span><span style="color:#a6e22e">countDown</span><span style="color:#f92672">();</span>
<span style="color:#f92672">}</span>
<span style="color:#f92672">});</span>
<span style="color:#66d9ef">try</span> <span style="color:#f92672">{</span>
RouteNote<span style="color:#f92672">[]</span> requests <span style="color:#f92672">=</span>
<span style="color:#f92672">{</span>newNote<span style="color:#f92672">(</span><span style="color:#e6db74">&#34;First message&#34;</span><span style="color:#f92672">,</span> 0<span style="color:#f92672">,</span> 0<span style="color:#f92672">),</span> newNote<span style="color:#f92672">(</span><span style="color:#e6db74">&#34;Second message&#34;</span><span style="color:#f92672">,</span> 0<span style="color:#f92672">,</span> 1<span style="color:#f92672">),</span>
newNote<span style="color:#f92672">(</span><span style="color:#e6db74">&#34;Third message&#34;</span><span style="color:#f92672">,</span> 1<span style="color:#f92672">,</span> 0<span style="color:#f92672">),</span> newNote<span style="color:#f92672">(</span><span style="color:#e6db74">&#34;Fourth message&#34;</span><span style="color:#f92672">,</span> 1<span style="color:#f92672">,</span> 1<span style="color:#f92672">)};</span>
<span style="color:#66d9ef">for</span> <span style="color:#f92672">(</span>RouteNote request <span style="color:#f92672">:</span> requests<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
appendLogs<span style="color:#f92672">(</span>logs<span style="color:#f92672">,</span> <span style="color:#e6db74">&#34;Sending message \&#34;{0}\&#34; at {1}, {2}&#34;</span><span style="color:#f92672">,</span> request<span style="color:#f92672">.</span><span style="color:#a6e22e">getMessage</span><span style="color:#f92672">(),</span>
request<span style="color:#f92672">.</span><span style="color:#a6e22e">getLocation</span><span style="color:#f92672">().</span><span style="color:#a6e22e">getLatitude</span><span style="color:#f92672">(),</span>
request<span style="color:#f92672">.</span><span style="color:#a6e22e">getLocation</span><span style="color:#f92672">().</span><span style="color:#a6e22e">getLongitude</span><span style="color:#f92672">());</span>
requestObserver<span style="color:#f92672">.</span><span style="color:#a6e22e">onNext</span><span style="color:#f92672">(</span>request<span style="color:#f92672">);</span>
<span style="color:#f92672">}</span>
<span style="color:#f92672">}</span> <span style="color:#66d9ef">catch</span> <span style="color:#f92672">(</span>RuntimeException e<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
<span style="color:#75715e">// Cancel RPC
</span><span style="color:#75715e"></span> requestObserver<span style="color:#f92672">.</span><span style="color:#a6e22e">onError</span><span style="color:#f92672">(</span>e<span style="color:#f92672">);</span>
<span style="color:#66d9ef">throw</span> e<span style="color:#f92672">;</span>
<span style="color:#f92672">}</span>
<span style="color:#75715e">// Mark the end of requests
</span><span style="color:#75715e"></span> requestObserver<span style="color:#f92672">.</span><span style="color:#a6e22e">onCompleted</span><span style="color:#f92672">();</span>
<span style="color:#75715e">// Receiving happens asynchronously
</span><span style="color:#75715e"></span> <span style="color:#66d9ef">if</span> <span style="color:#f92672">(!</span>finishLatch<span style="color:#f92672">.</span><span style="color:#a6e22e">await</span><span style="color:#f92672">(</span>1<span style="color:#f92672">,</span> TimeUnit<span style="color:#f92672">.</span><span style="color:#a6e22e">MINUTES</span><span style="color:#f92672">))</span> <span style="color:#f92672">{</span>
<span style="color:#66d9ef">throw</span> <span style="color:#66d9ef">new</span> RuntimeException<span style="color:#f92672">(</span>
<span style="color:#e6db74">&#34;Could not finish rpc within 1 minute, the server is likely down&#34;</span><span style="color:#f92672">);</span>
<span style="color:#f92672">}</span>
<span style="color:#66d9ef">if</span> <span style="color:#f92672">(</span>failed <span style="color:#f92672">!=</span> <span style="color:#66d9ef">null</span><span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
<span style="color:#66d9ef">throw</span> <span style="color:#66d9ef">new</span> RuntimeException<span style="color:#f92672">(</span>failed<span style="color:#f92672">);</span>
<span style="color:#f92672">}</span>
<span style="color:#66d9ef">return</span> logs<span style="color:#f92672">.</span><span style="color:#a6e22e">toString</span><span style="color:#f92672">();</span>
<span style="color:#f92672">}</span></code></pre></div>
<p>As with our client-side streaming example, we both get and return a <code>StreamObserver</code> response observer, except this time we send values via our method&rsquo;s response observer while the server is still writing messages to <em>their</em> message stream. The syntax for reading and writing here is exactly the same as for our client-streaming method. Although each side will always get the other&rsquo;s messages in the order they were written, both the client and server can read and write in any order — the streams operate completely independently.</p>
<h3 id="try-it-out">Try it out!</h3>
<p>Follow the instructions in the example directory <a href="https://github.com/grpc/grpc-java/blob/v1.20.0/examples/android/README.md">README</a> to build and run the client and server.</p>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>