• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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.hostname;
18 
19 import io.grpc.Grpc;
20 import io.grpc.InsecureServerCredentials;
21 import io.grpc.Server;
22 import io.grpc.ServerBuilder;
23 import io.grpc.health.v1.HealthCheckResponse.ServingStatus;
24 import io.grpc.protobuf.services.ProtoReflectionService;
25 import io.grpc.services.HealthStatusManager;
26 import java.io.IOException;
27 import java.util.concurrent.TimeUnit;
28 
29 /**
30  * A server that hosts HostnameGreeter, plus infrastructure services like health and reflection.
31  *
32  * <p>This server is intended to be a general purpose "dummy" server.
33  */
34 public final class HostnameServer {
main(String[] args)35   public static void main(String[] args) throws IOException, InterruptedException {
36     int port = 50051;
37     String hostname = null;
38     if (args.length >= 1) {
39       try {
40         port = Integer.parseInt(args[0]);
41       } catch (NumberFormatException ex) {
42         System.err.println("Usage: [port [hostname]]");
43         System.err.println("");
44         System.err.println("  port      The listen port. Defaults to " + port);
45         System.err.println("  hostname  The name clients will see in greet responses. ");
46         System.err.println("            Defaults to the machine's hostname");
47         System.exit(1);
48       }
49     }
50     if (args.length >= 2) {
51       hostname = args[1];
52     }
53     HealthStatusManager health = new HealthStatusManager();
54     final Server server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
55         .addService(new HostnameGreeter(hostname))
56         .addService(ProtoReflectionService.newInstance())
57         .addService(health.getHealthService())
58         .build()
59         .start();
60     System.out.println("Listening on port " + port);
61     Runtime.getRuntime().addShutdownHook(new Thread() {
62       @Override
63       public void run() {
64         // Start graceful shutdown
65         server.shutdown();
66         try {
67           // Wait for RPCs to complete processing
68           if (!server.awaitTermination(30, TimeUnit.SECONDS)) {
69             // That was plenty of time. Let's cancel the remaining RPCs
70             server.shutdownNow();
71             // shutdownNow isn't instantaneous, so give a bit of time to clean resources up
72             // gracefully. Normally this will be well under a second.
73             server.awaitTermination(5, TimeUnit.SECONDS);
74           }
75         } catch (InterruptedException ex) {
76           server.shutdownNow();
77         }
78       }
79     });
80     // This would normally be tied to the service's dependencies. For example, if HostnameGreeter
81     // used a Channel to contact a required service, then when 'channel.getState() ==
82     // TRANSIENT_FAILURE' we'd want to set NOT_SERVING. But HostnameGreeter has no dependencies, so
83     // hard-coding SERVING is appropriate.
84     health.setStatus("", ServingStatus.SERVING);
85     server.awaitTermination();
86   }
87 }
88