com.twitter.common.thrift
Class ThriftServer

java.lang.Object
  extended by com.twitter.common.thrift.ThriftServer

public abstract class ThriftServer
extends Object

Implementation of common functionality to satisfy the twitter ThriftService interface.


Nested Class Summary
static class ThriftServer.ServerSetup
          Represents the server configuration variables needed to construct a TServer.
 
Field Summary
static Supplier<org.apache.thrift.protocol.TProtocolFactory> BINARY_PROTOCOL
           
static Supplier<org.apache.thrift.protocol.TProtocolFactory> COMPACT_PROTOCOL
           
static ExceptionalFunction<ThriftServer.ServerSetup,org.apache.thrift.server.TServer,org.apache.thrift.transport.TTransportException> NONBLOCKING_SERVER
          This field actually provides a THsHaServer (Nonblocking server which had a thread pool)
static ExceptionalFunction<ThriftServer.ServerSetup,org.apache.thrift.server.TServer,org.apache.thrift.transport.TTransportException> THREADPOOL_SERVER
           
 
Constructor Summary
ThriftServer(String name, String version)
          Creates a new default thrift server, which uses a TThreadPoolServer and
ThriftServer(String name, String version, Supplier<org.apache.thrift.protocol.TProtocolFactory> protoFactorySupplier, ExceptionalFunction<ThriftServer.ServerSetup,org.apache.thrift.server.TServer,org.apache.thrift.transport.TTransportException> serverSupplier)
          Creates a new thrift server with the provided configuration.
 
Method Summary
 void awaitShutdown(Amount<Long,Time> timeout)
          Attempts to shut down this server, and waits for the shutdown operation to complete.
protected  void doStart(ThriftServer.ServerSetup setup)
           
 int getListeningPort()
           
 String getName()
           
 Status getStatus()
           
 String getVersion()
           
protected  void setStatus(Status status)
          Changes the status of the server.
 void shutdown()
          Attempts to shut down the server.
 void start(int port, org.apache.thrift.TProcessor processor)
          Starts the server.
 void start(ThriftServer.ServerSetup setup)
          Starts the server.
protected  void tryShutdown()
          Notification to the server that a shutdown request has been made, and the server is no longer processing requests.
 long uptime()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BINARY_PROTOCOL

public static final Supplier<org.apache.thrift.protocol.TProtocolFactory> BINARY_PROTOCOL

COMPACT_PROTOCOL

public static final Supplier<org.apache.thrift.protocol.TProtocolFactory> COMPACT_PROTOCOL

THREADPOOL_SERVER

public static final ExceptionalFunction<ThriftServer.ServerSetup,org.apache.thrift.server.TServer,org.apache.thrift.transport.TTransportException> THREADPOOL_SERVER

NONBLOCKING_SERVER

public static final ExceptionalFunction<ThriftServer.ServerSetup,org.apache.thrift.server.TServer,org.apache.thrift.transport.TTransportException> NONBLOCKING_SERVER
This field actually provides a THsHaServer (Nonblocking server which had a thread pool)

Constructor Detail

ThriftServer

public ThriftServer(String name,
                    String version)
Creates a new default thrift server, which uses a TThreadPoolServer and

Parameters:
name - Name for the server.
version - Version identifier.

ThriftServer

public ThriftServer(String name,
                    String version,
                    Supplier<org.apache.thrift.protocol.TProtocolFactory> protoFactorySupplier,
                    ExceptionalFunction<ThriftServer.ServerSetup,org.apache.thrift.server.TServer,org.apache.thrift.transport.TTransportException> serverSupplier)
Creates a new thrift server with the provided configuration.

Parameters:
name - Name for the server.
version - Version identifier.
protoFactorySupplier - Supplier to build the protocol factory to use.
serverSupplier - Function to build a TServer object based on the server setup.
Method Detail

start

public void start(int port,
                  org.apache.thrift.TProcessor processor)
Starts the server. This may be called at any point except when the server is already alive. That is, it's allowable to start, stop, and re-start the server.

Parameters:
port - The port to listen on.
processor - The processor to handle requests.

start

public void start(ThriftServer.ServerSetup setup)
Starts the server. This may be called at any point except when the server is already alive. That is, it's allowable to start, stop, and re-start the server.

Parameters:
setup - options for server

doStart

protected void doStart(ThriftServer.ServerSetup setup)
                throws org.apache.thrift.transport.TTransportException
Throws:
org.apache.thrift.transport.TTransportException

getListeningPort

public int getListeningPort()

getName

public String getName()

getVersion

public String getVersion()

getStatus

public Status getStatus()

setStatus

protected void setStatus(Status status)
Changes the status of the server.

Parameters:
status - New status.

uptime

public long uptime()

tryShutdown

protected void tryShutdown()
                    throws Exception
Notification to the server that a shutdown request has been made, and the server is no longer processing requests. The implementer may veto the shutdown by throwing an exception. A veto would suggest a failure to terminate backend connections in a timely manner.

Throws:
Exception - If the shutdown request could not be honored.

shutdown

public void shutdown()
Attempts to shut down the server. The server may be shut down at any time, though the request will be ignored if the server is already stopped.


awaitShutdown

public void awaitShutdown(Amount<Long,Time> timeout)
                   throws InterruptedException
Attempts to shut down this server, and waits for the shutdown operation to complete.

Parameters:
timeout - Maximum amount of time to wait for shutdown before giving up. a timeout of zero means wait forever.
Throws:
InterruptedException - If interrupted while waiting for shutdown.