Created
October 29, 2021 21:23
-
-
Save NiteshKant/c5fade376afb85c2a92c13712f2e05b9 to your computer and use it in GitHub Desktop.
RoutGuide client with retry and endless stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright © 2019 Apple Inc. and the ServiceTalk project authors | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package io.servicetalk.examples.grpc.routeguide.async.streaming; | |
import io.servicetalk.concurrent.api.TerminalSignalConsumer; | |
import io.servicetalk.grpc.netty.GrpcClients; | |
import io.grpc.examples.routeguide.Point; | |
import io.grpc.examples.routeguide.RouteGuide.ClientFactory; | |
import io.grpc.examples.routeguide.RouteGuide.RouteGuideClient; | |
import io.grpc.examples.routeguide.RouteNote; | |
import java.time.Duration; | |
import static io.servicetalk.concurrent.api.Publisher.from; | |
import static java.time.Duration.ofSeconds; | |
public final class RouteGuideStreamingClient { | |
public static void main(String[] args) throws Exception { | |
try (RouteGuideClient client = GrpcClients.forAddress("localhost", 8081).build(new ClientFactory())) { | |
client.routeChat(from(RouteNote.newBuilder() | |
.setLocation(Point.newBuilder().setLatitude(123456).setLongitude(-123456).build()) | |
.setMessage("First note.").build(), | |
RouteNote.newBuilder() | |
.setLocation(Point.newBuilder().setLatitude(123456).setLongitude(-123456).build()) | |
.setMessage("Querying notes.").build()) | |
.repeatWhen(__ -> client.executionContext().executor().timer(ofSeconds(1))) | |
.whenOnNext(routeNote -> System.out.println("Sending note => " + routeNote)) | |
.beforeOnSubscribe(__ -> System.out.println("Subscribed")) | |
.beforeRequest(value -> System.out.println("Requesting .. " + value)) | |
.whenFinally(new TerminalSignalConsumer() { | |
@Override | |
public void onComplete() { | |
System.out.println("RouteGuideStreamingClient.onComplete"); | |
} | |
@Override | |
public void onError(final Throwable throwable) { | |
System.out.println("RouteGuideStreamingClient.onError"); | |
} | |
@Override | |
public void cancel() { | |
System.out.println("RouteGuideStreamingClient.cancel"); | |
} | |
})) | |
.retry((i, throwable) -> { | |
System.out.println("Retrying ... "); | |
return true; | |
}) | |
.whenOnNext(System.out::println) | |
// This example is demonstrating asynchronous execution, but needs to prevent the main thread from exiting | |
// before the response has been processed. This isn't typical usage for an asynchronous API but is useful | |
// for demonstration purposes. | |
.toFuture().get(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment