1 /* 2 * Copyright 2023 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.multiplex; 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.StatusRuntimeException; 24 import io.grpc.examples.helloworld.GreeterGrpc; 25 import io.grpc.examples.helloworld.HelloReply; 26 import io.grpc.examples.helloworld.HelloRequest; 27 import io.grpc.examples.echo.EchoGrpc; 28 import io.grpc.examples.echo.EchoRequest; 29 import io.grpc.examples.echo.EchoResponse; 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 * A sample gRPC server that serves both the Greeting and Echo services. 37 */ 38 public class MultiplexingServer { 39 40 private static final Logger logger = Logger.getLogger(MultiplexingServer.class.getName()); 41 42 private final int port; 43 private Server server; 44 MultiplexingServer(int port)45 public MultiplexingServer(int port) throws IOException { 46 this.port = port; 47 } 48 start()49 private void start() throws IOException { 50 /* The port on which the server should run */ 51 server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create()) 52 .addService(new MultiplexingServer.GreeterImpl()) 53 .addService(new EchoService()) 54 .build() 55 .start(); 56 logger.info("Server started, listening on " + port); 57 Runtime.getRuntime().addShutdownHook(new Thread() { 58 @Override 59 public void run() { 60 // Use stderr here since the logger may have been reset by its JVM shutdown hook. 61 System.err.println("*** shutting down gRPC server since JVM is shutting down"); 62 try { 63 MultiplexingServer.this.stop(); 64 } catch (InterruptedException e) { 65 e.printStackTrace(System.err); 66 } 67 System.err.println("*** server shut down"); 68 } 69 }); 70 } 71 stop()72 private void stop() throws InterruptedException { 73 if (server != null) { 74 server.shutdown().awaitTermination(30, TimeUnit.SECONDS); 75 } 76 } 77 78 /** 79 * Await termination on the main thread since the grpc library uses daemon threads. 80 */ blockUntilShutdown()81 private void blockUntilShutdown() throws InterruptedException { 82 if (server != null) { 83 server.awaitTermination(); 84 } 85 } 86 87 /** 88 * Main launches the server from the command line. 89 */ main(String[] args)90 public static void main(String[] args) throws IOException, InterruptedException { 91 System.setProperty("java.util.logging.SimpleFormatter.format", 92 "%1$tH:%1$tM:%1$tS %4$s %2$s: %5$s%6$s%n"); 93 94 final MultiplexingServer server = new MultiplexingServer(50051); 95 server.start(); 96 server.blockUntilShutdown(); 97 } 98 99 static class GreeterImpl extends GreeterGrpc.GreeterImplBase { 100 101 @Override sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver)102 public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) { 103 logger.info("Received sayHello request: " + req.getName()); 104 HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build(); 105 responseObserver.onNext(reply); 106 responseObserver.onCompleted(); 107 } 108 } 109 } 110