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.ServerCalls.asyncUnaryCall; 20 21 import io.grpc.BindableService; 22 import io.grpc.Grpc; 23 import io.grpc.InsecureServerCredentials; 24 import io.grpc.Server; 25 import io.grpc.ServerServiceDefinition; 26 import io.grpc.examples.helloworld.GreeterGrpc; 27 import io.grpc.examples.helloworld.HelloReply; 28 import io.grpc.examples.helloworld.HelloRequest; 29 import io.grpc.stub.ServerCalls.UnaryMethod; 30 import io.grpc.stub.StreamObserver; 31 import java.io.IOException; 32 import java.util.concurrent.TimeUnit; 33 import java.util.logging.Logger; 34 35 /** 36 * Server that manages startup/shutdown of a {@code Greeter} server. 37 * 38 * <p>This is an advanced example of how to swap out the serialization logic. Normal users do not 39 * need to do this. This code is not intended to be a production-ready implementation, since JSON 40 * encoding is slow. Additionally, JSON serialization as implemented may be not resilient to 41 * malicious input. 42 * 43 * <p>If you are considering implementing your own serialization logic, contact the grpc team at 44 * https://groups.google.com/forum/#!forum/grpc-io 45 */ 46 public class HelloJsonServer { 47 private static final Logger logger = Logger.getLogger(HelloJsonServer.class.getName()); 48 49 private Server server; 50 start()51 private void start() throws IOException { 52 /* The port on which the server should run */ 53 int port = 50051; 54 server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create()) 55 .addService(new GreeterImpl()) 56 .build() 57 .start(); 58 logger.info("Server started, listening on " + port); 59 Runtime.getRuntime().addShutdownHook(new Thread() { 60 @Override 61 public void run() { 62 // Use stderr here since the logger may have been reset by its JVM shutdown hook. 63 System.err.println("*** shutting down gRPC server since JVM is shutting down"); 64 try { 65 HelloJsonServer.this.stop(); 66 } catch (InterruptedException e) { 67 e.printStackTrace(System.err); 68 } 69 System.err.println("*** server shut down"); 70 } 71 }); 72 } 73 stop()74 private void stop() throws InterruptedException { 75 if (server != null) { 76 server.shutdown().awaitTermination(30, TimeUnit.SECONDS); 77 } 78 } 79 80 /** 81 * Await termination on the main thread since the grpc library uses daemon threads. 82 */ blockUntilShutdown()83 private void blockUntilShutdown() throws InterruptedException { 84 if (server != null) { 85 server.awaitTermination(); 86 } 87 } 88 89 /** 90 * Main launches the server from the command line. 91 */ main(String[] args)92 public static void main(String[] args) throws IOException, InterruptedException { 93 final HelloJsonServer server = new HelloJsonServer(); 94 server.start(); 95 server.blockUntilShutdown(); 96 } 97 98 private static class GreeterImpl implements BindableService { 99 sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver)100 private void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) { 101 HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build(); 102 responseObserver.onNext(reply); 103 responseObserver.onCompleted(); 104 } 105 106 @Override bindService()107 public ServerServiceDefinition bindService() { 108 return io.grpc.ServerServiceDefinition 109 .builder(GreeterGrpc.getServiceDescriptor().getName()) 110 .addMethod(HelloJsonClient.HelloJsonStub.METHOD_SAY_HELLO, 111 asyncUnaryCall( 112 new UnaryMethod<HelloRequest, HelloReply>() { 113 @Override 114 public void invoke( 115 HelloRequest request, StreamObserver<HelloReply> responseObserver) { 116 sayHello(request, responseObserver); 117 } 118 })) 119 .build(); 120 } 121 } 122 } 123