• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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.nameresolve;
18 
19 import io.grpc.Server;
20 import io.grpc.ServerBuilder;
21 import io.grpc.examples.helloworld.GreeterGrpc;
22 import io.grpc.examples.helloworld.HelloReply;
23 import io.grpc.examples.helloworld.HelloRequest;
24 import io.grpc.stub.StreamObserver;
25 
26 import java.io.IOException;
27 import java.util.concurrent.TimeUnit;
28 import java.util.logging.Logger;
29 
30 public class NameResolveServer {
31     static public final int serverCount = 3;
32     static public final int startPort = 50051;
33     private static final Logger logger = Logger.getLogger(NameResolveServer.class.getName());
34     private Server[] servers;
35 
main(String[] args)36     public static void main(String[] args) throws IOException, InterruptedException {
37         final NameResolveServer server = new NameResolveServer();
38         server.start();
39         server.blockUntilShutdown();
40     }
41 
start()42     private void start() throws IOException {
43         servers = new Server[serverCount];
44         for (int i = 0; i < serverCount; i++) {
45             int port = startPort + i;
46             servers[i] = ServerBuilder.forPort(port)
47                     .addService(new GreeterImpl(port))
48                     .build()
49                     .start();
50             logger.info("Server started, listening on " + port);
51         }
52         Runtime.getRuntime().addShutdownHook(new Thread(() -> {
53             System.err.println("*** shutting down gRPC server since JVM is shutting down");
54             try {
55                 NameResolveServer.this.stop();
56             } catch (InterruptedException e) {
57                 e.printStackTrace(System.err);
58             }
59             System.err.println("*** server shut down");
60         }));
61     }
62 
stop()63     private void stop() throws InterruptedException {
64         for (int i = 0; i < serverCount; i++) {
65             if (servers[i] != null) {
66                 servers[i].shutdown().awaitTermination(30, TimeUnit.SECONDS);
67             }
68         }
69     }
70 
blockUntilShutdown()71     private void blockUntilShutdown() throws InterruptedException {
72         for (int i = 0; i < serverCount; i++) {
73             if (servers[i] != null) {
74                 servers[i].awaitTermination();
75             }
76         }
77     }
78 
79     static class GreeterImpl extends GreeterGrpc.GreeterImplBase {
80 
81         int port;
82 
GreeterImpl(int port)83         public GreeterImpl(int port) {
84             this.port = port;
85         }
86 
87         @Override
sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver)88         public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
89             HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName() + " from server<" + this.port + ">").build();
90             responseObserver.onNext(reply);
91             responseObserver.onCompleted();
92         }
93     }
94 }
95