Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add LoggingErrorHandlingGreeterClient with java #289

Merged
merged 5 commits into from
Apr 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 example.myapp.helloworld;
laglangyue marked this conversation as resolved.
Show resolved Hide resolved

import example.myapp.helloworld.grpc.GreeterServiceClient;
import example.myapp.helloworld.grpc.HelloReply;
import example.myapp.helloworld.grpc.HelloRequest;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.grpc.GrpcClientSettings;

public class LoggingErrorHandlingGreeterClient {

public static void main(String[] args) {

// Boot pekko
ActorSystem sys = ActorSystem.create("LoggingErrorHandlingGreeterClient");

// Take details how to connect to the service from the config.
GrpcClientSettings clientSettings =
GrpcClientSettings.connectToServiceAt("127.0.0.1", 8082, sys).withTls(false);
// Create a client-side stub for the service
GreeterServiceClient client = GreeterServiceClient.create(clientSettings, sys);

try {
HelloRequest Martin = HelloRequest.newBuilder().setName("Martin").build();
CompletionStage<HelloReply> successful = client.sayHello(Martin);
successful.toCompletableFuture().get(10, TimeUnit.SECONDS);
sys.log().info("Call succeeded");

HelloRequest martin = HelloRequest.newBuilder().setName("martin").build();
CompletionStage<HelloReply> failedBecauseLowercase = client.sayHello(martin);
failedBecauseLowercase
.handle(
(response, exception) -> {
if (exception != null) {
sys.log().info("Call with lowercase name failed");
}
return response;
})
.toCompletableFuture()
.get(10, TimeUnit.SECONDS);

HelloRequest empty = HelloRequest.newBuilder().setName("").build();
CompletionStage<HelloReply> failedBecauseEmpty = client.sayHello(empty);
failedBecauseEmpty
.handle(
(response, exception) -> {
if (exception != null) {
sys.log().info("Call with empty name failed");
}
return response;
})
.toCompletableFuture()
.get(10, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new RuntimeException(e);
}
sys.terminate();
}
}
Loading