com.twitter.common.thrift
Class ThriftFactory<T>

java.lang.Object
  extended by com.twitter.common.thrift.ThriftFactory<T>

public class ThriftFactory<T>
extends Object

A utility that provides convenience methods to build common Thrifts. The thrift factory allows you to specify parameters that define how the client connects to and communicates with servers, such as the transport type, connection settings, and load balancing. Request-level settings like sync/async and retries should be set on the Thrift instance that this factory will create. The factory will attempt to provide reasonable defaults to allow the caller to minimize the amount of necessary configuration. Currently, the default behavior includes:


Nested Class Summary
static class ThriftFactory.ThriftFactoryException
           
 
Method Summary
 Thrift<T> build(DynamicHostSet<ServiceInstance> hostSet)
          Creates a synchronous thrift client that will communicate with a dynamic host set.
 Thrift<T> build(Set<InetSocketAddress> backends)
          Creates the thrift client, and initializes connection pools.
 Thrift<T> buildAsync(DynamicHostSet<ServiceInstance> hostSet)
          Creates an asynchronous thrift client that will communicate with a dynamic host set.
 Thrift<T> buildAsync(Set<InetSocketAddress> backends)
          Creates an asynchronous thrift client that will communicate with a fixed set of backends.
static
<T> ThriftFactory<T>
create(Class<T> serviceInterface)
           
 TrafficMonitor<InetSocketAddress> getMonitor()
           
 ThriftFactory<T> useFramedTransport(boolean framedTransport)
          Instructs the factory whether framed transport should be used.
 ThriftFactory<T> withClientFactory(Function<org.apache.thrift.transport.TTransport,T> clientFactory)
           
 ThriftFactory<T> withDeadConnectionRestoreInterval(Amount<Long,Time> connectionRestoreInterval)
          Specifies the interval at which dead endpoint connections should be checked and revived.
 ThriftFactory<T> withLoadBalancingStrategy(LoadBalancingStrategy<InetSocketAddress> strategy)
          Specifies the load balancer to use when interacting with multiple backends.
 ThriftFactory<T> withMaxConnectionsPerEndpoint(int maxConnectionsPerEndpoint)
          Specifies the maximum number of connections that should be made to any single endpoint.
 ThriftFactory<T> withPostCreateCallback(Closure<Connection<org.apache.thrift.transport.TTransport,InetSocketAddress>> postCreateCallback)
          Specifies the callback to notify when a connection has been created.
 ThriftFactory<T> withServiceName(String serviceName)
          Name to be passed to Thrift constructor, used in stats.
 ThriftFactory<T> withSocketTimeout(Amount<Long,Time> socketTimeout)
          Specifies the net read/write timeout to set via SO_TIMEOUT on the thrift blocking client or AsyncClient.setTimeout on the thrift async client.
 ThriftFactory<T> withSslEnabled()
           
 ThriftFactory<T> withStatsProvider(StatsProvider statsProvider)
          Registers a custom stats provider to use to track various client stats.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

create

public static <T> ThriftFactory<T> create(Class<T> serviceInterface)

getMonitor

public TrafficMonitor<InetSocketAddress> getMonitor()

build

public Thrift<T> build(Set<InetSocketAddress> backends)
Creates the thrift client, and initializes connection pools.

Parameters:
backends - Backends to connect to.
Returns:
A new thrift client.

build

public Thrift<T> build(DynamicHostSet<ServiceInstance> hostSet)
                throws ThriftFactory.ThriftFactoryException
Creates a synchronous thrift client that will communicate with a dynamic host set.

Parameters:
hostSet - The host set to use as a backend.
Returns:
A thrift client.
Throws:
ThriftFactory.ThriftFactoryException - If an error occurred while creating the client.

buildAsync

public Thrift<T> buildAsync(Set<InetSocketAddress> backends)
                     throws ThriftFactory.ThriftFactoryException
Creates an asynchronous thrift client that will communicate with a fixed set of backends.

Parameters:
backends - Backends to connect to.
Returns:
A thrift client.
Throws:
ThriftFactory.ThriftFactoryException - If an error occurred while creating the client.

buildAsync

public Thrift<T> buildAsync(DynamicHostSet<ServiceInstance> hostSet)
                     throws ThriftFactory.ThriftFactoryException
Creates an asynchronous thrift client that will communicate with a dynamic host set.

Parameters:
hostSet - The host set to use as a backend.
Returns:
A thrift client.
Throws:
ThriftFactory.ThriftFactoryException - If an error occurred while creating the client.

withClientFactory

public ThriftFactory<T> withClientFactory(Function<org.apache.thrift.transport.TTransport,T> clientFactory)

withSslEnabled

public ThriftFactory<T> withSslEnabled()

withMaxConnectionsPerEndpoint

public ThriftFactory<T> withMaxConnectionsPerEndpoint(int maxConnectionsPerEndpoint)
Specifies the maximum number of connections that should be made to any single endpoint.

Parameters:
maxConnectionsPerEndpoint - Maximum number of connections per endpoint.
Returns:
A reference to the factory.

withDeadConnectionRestoreInterval

public ThriftFactory<T> withDeadConnectionRestoreInterval(Amount<Long,Time> connectionRestoreInterval)
Specifies the interval at which dead endpoint connections should be checked and revived.

Parameters:
connectionRestoreInterval - the time interval to check.
Returns:
A reference to the factory.

useFramedTransport

public ThriftFactory<T> useFramedTransport(boolean framedTransport)
Instructs the factory whether framed transport should be used.

Parameters:
framedTransport - Whether to use framed transport.
Returns:
A reference to the factory.

withLoadBalancingStrategy

public ThriftFactory<T> withLoadBalancingStrategy(LoadBalancingStrategy<InetSocketAddress> strategy)
Specifies the load balancer to use when interacting with multiple backends.

Parameters:
strategy - Load balancing strategy.
Returns:
A reference to the factory.

withSocketTimeout

public ThriftFactory<T> withSocketTimeout(Amount<Long,Time> socketTimeout)
Specifies the net read/write timeout to set via SO_TIMEOUT on the thrift blocking client or AsyncClient.setTimeout on the thrift async client. Defaults to the connectTimeout on the blocking client if not set.

Parameters:
socketTimeout - timeout on thrift i/o operations
Returns:
A reference to the factory.

withPostCreateCallback

public ThriftFactory<T> withPostCreateCallback(Closure<Connection<org.apache.thrift.transport.TTransport,InetSocketAddress>> postCreateCallback)
Specifies the callback to notify when a connection has been created. The callback may be used to make thrift calls to the connection, but must not invalidate it. Defaults to a no-op closure.

Parameters:
postCreateCallback - function to setup new connections
Returns:
A reference to the factory.

withStatsProvider

public ThriftFactory<T> withStatsProvider(StatsProvider statsProvider)
Registers a custom stats provider to use to track various client stats.

Parameters:
statsProvider - the StatsProvider to use
Returns:
A reference to the factory.

withServiceName

public ThriftFactory<T> withServiceName(String serviceName)
Name to be passed to Thrift constructor, used in stats.

Parameters:
serviceName - string to use
Returns:
A reference to the factory.