com.qubole.rubix.bookkeeper.BookKeeperServer.java Source code

Java tutorial

Introduction

Here is the source code for com.qubole.rubix.bookkeeper.BookKeeperServer.java

Source

/**
 * Copyright (c) 2016. Qubole Inc
 * 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. See accompanying LICENSE file.
 */
package com.qubole.rubix.bookkeeper;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;

import static com.qubole.rubix.spi.CacheConfig.getServerMaxThreads;
import static com.qubole.rubix.spi.CacheConfig.getServerPort;

/**
 * Created by stagra on 15/2/16.
 */
public class BookKeeperServer {
    public static BookKeeper bookKeeper;
    public static BookKeeperService.Processor processor;

    public static Configuration conf;

    private static TServer server;

    private static Log log = LogFactory.getLog(BookKeeperServer.class.getName());

    private BookKeeperServer() {
    }

    public static void main(String[] args) {
        conf = new Configuration();

        Runnable bookKeeperServer = new Runnable() {
            public void run() {
                startServer(conf);
            }
        };

        new Thread(bookKeeperServer).run();
    }

    public static void startServer(Configuration conf) {
        bookKeeper = new BookKeeper(conf);
        processor = new BookKeeperService.Processor(bookKeeper);
        log.info("Starting BookKeeperServer on port " + getServerPort(conf));
        try {
            TServerTransport serverTransport = new TServerSocket(getServerPort(conf));
            server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor)
                    .maxWorkerThreads(getServerMaxThreads(conf)));

            server.serve();
        } catch (TTransportException e) {
            e.printStackTrace();
            log.error(String.format("Error starting server %s", Throwables.getStackTraceAsString(e)));
        }
    }

    public static void stopServer() {
        server.stop();
    }

    @VisibleForTesting
    public static boolean isServerUp() {
        if (server != null) {
            return server.isServing();
        }

        return false;
    }
}