1 /* 2 * Copyright 2016 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc.examples.advanced; 18 19 import static io.grpc.stub.ClientCalls.blockingUnaryCall; 20 21 import io.grpc.CallOptions; 22 import io.grpc.Channel; 23 import io.grpc.Grpc; 24 import io.grpc.InsecureChannelCredentials; 25 import io.grpc.ManagedChannel; 26 import io.grpc.MethodDescriptor; 27 import io.grpc.StatusRuntimeException; 28 import io.grpc.examples.helloworld.GreeterGrpc; 29 import io.grpc.examples.helloworld.HelloReply; 30 import io.grpc.examples.helloworld.HelloRequest; 31 import io.grpc.stub.AbstractStub; 32 import java.util.concurrent.TimeUnit; 33 import java.util.logging.Level; 34 import java.util.logging.Logger; 35 36 /** 37 * Advanced example of how to swap out the serialization logic. Normal users do not need to do 38 * this. This code is not intended to be a production-ready implementation, since JSON encoding 39 * is slow. Additionally, JSON serialization as implemented may be not resilient to malicious 40 * input. 41 * 42 * <p>If you are considering implementing your own serialization logic, contact the grpc team at 43 * https://groups.google.com/forum/#!forum/grpc-io 44 */ 45 public final class HelloJsonClient { 46 private static final Logger logger = Logger.getLogger(HelloJsonClient.class.getName()); 47 48 private final ManagedChannel channel; 49 private final HelloJsonStub blockingStub; 50 51 /** Construct client connecting to HelloWorld server at {@code host:port}. */ HelloJsonClient(String host, int port)52 public HelloJsonClient(String host, int port) { 53 channel = Grpc.newChannelBuilderForAddress(host, port, InsecureChannelCredentials.create()) 54 .build(); 55 blockingStub = new HelloJsonStub(channel); 56 } 57 shutdown()58 public void shutdown() throws InterruptedException { 59 channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); 60 } 61 62 /** Say hello to server. */ greet(String name)63 public void greet(String name) { 64 logger.info("Will try to greet " + name + " ..."); 65 HelloRequest request = HelloRequest.newBuilder().setName(name).build(); 66 HelloReply response; 67 try { 68 response = blockingStub.sayHello(request); 69 } catch (StatusRuntimeException e) { 70 logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); 71 return; 72 } 73 logger.info("Greeting: " + response.getMessage()); 74 } 75 76 /** 77 * Greet server. If provided, the first element of {@code args} is the name to use in the 78 * greeting. 79 */ main(String[] args)80 public static void main(String[] args) throws Exception { 81 // Access a service running on the local machine on port 50051 82 HelloJsonClient client = new HelloJsonClient("localhost", 50051); 83 try { 84 String user = "world"; 85 // Use the arg as the name to greet if provided 86 if (args.length > 0) { 87 user = args[0]; 88 } 89 client.greet(user); 90 } finally { 91 client.shutdown(); 92 } 93 } 94 95 static final class HelloJsonStub extends AbstractStub<HelloJsonStub> { 96 97 static final MethodDescriptor<HelloRequest, HelloReply> METHOD_SAY_HELLO = 98 GreeterGrpc.getSayHelloMethod() 99 .toBuilder( 100 JsonMarshaller.jsonMarshaller(HelloRequest.getDefaultInstance()), 101 JsonMarshaller.jsonMarshaller(HelloReply.getDefaultInstance())) 102 .build(); 103 HelloJsonStub(Channel channel)104 protected HelloJsonStub(Channel channel) { 105 super(channel); 106 } 107 HelloJsonStub(Channel channel, CallOptions callOptions)108 protected HelloJsonStub(Channel channel, CallOptions callOptions) { 109 super(channel, callOptions); 110 } 111 112 @Override build(Channel channel, CallOptions callOptions)113 protected HelloJsonStub build(Channel channel, CallOptions callOptions) { 114 return new HelloJsonStub(channel, callOptions); 115 } 116 sayHello(HelloRequest request)117 public HelloReply sayHello(HelloRequest request) { 118 return blockingUnaryCall( 119 getChannel(), METHOD_SAY_HELLO, getCallOptions(), request); 120 } 121 } 122 } 123 124