Search in sources :

Example 1 with ThriftCaller

use of com.twitter.common.thrift.callers.ThriftCaller in project commons by twitter.

the class Thrift method createClient.

private T createClient(Config config) {
    StatsProvider statsProvider = config.getStatsProvider();
    // lease/call/[invalidate]/release
    boolean debug = config.isDebug();
    Caller decorated = new ThriftCaller<T>(connectionPool, requestTracker, clientFactory, config.getConnectTimeout(), debug);
    // [retry]
    if (config.getMaxRetries() > 0) {
        decorated = new RetryingCaller(decorated, async, statsProvider, serviceName, config.getMaxRetries(), config.getRetryableExceptions(), debug);
    }
    // [deadline]
    if (config.getRequestTimeout().getValue() > 0) {
        Preconditions.checkArgument(!async, "Request deadlines may not be used with an asynchronous client.");
        decorated = new DeadlineCaller(decorated, async, executorService, config.getRequestTimeout());
    }
    // [debug]
    if (debug) {
        decorated = new DebugCaller(decorated, async);
    }
    // stats
    if (config.enableStats()) {
        decorated = new StatTrackingCaller(decorated, async, statsProvider, serviceName);
    }
    final Caller caller = decorated;
    final InvocationHandler invocationHandler = new InvocationHandler() {

        @Override
        public Object invoke(Object o, Method method, Object[] args) throws Throwable {
            AsyncMethodCallback callback = null;
            if (args != null && async) {
                List<Object> argsList = Lists.newArrayList(args);
                callback = extractCallback(argsList);
                args = argsList.toArray();
            }
            return caller.call(method, args, callback, null);
        }
    };
    @SuppressWarnings("unchecked") T instance = (T) Proxy.newProxyInstance(serviceInterface.getClassLoader(), new Class<?>[] { serviceInterface }, invocationHandler);
    return instance;
}
Also used : DeadlineCaller(com.twitter.common.thrift.callers.DeadlineCaller) ThriftCaller(com.twitter.common.thrift.callers.ThriftCaller) DebugCaller(com.twitter.common.thrift.callers.DebugCaller) Method(java.lang.reflect.Method) StatTrackingCaller(com.twitter.common.thrift.callers.StatTrackingCaller) InvocationHandler(java.lang.reflect.InvocationHandler) StatsProvider(com.twitter.common.stats.StatsProvider) StatTrackingCaller(com.twitter.common.thrift.callers.StatTrackingCaller) RetryingCaller(com.twitter.common.thrift.callers.RetryingCaller) DebugCaller(com.twitter.common.thrift.callers.DebugCaller) DeadlineCaller(com.twitter.common.thrift.callers.DeadlineCaller) ThriftCaller(com.twitter.common.thrift.callers.ThriftCaller) Caller(com.twitter.common.thrift.callers.Caller) AsyncMethodCallback(org.apache.thrift.async.AsyncMethodCallback) RetryingCaller(com.twitter.common.thrift.callers.RetryingCaller)

Aggregations

StatsProvider (com.twitter.common.stats.StatsProvider)1 Caller (com.twitter.common.thrift.callers.Caller)1 DeadlineCaller (com.twitter.common.thrift.callers.DeadlineCaller)1 DebugCaller (com.twitter.common.thrift.callers.DebugCaller)1 RetryingCaller (com.twitter.common.thrift.callers.RetryingCaller)1 StatTrackingCaller (com.twitter.common.thrift.callers.StatTrackingCaller)1 ThriftCaller (com.twitter.common.thrift.callers.ThriftCaller)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 AsyncMethodCallback (org.apache.thrift.async.AsyncMethodCallback)1