• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.header;
18 
19 import io.grpc.Server;
20 import io.grpc.ServerBuilder;
21 import io.grpc.ServerInterceptors;
22 import io.grpc.examples.helloworld.GreeterGrpc;
23 import io.grpc.examples.helloworld.HelloReply;
24 import io.grpc.examples.helloworld.HelloRequest;
25 import io.grpc.stub.StreamObserver;
26 import java.io.IOException;
27 import java.util.logging.Logger;
28 
29 /**
30  * A simple server that like {@link io.grpc.examples.helloworld.HelloWorldServer}.
31  * You can get and response any header in {@link io.grpc.examples.header.HeaderServerInterceptor}.
32  */
33 public class CustomHeaderServer {
34   private static final Logger logger = Logger.getLogger(CustomHeaderServer.class.getName());
35 
36   /* The port on which the server should run */
37   private static final int PORT = 50051;
38   private Server server;
39 
start()40   private void start() throws IOException {
41     server = ServerBuilder.forPort(PORT)
42         .addService(ServerInterceptors.intercept(new GreeterImpl(), new HeaderServerInterceptor()))
43         .build()
44         .start();
45     logger.info("Server started, listening on " + PORT);
46     Runtime.getRuntime().addShutdownHook(new Thread() {
47       @Override
48       public void run() {
49         // Use stderr here since the logger may have been reset by its JVM shutdown hook.
50         System.err.println("*** shutting down gRPC server since JVM is shutting down");
51         CustomHeaderServer.this.stop();
52         System.err.println("*** server shut down");
53       }
54     });
55   }
56 
stop()57   private void stop() {
58     if (server != null) {
59       server.shutdown();
60     }
61   }
62 
63   /**
64    * Await termination on the main thread since the grpc library uses daemon threads.
65    */
blockUntilShutdown()66   private void blockUntilShutdown() throws InterruptedException {
67     if (server != null) {
68       server.awaitTermination();
69     }
70   }
71 
72   /**
73    * Main launches the server from the command line.
74    */
main(String[] args)75   public static void main(String[] args) throws IOException, InterruptedException {
76     final CustomHeaderServer server = new CustomHeaderServer();
77     server.start();
78     server.blockUntilShutdown();
79   }
80 
81   private static class GreeterImpl extends GreeterGrpc.GreeterImplBase {
82 
83     @Override
sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver)84     public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
85       HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build();
86       responseObserver.onNext(reply);
87       responseObserver.onCompleted();
88     }
89   }
90 }
91