001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.thrift; 019 020import java.io.Closeable; 021import java.io.IOException; 022import org.slf4j.Logger; 023import org.slf4j.LoggerFactory; 024 025/** 026 * Run ThriftServer with passed arguments. Access the exception thrown after we complete run -- if 027 * an exception thrown -- via {@link #getRunException()}}. Call close to shutdown this Runner and 028 * hosted {@link ThriftServer}. 029 */ 030class ThriftServerRunner extends Thread implements Closeable { 031 private static final Logger LOG = LoggerFactory.getLogger(ThriftServerRunner.class); 032 Exception exception = null; 033 private final ThriftServer thriftServer; 034 private final String[] args; 035 036 ThriftServerRunner(ThriftServer thriftServer, String[] args) { 037 this.thriftServer = thriftServer; 038 this.args = args; 039 LOG.info("thriftServer={}, args={}", getThriftServer(), args); 040 } 041 042 ThriftServer getThriftServer() { 043 return this.thriftServer; 044 } 045 046 /** Returns Empty unless {@link #run()} threw an exception; if it did, access it here. */ 047 Exception getRunException() { 048 return this.exception; 049 } 050 051 @Override 052 public void run() { 053 try { 054 this.thriftServer.run(this.args); 055 } catch (Exception e) { 056 LOG.error("Run threw an exception", e); 057 this.exception = e; 058 } 059 } 060 061 @Override 062 public void close() throws IOException { 063 LOG.info("Stopping {}", this); 064 this.thriftServer.stop(); 065 } 066}