com.twitter.common.thrift
Class ThriftConnectionFactory

java.lang.Object
  extended by com.twitter.common.thrift.ThriftConnectionFactory
All Implemented Interfaces:
ConnectionFactory<Connection<org.apache.thrift.transport.TTransport,InetSocketAddress>>

public class ThriftConnectionFactory
extends Object
implements ConnectionFactory<Connection<org.apache.thrift.transport.TTransport,InetSocketAddress>>

A connection factory for thrift transport connections to a given host. This connection factory is lazy and will only create a configured maximum number of active connections - where a created connection that has not been destroyed is considered active.


Nested Class Summary
static class ThriftConnectionFactory.TransportType
           
 
Constructor Summary
ThriftConnectionFactory(InetSocketAddress endpoint, int maxConnections, boolean framedTransport)
          Creates a thrift connection factory.
ThriftConnectionFactory(InetSocketAddress endpoint, int maxConnections, ThriftConnectionFactory.TransportType transportType)
           
ThriftConnectionFactory(InetSocketAddress endpoint, int maxConnections, ThriftConnectionFactory.TransportType transportType, Amount<Long,Time> socketTimeout)
          Creates a thrift connection factory.
ThriftConnectionFactory(InetSocketAddress endpoint, int maxConnections, ThriftConnectionFactory.TransportType transportType, Amount<Long,Time> socketTimeout, Closure<Connection<org.apache.thrift.transport.TTransport,InetSocketAddress>> postCreateCallback, boolean sslTransport)
           
ThriftConnectionFactory(String host, int port, int maxConnections)
          Creates a thrift connection factory with a plain socket (non-framed transport).
ThriftConnectionFactory(String host, int port, int maxConnections, boolean framedTransport)
          Creates a thrift connection factory.
ThriftConnectionFactory(String host, int port, int maxConnections, ThriftConnectionFactory.TransportType transportType)
          Creates a thrift connection factory.
ThriftConnectionFactory(String host, int port, int maxConnections, ThriftConnectionFactory.TransportType transportType, Amount<Long,Time> socketTimeout)
          Creates a thrift connection factory.
 
Method Summary
 Connection<org.apache.thrift.transport.TTransport,InetSocketAddress> create(Amount<Long,Time> timeout)
          FIXME: shouldn't this throw TimeoutException instead of returning null in the timeout cases as per the ConnectionFactory.create javadoc?
 void destroy(Connection<org.apache.thrift.transport.TTransport,InetSocketAddress> connection)
          Destroys a connection.
 boolean mightCreate()
          Checks whether this factory might create a connection if requested.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ThriftConnectionFactory

public ThriftConnectionFactory(String host,
                               int port,
                               int maxConnections)
Creates a thrift connection factory with a plain socket (non-framed transport). This is the same as calling ThriftConnectionFactory(String, int, int, boolean) with framedTransport set to false.

Parameters:
host - Host to connect to.
port - Port to connect on.
maxConnections - Maximum number of connections for this host:port.

ThriftConnectionFactory

public ThriftConnectionFactory(String host,
                               int port,
                               int maxConnections,
                               boolean framedTransport)
Creates a thrift connection factory. If framedTransport is set to true, TFramedTransport will be used, otherwise a raw TSocket will be used.

Parameters:
host - Host to connect to.
port - Port to connect on.
maxConnections - Maximum number of connections for this host:port.
framedTransport - Whether to use framed or blocking transport.

ThriftConnectionFactory

public ThriftConnectionFactory(InetSocketAddress endpoint,
                               int maxConnections,
                               boolean framedTransport)
Creates a thrift connection factory. If framedTransport is set to true, TFramedTransport will be used, otherwise a raw TSocket will be used.

Parameters:
endpoint - Endpoint to connect to.
maxConnections - Maximum number of connections for this host:port.
framedTransport - Whether to use framed or blocking transport.

ThriftConnectionFactory

public ThriftConnectionFactory(String host,
                               int port,
                               int maxConnections,
                               ThriftConnectionFactory.TransportType transportType)
Creates a thrift connection factory. If framedTransport is set to true, TFramedTransport will be used, otherwise a raw TSocket will be used. If nonblocking is set to true, TNonblockingSocket will be used, otherwise a raw TSocket will be used. Timeouts are ignored when nonblocking transport is used.

Parameters:
host - Host to connect to.
port - Port to connect on.
maxConnections - Maximum number of connections for this host:port.
transportType - Whether to use normal blocking, framed blocking, or non-blocking (implicitly framed) transport.

ThriftConnectionFactory

public ThriftConnectionFactory(String host,
                               int port,
                               int maxConnections,
                               ThriftConnectionFactory.TransportType transportType,
                               Amount<Long,Time> socketTimeout)
Creates a thrift connection factory. If framedTransport is set to true, TFramedTransport will be used, otherwise a raw TSocket will be used. If nonblocking is set to true, TNonblockingSocket will be used, otherwise a raw TSocket will be used. Timeouts are ignored when nonblocking transport is used.

Parameters:
host - Host to connect to.
port - Port to connect on.
maxConnections - Maximum number of connections for this host:port.
transportType - Whether to use normal blocking, framed blocking, or non-blocking (implicitly framed) transport.
socketTimeout - timeout on thrift i/o operations, or null to default to connectTimeout o the blocking client.

ThriftConnectionFactory

public ThriftConnectionFactory(InetSocketAddress endpoint,
                               int maxConnections,
                               ThriftConnectionFactory.TransportType transportType)

ThriftConnectionFactory

public ThriftConnectionFactory(InetSocketAddress endpoint,
                               int maxConnections,
                               ThriftConnectionFactory.TransportType transportType,
                               Amount<Long,Time> socketTimeout)
Creates a thrift connection factory. If framedTransport is set to true, TFramedTransport will be used, otherwise a raw TSocket will be used. If nonblocking is set to true, TNonblockingSocket will be used, otherwise a raw TSocket will be used. Timeouts are ignored when nonblocking transport is used.

Parameters:
endpoint - Endpoint to connect to.
maxConnections - Maximum number of connections for this host:port.
transportType - Whether to use normal blocking, framed blocking, or non-blocking (implicitly framed) transport.
socketTimeout - timeout on thrift i/o operations, or null to default to connectTimeout o the blocking client.

ThriftConnectionFactory

public ThriftConnectionFactory(InetSocketAddress endpoint,
                               int maxConnections,
                               ThriftConnectionFactory.TransportType transportType,
                               Amount<Long,Time> socketTimeout,
                               Closure<Connection<org.apache.thrift.transport.TTransport,InetSocketAddress>> postCreateCallback,
                               boolean sslTransport)
Method Detail

mightCreate

public boolean mightCreate()
Description copied from interface: ConnectionFactory
Checks whether this factory might create a connection if requested.

Specified by:
mightCreate in interface ConnectionFactory<Connection<org.apache.thrift.transport.TTransport,InetSocketAddress>>
Returns:
true if this factory might create a connection at this point in time; ie a call to ConnectionFactory.create(com.twitter.common.quantity.Amount) might not have returned null. May return true to multiple threads if concurrently creating connections.

create

public Connection<org.apache.thrift.transport.TTransport,InetSocketAddress> create(Amount<Long,Time> timeout)
                                                                            throws org.apache.thrift.transport.TTransportException,
                                                                                   IOException
FIXME: shouldn't this throw TimeoutException instead of returning null in the timeout cases as per the ConnectionFactory.create javadoc?

Specified by:
create in interface ConnectionFactory<Connection<org.apache.thrift.transport.TTransport,InetSocketAddress>>
Parameters:
timeout - the maximum amount of time to wait
Returns:
a new connection or null if there are too many connections already
Throws:
org.apache.thrift.transport.TTransportException
IOException

destroy

public void destroy(Connection<org.apache.thrift.transport.TTransport,InetSocketAddress> connection)
Description copied from interface: ConnectionFactory
Destroys a connection. It is an error to attempt to destroy a connection this factory did not ConnectionFactory.create(com.twitter.common.quantity.Amount)

Specified by:
destroy in interface ConnectionFactory<Connection<org.apache.thrift.transport.TTransport,InetSocketAddress>>
Parameters:
connection - The connection to destroy.

toString

public String toString()
Overrides:
toString in class Object