use of org.apache.thrift.async.AsyncMethodCallback in project commons by twitter.
the class ThriftTest method testAsyncRetrySelection.
@Test
public void testAsyncRetrySelection() throws Exception {
// verify subclasses pass the retry filter
class HopelesslyLost extends NotFoundException {
}
Capture<AsyncMethodCallback<Integer>> callbackCapture1 = new Capture<AsyncMethodCallback<Integer>>();
expectAsyncServiceCall(true).calculateMass(eq("jake"), capture(callbackCapture1));
requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED), anyLong());
Capture<AsyncMethodCallback<Integer>> callbackCapture2 = new Capture<AsyncMethodCallback<Integer>>();
expectAsyncServiceRetry(true).calculateMass(eq("jake"), capture(callbackCapture2));
requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED), anyLong());
// Verifies that our callback was called.
TTransportException nonRetryableException = new TTransportException();
callback.onError(nonRetryableException);
Thrift<TestServiceAsync> thrift = createAsyncThrift(expectUnusedExecutorService());
control.replay();
TestServiceAsync testService = thrift.builder().withRetries(2).retryOn(NotFoundException.class).withConnectTimeout(ASYNC_CONNECT_TIMEOUT).create();
testService.calculateMass("jake", callback);
callbackCapture1.getValue().onError(new HopelesslyLost());
callbackCapture2.getValue().onError(nonRetryableException);
assertRequestsTotal(thrift, 1);
assertErrorsTotal(thrift, 1);
assertReconnectsTotal(thrift, 1);
assertTimeoutsTotal(thrift, 0);
control.verify();
}
use of org.apache.thrift.async.AsyncMethodCallback in project commons by twitter.
the class ThriftTest method testAsyncRetriesRecover.
@Test
public void testAsyncRetriesRecover() throws Exception {
// Capture the callback that Thift has wrapped around our callback.
Capture<AsyncMethodCallback<Integer>> callbackCapture = new Capture<AsyncMethodCallback<Integer>>();
// 1st call
expectAsyncServiceCall(true).calculateMass(eq("jake"), capture(callbackCapture));
expectLastCall().andThrow(new TTransportException());
requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED), anyLong());
// 1st retry recovers
expectAsyncServiceRetry(false).calculateMass(eq("jake"), capture(callbackCapture));
requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.SUCCESS), anyLong());
// Verifies that our callback was called.
callback.onComplete(42);
Thrift<TestServiceAsync> thrift = createAsyncThrift(expectUnusedExecutorService());
control.replay();
thrift.builder().withRetries(1).withConnectTimeout(ASYNC_CONNECT_TIMEOUT).create().calculateMass("jake", callback);
// Mimicks the async response from the server.
callbackCapture.getValue().onComplete(42);
assertRequestsTotal(thrift, 1);
assertErrorsTotal(thrift, 0);
assertReconnectsTotal(thrift, 0);
assertTimeoutsTotal(thrift, 0);
control.verify();
}
use of org.apache.thrift.async.AsyncMethodCallback in project commons by twitter.
the class ThriftFactoryTest method testCreateAsync.
@Test
public void testCreateAsync() throws IOException, InterruptedException, ThriftFactory.ThriftFactoryException {
final String[] responseHolder = new String[] { null };
final CountDownLatch done = new CountDownLatch(1);
AsyncMethodCallback<String> callback = new AsyncMethodCallback<String>() {
@Override
public void onComplete(String response) {
responseHolder[0] = response;
done.countDown();
}
@Override
public void onError(Throwable throwable) {
responseHolder[0] = throwable.toString();
done.countDown();
}
};
final Thrift<AsyncIface> thrift = ThriftFactory.create(GoodService.AsyncIface.class).withMaxConnectionsPerEndpoint(1).useFramedTransport(true).buildAsync(ImmutableSet.of(new InetSocketAddress(1234)));
addTearDown(new TearDown() {
@Override
public void tearDown() {
thrift.close();
}
});
GoodService.AsyncIface client = thrift.builder().blocking().create();
client.doWork(callback);
assertTrue("wasn't called back in time, callback got " + responseHolder[0], done.await(5000, TimeUnit.MILLISECONDS));
assertEquals(GoodService.DONE, responseHolder[0]);
}
use of org.apache.thrift.async.AsyncMethodCallback in project mlib by myshzzx.
the class AsyncTest1 method test2.
@Test
public void test2() throws Exception {
TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(new InetSocketAddress("l", 19000), 0);
TServer server = new THsHaServer(new THsHaServer.Args(serverTransport).processor(new TService1.AsyncProcessor<TService1.AsyncIface>(new Service2Impl())).protocolFactory(new TCompactProtocol.Factory()));
new Thread() {
@Override
public void run() {
server.serve();
}
}.start();
Thread.sleep(1000);
TNonblockingSocket transport = new TNonblockingSocket("l", 19000, 5000);
TService1.AsyncIface client = new TService1.AsyncClient(new TCompactProtocol.Factory(), new TAsyncClientManager(), transport);
byte[] b = { 1, 2, 3 };
AsyncMethodCallback<TService1.AsyncClient.getStr_call> h = new AsyncMethodCallback<TService1.AsyncClient.getStr_call>() {
@Override
public void onComplete(TService1.AsyncClient.getStr_call response) {
try {
System.out.println(response.getResult());
} catch (TException e) {
log.error("async get rsp fail.", e);
}
}
@Override
public void onError(Exception e) {
log.error("async call fail.", e);
}
};
for (int i = 0; i < 2; i++) {
client.getStr("mysh", ByteBuffer.wrap(b), h);
}
Thread.sleep(10000000);
}
use of org.apache.thrift.async.AsyncMethodCallback 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;
}
Aggregations