1 /* 2 * Copyright 2015 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.experimental; 18 19 import io.grpc.Grpc; 20 import io.grpc.InsecureChannelCredentials; 21 import io.grpc.ManagedChannel; 22 import io.grpc.StatusRuntimeException; 23 import io.grpc.examples.helloworld.GreeterGrpc; 24 import io.grpc.examples.helloworld.HelloReply; 25 import io.grpc.examples.helloworld.HelloRequest; 26 import java.util.concurrent.TimeUnit; 27 import java.util.logging.Level; 28 import java.util.logging.Logger; 29 30 /** 31 * A simple client that requests a greeting from the 32 * {@link io.grpc.examples.helloworld.HelloWorldServer}. 33 * 34 * <p>This class should act a drop in replacement for 35 * {@link io.grpc.examples.helloworld.HelloWorldClient}. 36 */ 37 public class CompressingHelloWorldClient { 38 private static final Logger logger = 39 Logger.getLogger(CompressingHelloWorldClient.class.getName()); 40 41 private final ManagedChannel channel; 42 private final GreeterGrpc.GreeterBlockingStub blockingStub; 43 44 /** Construct client connecting to HelloWorld server at {@code host:port}. */ CompressingHelloWorldClient(String host, int port)45 public CompressingHelloWorldClient(String host, int port) { 46 channel = Grpc.newChannelBuilderForAddress(host, port, InsecureChannelCredentials.create()) 47 .build(); 48 blockingStub = GreeterGrpc.newBlockingStub(channel); 49 } 50 shutdown()51 public void shutdown() throws InterruptedException { 52 channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); 53 } 54 55 /** Say hello to server. */ greet(String name)56 public void greet(String name) { 57 logger.info("Will try to greet " + name + " ..."); 58 HelloRequest request = HelloRequest.newBuilder().setName(name).build(); 59 HelloReply response; 60 try { 61 // This enables compression for requests. Independent of this setting, servers choose whether 62 // to compress responses. 63 response = blockingStub.withCompression("gzip").sayHello(request); 64 } catch (StatusRuntimeException e) { 65 logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); 66 return; 67 } 68 logger.info("Greeting: " + response.getMessage()); 69 } 70 71 /** 72 * Greet server. If provided, the first element of {@code args} is the name to use in the 73 * greeting. 74 */ main(String[] args)75 public static void main(String[] args) throws Exception { 76 // Access a service running on the local machine on port 50051 77 CompressingHelloWorldClient client = new CompressingHelloWorldClient("localhost", 50051); 78 try { 79 String user = "world"; 80 // Use the arg as the name to greet if provided 81 if (args.length > 0) { 82 user = args[0]; 83 } 84 client.greet(user); 85 } finally { 86 client.shutdown(); 87 } 88 } 89 } 90