use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class ConfigWriter method writeConfig.
public void writeConfig() throws ExecutionException, TimeoutException, InterruptedException {
long startTime = System.currentTimeMillis();
FutureCallback<None> callback = new FutureCallback<None>();
_store.start(callback);
callback.get(_timeout, _timeoutUnit);
final Semaphore outstandingPutSemaphore = new Semaphore(_maxOutstandingWrites);
for (final String key : _source.keySet()) {
Map<String, Object> map = merge(_source.get(key), _defaultMap);
T properties = _builder.fromMap(map);
Callback<None> putCallback = new Callback<None>() {
@Override
public void onSuccess(None none) {
outstandingPutSemaphore.release();
}
@Override
public void onError(Throwable e) {
_log.error("Put failed for {}", key, e);
outstandingPutSemaphore.release();
}
};
if (!outstandingPutSemaphore.tryAcquire(_timeout, _timeoutUnit)) {
_log.error("Put timed out for {}", key);
throw new TimeoutException();
}
_store.put(key, properties, putCallback);
}
// Wait until all puts are finished.
if (!outstandingPutSemaphore.tryAcquire(_maxOutstandingWrites, _timeout, _timeoutUnit)) {
_log.error("Put timed out with {} outstanding writes", _maxOutstandingWrites - outstandingPutSemaphore.availablePermits());
throw new TimeoutException();
}
FutureCallback<None> shutdownCallback = new FutureCallback<None>();
_store.shutdown(shutdownCallback);
shutdownCallback.get(_timeout, _timeoutUnit);
long elapsedTime = System.currentTimeMillis() - startTime;
_log.info("A total of {}.{}s elapsed to write configs to store.", elapsedTime / 1000, elapsedTime % 1000);
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class TestRouteLookupClient method testRouteLookupClientCallback.
@Test
public void testRouteLookupClientCallback() throws InterruptedException, ExecutionException, TimeoutException {
RouteLookup routeLookup = new SimpleTestRouteLookup();
final D2Client d2Client = new D2ClientBuilder().setZkHosts("localhost:2121").build();
d2Client.start(new FutureCallback<None>());
RouteLookupClient routeLookupClient = new RouteLookupClient(d2Client, routeLookup, "WestCoast");
RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("d2://simple_uri")).build();
FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
routeLookupClient.restRequest(dummyRestRequest, futureCallback, "5555");
try {
RestResponse response = futureCallback.get(10, TimeUnit.SECONDS);
Assert.fail("Unexpected success, request should have thrown a ServiceUnavailableException");
} catch (Exception e) {
String message = e.getMessage();
if (!message.contains("_serviceName=simple_uriWestCoast5555Foo")) {
Assert.fail("request was not rewritten to point at the d2 service simple_uriWestCoast5555Foo");
}
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class RouteLookupClient method restRequest.
@Override
public Future<RestResponse> restRequest(final RestRequest request, final RequestContext requestContext, String routekey) {
final FutureCallback<String> futureCallback = new FutureCallback<String>();
String originalServiceName = LoadBalancerUtil.getServiceNameFromUri(request.getURI());
String resultServiceName;
_routeLookup.run(originalServiceName, _routingGroup, routekey, futureCallback);
try {
resultServiceName = futureCallback.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
RestRequest resultRequest = createNewRequestWithNewServiceName(request, resultServiceName);
Future<RestResponse> resultFuture = _client.restRequest(resultRequest, requestContext);
return resultFuture;
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class ZooKeeperAnnouncerJmx method markDown.
@Override
public void markDown() throws PropertyStoreException {
FutureCallback<None> callback = new FutureCallback<None>();
_announcer.markDown(callback);
try {
callback.get(10, TimeUnit.SECONDS);
} catch (Exception e) {
throw new PropertyStoreException(e);
}
}
use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.
the class ZooKeeperAnnouncerJmx method reset.
@Override
public void reset() throws PropertyStoreException {
FutureCallback<None> callback = new FutureCallback<None>();
_announcer.reset(callback);
try {
callback.get(10, TimeUnit.SECONDS);
} catch (Exception e) {
throw new PropertyStoreException(e);
}
}
Aggregations