/*
* Copyright 2015 Google, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.examples.abelanav2.grpc;
import com.examples.abelanav2.BackendConstants;
import io.grpc.ServerImpl;
import io.grpc.ServerInterceptors;
import io.grpc.transport.netty.NettyServerBuilder;
import java.util.logging.Logger;
/**
* The Abelana GRPC server main class.
*/
public final class AbelanaGrpcServer {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(AbelanaGrpcServer.class.getName());
/**
* The server implentation.
*/
private ServerImpl server;
/**
* Constructor for Abelana GRPC server.
*/
private AbelanaGrpcServer() {
}
/**
* Main launches the server from the command line.
* @param args command line arguments
* @throws Exception runtime exception
*/
public static void main(final String[] args) throws Exception {
final AbelanaGrpcServer server = new AbelanaGrpcServer();
server.start();
}
/**
* Starts the Abelana GRPC server.
* @throws Exception runtime Abelana GRPC server exception
*/
private void start() throws Exception {
server = NettyServerBuilder.forPort(BackendConstants.PORT).addService(ServerInterceptors
.intercept(AbelanaGrpc.bindService(new AbelanaGrpcImpl()),
new AuthHeaderServerInterceptor())).build().start();
LOGGER.info("Server started, listening on " + BackendConstants.PORT);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.err.println("*** shutting down gRPC server"
+ " since JVM is shutting down");
AbelanaGrpcServer.this.stop();
System.err.println("*** server shut down");
}
});
}
/**
* Stops the Abelana GRPC server.
*/
private void stop() {
if (server != null) {
server.shutdown();
}
}
}