diff options
author | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2019-03-01 15:39:30 +0530 |
---|---|---|
committer | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2019-03-01 16:38:50 +0530 |
commit | 9cc868751e207dfcf966c4ba9ed4b50e7e996a07 (patch) | |
tree | ed3b5dea6b93a5193ca4eeb058883d617784e556 /grpc/grpc-client/src/main/java | |
parent | 3b7ac0faaec43dd4785ec78a3e0b27949eb77dbf (diff) |
Add timeout for rpc
Issue-ID: CLI-129
Change-Id: I99c32356f3fc6fa13f4f6ccfaac3aef059b3f341
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Diffstat (limited to 'grpc/grpc-client/src/main/java')
-rw-r--r-- | grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java index 2d9d32fc..9a818d7a 100644 --- a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java +++ b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java @@ -37,6 +37,25 @@ public class OpenInterfaceGrpcClient { private final ManagedChannel channel; private final OpenInterfaceGrpc.OpenInterfaceBlockingStub blockingStub; + public static class OpenInterfaceGrpcExecption extends Exception { + private static final long serialVersionUID = -8755636432217894246L; + + private int errorCode = -1; + + public OpenInterfaceGrpcExecption(int errorCode, String message) { + super(message); + this.errorCode = errorCode; + } + } + + public static class OpenInterfaceGrpcTimeoutExecption extends OpenInterfaceGrpcExecption { + private static int errorCode = 1; + + public OpenInterfaceGrpcTimeoutExecption(String message) { + super(errorCode, message); + } + } + public OpenInterfaceGrpcClient(String host, int port) { this(ManagedChannelBuilder.forAddress(host, port) // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid @@ -54,27 +73,31 @@ public class OpenInterfaceGrpcClient { channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); } - public Output invoke(Input input) { + public Output invoke(Input input) throws OpenInterfaceGrpcTimeoutExecption { logger.info("Input " + input.toString()); Output result = Output.newBuilder().build(); try { - result = blockingStub.invoke(input); + result = blockingStub.withDeadlineAfter(10, TimeUnit.SECONDS).invoke(input); } catch (StatusRuntimeException e) { logger.warn("RPC failed: {0}", e.getStatus()); + //Status{code=DEADLINE_EXCEEDED} + throw new OpenInterfaceGrpcTimeoutExecption(e.getMessage()); } logger.info("Output: " + result.toString()); return result; } - public Result remoteCli(Args args) { + public Result remoteCli(Args args) throws OpenInterfaceGrpcTimeoutExecption { logger.info(args.toString()); Result result = Result.newBuilder().setExitCode(1).build(); try { - result = blockingStub.remoteCli(args); + result = blockingStub.withDeadlineAfter(10, TimeUnit.SECONDS).remoteCli(args); } catch (StatusRuntimeException e) { logger.warn("RPC failed: {0}", e.getStatus()); + //Status{code=DEADLINE_EXCEEDED} + throw new OpenInterfaceGrpcTimeoutExecption(e.getMessage()); } logger.info("Result: " + result.toString()); |